View Javadoc
1   package org.argeo.people.ui.dialogs;
2   
3   import java.util.UUID;
4   
5   import javax.jcr.Node;
6   import javax.jcr.PropertyType;
7   import javax.jcr.RepositoryException;
8   import javax.jcr.nodetype.NodeType;
9   
10  import org.argeo.connect.ConnectException;
11  import org.argeo.connect.ConnectNames;
12  import org.argeo.connect.resources.ResourcesService;
13  import org.argeo.connect.ui.ConnectUiUtils;
14  import org.argeo.connect.util.ConnectJcrUtils;
15  import org.argeo.eclipse.ui.EclipseUiUtils;
16  import org.argeo.eclipse.ui.Selected;
17  import org.argeo.people.PeopleException;
18  import org.argeo.people.PeopleNames;
19  import org.argeo.people.PeopleService;
20  import org.argeo.people.PeopleTypes;
21  import org.argeo.people.ui.PeopleMsg;
22  import org.argeo.people.util.PeopleJcrUtils;
23  import org.eclipse.jface.dialogs.MessageDialog;
24  import org.eclipse.jface.wizard.Wizard;
25  import org.eclipse.jface.wizard.WizardPage;
26  import org.eclipse.swt.SWT;
27  import org.eclipse.swt.layout.GridData;
28  import org.eclipse.swt.layout.GridLayout;
29  import org.eclipse.swt.widgets.Button;
30  import org.eclipse.swt.widgets.Composite;
31  import org.eclipse.swt.widgets.Text;
32  
33  /**
34   * Ask legal name and form, and an optional display name. Update the node that
35   * has been created and passed on finish. The session is *NOT* saved.
36   */
37  
38  public class NewOrgWizard extends Wizard implements PeopleNames {
39  
40  	// Context
41  	private Node draft;
42  
43  	private PeopleService peopleService;
44  	private ResourcesService resourcesService;
45  
46  	// This page widgets
47  	private Text legalNameTxt;
48  	// private Button useDistinctDisplayNameBtn;
49  	// private Text displayNameTxt;
50  	private Text legalFormTxt;
51  
52  	Button createUser;
53  	Text userFirstName, userLastName, userEmail;
54  
55  	public NewOrgWizard(Node org, PeopleService peopleService, ResourcesService resourcesService) {
56  		this.draft = org;
57  		this.peopleService = peopleService;
58  		this.resourcesService = resourcesService;
59  	}
60  
61  	@Override
62  	public void addPages() {
63  		try {
64  			MainInfoPage page = new MainInfoPage("Main page");
65  			addPage(page);
66  		} catch (Exception e) {
67  			throw new PeopleException("Cannot add page to wizard", e);
68  		}
69  		setWindowTitle(PeopleMsg.orgWizardWindowTitle.lead());
70  	}
71  
72  	/**
73  	 * Called when the user click on 'Finish' in the wizard. The task is then
74  	 * created and the corresponding session saved.
75  	 */
76  	@Override
77  	public boolean performFinish() {
78  		String legalName = legalNameTxt.getText();
79  		// boolean useDistinctDisplayName = useDistinctDisplayNameBtn.getSelection();
80  		String legalForm = legalFormTxt.getText();
81  		// String displayName = displayNameTxt.getText();
82  
83  		if (EclipseUiUtils.isEmpty(legalName)) {
84  			MessageDialog.openError(getShell(), "Non-valid information",
85  					"Please enter at least a legal or a display name that is not empty.");
86  			return false;
87  		}
88  
89  		ConnectJcrUtils.setJcrProperty(draft, PEOPLE_LEGAL_NAME, PropertyType.STRING, legalName);
90  		// if (useDistinctDisplayName)
91  		// ConnectJcrUtils.setJcrProperty(org, PEOPLE_DISPLAY_NAME, PropertyType.STRING,
92  		// displayName);
93  		if (EclipseUiUtils.notEmpty(legalForm))
94  			ConnectJcrUtils.setJcrProperty(draft, PEOPLE_LEGAL_FORM, PropertyType.STRING, legalForm);
95  
96  		if (createUser.getSelection()) {
97  			try {
98  				Node person = draft.addNode(PeopleNames.PEOPLE_ROLE);
99  				person.addMixin(PeopleTypes.PEOPLE_PERSON);
100 				String personUid = UUID.randomUUID().toString();
101 				// TODO centralize with new person page
102 				ConnectJcrUtils.setJcrProperty(person, ConnectNames.CONNECT_UID, PropertyType.STRING, personUid);
103 				ConnectJcrUtils.setJcrProperty(person, PeopleNames.PEOPLE_FIRST_NAME, PropertyType.STRING,
104 						userFirstName.getText());
105 				ConnectJcrUtils.setJcrProperty(person, PeopleNames.PEOPLE_LAST_NAME, PropertyType.STRING,
106 						userLastName.getText());
107 				ConnectJcrUtils.setJcrProperty(person, PeopleNames.PEOPLE_DISPLAY_NAME, PropertyType.STRING,
108 						userFirstName.getText() + " " + userLastName.getText());
109 				String email = userEmail.getText();
110 				ConnectJcrUtils.setJcrProperty(person, PeopleNames.PEOPLE_PRIMARY_EMAIL, PropertyType.STRING, email);
111 				PeopleJcrUtils.createEmail(resourcesService, peopleService, person, email, true, null, null);
112 			} catch (RepositoryException e) {
113 				throw new ConnectException("Cannot add person", e);
114 			}
115 		}
116 		return true;
117 	}
118 
119 	protected Node getDraft() {
120 		return draft;
121 	}
122 
123 	protected PeopleService getPeopleService() {
124 		return peopleService;
125 	}
126 
127 	protected ResourcesService getResourcesService() {
128 		return resourcesService;
129 	}
130 
131 	protected class MainInfoPage extends WizardPage {
132 		private static final long serialVersionUID = 1L;
133 
134 		public MainInfoPage(String pageName) {
135 			super(pageName);
136 			setTitle(PeopleMsg.orgWizardPageTitle.lead());
137 			// setMessage("Please fill out following information.");
138 		}
139 
140 		public void createControl(Composite parent) {
141 			parent.setLayout(new GridLayout(2, false));
142 
143 			// Legal Name
144 			ConnectUiUtils.createBoldLabel(parent, PeopleMsg.legalName);
145 			legalNameTxt = new Text(parent, SWT.BORDER);
146 			// legalNameTxt.setMessage("the legal name");
147 			legalNameTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
148 
149 			// Legal Form
150 			ConnectUiUtils.createBoldLabel(parent, PeopleMsg.legalForm);
151 			legalFormTxt = new Text(parent, SWT.BORDER);
152 			// legalFormTxt.setMessage("the legal name (Ltd, Org, GmbH...) ");
153 			legalFormTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
154 
155 			// Display Name
156 			// useDistinctDisplayNameBtn = new Button(parent, SWT.CHECK);
157 			// useDistinctDisplayNameBtn.setText("Define a display name that is not the
158 			// legal name");
159 			// useDistinctDisplayNameBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
160 			// true, false, 2, 1));
161 			//
162 			// ConnectWorkbenchUtils.createBoldLabel(parent, "Display Name");
163 			// displayNameTxt = new Text(parent, SWT.BORDER);
164 			// displayNameTxt.setMessage("an optional display name");
165 			// displayNameTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
166 			// false));
167 			// displayNameTxt.setEnabled(false);
168 			//
169 			// useDistinctDisplayNameBtn.addSelectionListener(new SelectionAdapter() {
170 			// private static final long serialVersionUID = 1L;
171 			//
172 			// @Override
173 			// public void widgetSelected(SelectionEvent e) {
174 			// displayNameTxt.setEnabled(useDistinctDisplayNameBtn.getSelection());
175 			// }
176 			// });
177 			createUser = new Button(parent, SWT.CHECK);
178 			createUser.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
179 			ConnectUiUtils.createBoldLabel(parent, PeopleMsg.personWizardPageTitle);
180 
181 			ConnectUiUtils.createBoldLabel(parent, PeopleMsg.firstName);
182 			userFirstName = new Text(parent, SWT.BORDER);
183 			userFirstName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
184 			ConnectUiUtils.createBoldLabel(parent, PeopleMsg.lastName);
185 			userLastName = new Text(parent, SWT.BORDER);
186 			userLastName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
187 			ConnectUiUtils.createBoldLabel(parent, PeopleMsg.email);
188 			userEmail = new Text(parent, SWT.BORDER);
189 			userEmail.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
190 
191 			createUser.addSelectionListener((Selected) (e) -> {
192 				updateUserForm();
193 			});
194 			createUser.setSelection(true);
195 			updateUserForm();
196 
197 			// Don't forget this.
198 			setControl(legalNameTxt);
199 			legalNameTxt.setFocus();
200 		}
201 
202 		private void updateUserForm() {
203 			userFirstName.setEnabled(createUser.getSelection());
204 			userLastName.setEnabled(createUser.getSelection());
205 			userEmail.setEnabled(createUser.getSelection());
206 		}
207 	}
208 }