3dbe100ef6134198a5933bbfcbff633414377f34
[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 import org.springframework.security.core.AuthenticationException;
17
18 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
19 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
20 import eu.etaxonomy.cdm.api.service.IUserService;
21 import eu.etaxonomy.cdm.model.common.User;
22 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
23 import eu.etaxonomy.taxeditor.model.MessagingUtils;
24 import eu.etaxonomy.taxeditor.store.CdmStore;
25 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
26
27 /**
28 * @author n.hoffmann
29 * @created Mar 10, 2011
30 * @version 1.0
31 */
32 public class PasswordWizard extends Wizard implements IConversationEnabled{
33
34 private PasswordWizardPage passwordPage;
35 private final User user;
36 private final ConversationHolder conversation;
37
38 /**
39 * @param user the user to change the password for
40 * @param conversation this optional parameter can be null. Only supply conversation if you
41 * need to run this wizard is a nested conversation
42 */
43 public PasswordWizard(User user, ConversationHolder conversation){
44 this.conversation = conversation;
45 this.user = user;
46 }
47
48 /* (non-Javadoc)
49 * @see org.eclipse.jface.wizard.Wizard#addPages()
50 */
51 @Override
52 public void addPages() {
53 CdmFormFactory formFactory = new CdmFormFactory(Display.getDefault());
54
55 passwordPage = new PasswordWizardPage(formFactory, getConversationHolder(), user);
56 addPage(passwordPage);
57 }
58
59 /* (non-Javadoc)
60 * @see org.eclipse.jface.wizard.Wizard#performFinish()
61 */
62 @Override
63 public boolean performFinish() {
64
65 ConversationHolder internalConversation = CdmStore.getCurrentApplicationConfiguration().NewConversation();
66 internalConversation.bind();
67 internalConversation.startTransaction();
68 try{
69 if(passwordPage.isChangingOwnPassword()){
70 // change own password with validating of old one
71 CdmStore.getService(IUserService.class).changePassword(passwordPage.getOldPassword(), passwordPage.getNewPassword());
72 }else{
73 // change others passwords
74 CdmStore.getService(IUserService.class).changePasswordForUser(user.getUsername(), passwordPage.getNewPassword());
75 }
76 internalConversation.commit(false);
77 internalConversation.unbind();
78 internalConversation.close();
79 return true;
80 }catch(AccessDeniedException e){
81 MessagingUtils.warningDialog("Could not change password", this, "The old password is not correct.");
82 return false;
83 }catch (AuthenticationException e){
84 MessagingUtils.warningDialog("Could not change password", this, "The old password is not correct.");
85
86 return false;
87 }catch(Exception e){
88 MessagingUtils.warningDialog("Problem with changing password", this, "The password could not be changed. " + e.getMessage());
89 return false;
90 }finally{
91 if(conversation != null){
92 conversation.bind();
93 }
94 }
95 }
96
97 /* (non-Javadoc)
98 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
99 */
100 @Override
101 public void update(CdmDataChangeMap arg0) {
102 // TODO Auto-generated method stub
103
104 }
105
106 /* (non-Javadoc)
107 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
108 */
109 @Override
110 public ConversationHolder getConversationHolder() {
111 return conversation;
112 }
113
114 }