f7db817d39ac79e1599b279fe5afb21f0131a2f2
[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 @Override
49 public void addPages() {
50 CdmFormFactory formFactory = new CdmFormFactory(Display.getDefault());
51
52 passwordPage = new PasswordWizardPage(formFactory, getConversationHolder(), user);
53 addPage(passwordPage);
54 }
55
56 @Override
57 public boolean performFinish() {
58
59 ConversationHolder internalConversation = CdmStore.getCurrentApplicationConfiguration().NewConversation();
60 internalConversation.bind();
61 internalConversation.startTransaction();
62 try{
63 if(passwordPage.isChangingOwnPassword()){
64 // change own password with validating of old one
65 CdmStore.getService(IUserService.class).changePassword(passwordPage.getOldPassword(), passwordPage.getNewPassword());
66 }else{
67 // change others passwords
68 CdmStore.getService(IUserService.class).changePasswordForUser(user.getUsername(), passwordPage.getNewPassword());
69 }
70 CdmStore.getService(IUserService.class).loadWithUpdate(user.getUuid());
71 internalConversation.commit(false);
72 internalConversation.unbind();
73 internalConversation.close();
74 return true;
75 }catch(AccessDeniedException e){
76 MessagingUtils.warningDialog("Could not change password", this, "The old password is not correct.");
77 return false;
78 }catch (AuthenticationException e){
79 MessagingUtils.warningDialog("Could not change password", this, "The old password is not correct.");
80
81 return false;
82 }catch(Exception e){
83 MessagingUtils.warningDialog("Problem with changing password", this, "The password could not be changed. " + e.getMessage());
84 return false;
85 }finally{
86 if(conversation != null){
87 conversation.bind();
88 }
89 }
90 }
91
92 @Override
93 public void update(CdmDataChangeMap arg0) {
94 }
95
96 @Override
97 public ConversationHolder getConversationHolder() {
98 return conversation;
99 }
100
101 }