- refactored LanguageStringWithLabelElement (isMultiLine flag)
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / password / PasswordWizard.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.password;
12
13 import org.eclipse.jface.wizard.Wizard;
14 import org.eclipse.swt.widgets.Display;
15 import org.springframework.security.access.AccessDeniedException;
16
17 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
18 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
19 import eu.etaxonomy.cdm.api.service.IUserService;
20 import eu.etaxonomy.cdm.model.common.User;
21 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
22 import eu.etaxonomy.taxeditor.model.MessagingUtils;
23 import eu.etaxonomy.taxeditor.store.CdmStore;
24 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
25
26 /**
27 * @author n.hoffmann
28 * @created Mar 10, 2011
29 * @version 1.0
30 */
31 public class PasswordWizard extends Wizard implements IConversationEnabled{
32
33 private PasswordWizardPage passwordPage;
34 private final User user;
35 private final ConversationHolder conversation;
36
37 /**
38 * @param user the user to change the password for
39 * @param conversation this optional parameter can be null. Only supply conversation if you
40 * need to run this wizard is a nested conversation
41 */
42 public PasswordWizard(User user, ConversationHolder conversation){
43 this.conversation = conversation;
44 this.user = user;
45 }
46
47 /* (non-Javadoc)
48 * @see org.eclipse.jface.wizard.Wizard#addPages()
49 */
50 @Override
51 public void addPages() {
52 CdmFormFactory formFactory = new CdmFormFactory(Display.getDefault());
53
54 passwordPage = new PasswordWizardPage(formFactory, getConversationHolder(), user);
55 addPage(passwordPage);
56 }
57
58 /* (non-Javadoc)
59 * @see org.eclipse.jface.wizard.Wizard#performFinish()
60 */
61 @Override
62 public boolean performFinish() {
63
64 ConversationHolder internalConversation = CdmStore.getCurrentApplicationConfiguration().NewConversation();
65 internalConversation.bind();
66 internalConversation.startTransaction();
67 try{
68 if(passwordPage.isChangingOwnPassword()){
69 // change own password with validating of old one
70 CdmStore.getService(IUserService.class).changePassword(passwordPage.getOldPassword(), passwordPage.getNewPassword());
71 }else{
72 // change others passwords
73 CdmStore.getService(IUserService.class).changePasswordForUser(user.getUsername(), passwordPage.getNewPassword());
74 }
75 internalConversation.commit(false);
76 internalConversation.unbind();
77 internalConversation.close();
78 return true;
79 }catch(AccessDeniedException e){
80 MessagingUtils.warningDialog("Could not change password", this, "The old password is not correct.");
81
82 return false;
83 }catch(Exception e){
84 MessagingUtils.warningDialog("Problem with changing password", this, "The password could not be changed. " + e.getMessage());
85 return false;
86 }finally{
87 if(conversation != null){
88 conversation.bind();
89 }
90 }
91 }
92
93 /* (non-Javadoc)
94 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
95 */
96 @Override
97 public void update(CdmDataChangeMap arg0) {
98 // TODO Auto-generated method stub
99
100 }
101
102 /* (non-Javadoc)
103 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
104 */
105 @Override
106 public ConversationHolder getConversationHolder() {
107 return conversation;
108 }
109
110 }