View Javadoc
1   package org.argeo.people.util;
2   
3   import static org.argeo.connect.util.ConnectUtils.notEmpty;
4   
5   import java.util.Calendar;
6   
7   import javax.jcr.Node;
8   import javax.jcr.NodeIterator;
9   import javax.jcr.RepositoryException;
10  import javax.jcr.Session;
11  import javax.jcr.query.QueryResult;
12  import javax.jcr.query.qom.Constraint;
13  import javax.jcr.query.qom.DynamicOperand;
14  import javax.jcr.query.qom.QueryObjectModel;
15  import javax.jcr.query.qom.QueryObjectModelFactory;
16  import javax.jcr.query.qom.Selector;
17  import javax.jcr.query.qom.StaticOperand;
18  
19  import org.argeo.connect.ConnectNames;
20  import org.argeo.connect.resources.ResourcesService;
21  import org.argeo.connect.util.ConnectJcrUtils;
22  import org.argeo.jcr.JcrUtils;
23  import org.argeo.people.PeopleException;
24  import org.argeo.people.PeopleNames;
25  import org.argeo.people.PeopleService;
26  import org.argeo.people.PeopleTypes;
27  
28  /**
29   * Static utilitary methods to manage Person concepts in JCR. Rather use these
30   * methods than direct JCR queries in order to ease model evolution.
31   */
32  public class PersonJcrUtils implements PeopleNames {
33  
34  	public static String getVariousNameInfo(Node person) {
35  		StringBuilder nameInfo = new StringBuilder();
36  
37  		if (ConnectJcrUtils.get(person, PEOPLE_SALUTATION) != null) {
38  			nameInfo.append(ConnectJcrUtils.get(person, PEOPLE_SALUTATION));
39  			nameInfo.append(" ");
40  		}
41  		if (ConnectJcrUtils.get(person, PEOPLE_HONORIFIC_TITLE) != null) {
42  			nameInfo.append(ConnectJcrUtils.get(person, PEOPLE_HONORIFIC_TITLE));
43  			nameInfo.append(" ");
44  		}
45  
46  		if (ConnectJcrUtils.get(person, PEOPLE_FIRST_NAME) != null) {
47  			nameInfo.append(ConnectJcrUtils.get(person, PEOPLE_FIRST_NAME));
48  			nameInfo.append(" ");
49  		}
50  
51  		if (ConnectJcrUtils.get(person, PEOPLE_NICKNAME) != null) {
52  			nameInfo.append("(");
53  			nameInfo.append(ConnectJcrUtils.get(person, PEOPLE_NICKNAME));
54  			nameInfo.append(") ");
55  		}
56  
57  		if (ConnectJcrUtils.get(person, PEOPLE_LAST_NAME) != null) {
58  			nameInfo.append(ConnectJcrUtils.get(person, PEOPLE_LAST_NAME));
59  			nameInfo.append(" ");
60  		}
61  
62  		if (ConnectJcrUtils.get(person, PEOPLE_NAME_SUFFIX) != null) {
63  			nameInfo.append(ConnectJcrUtils.get(person, PEOPLE_NAME_SUFFIX));
64  			nameInfo.append(" ");
65  		}
66  		return nameInfo.toString();
67  	}
68  
69  	public static String getSecondaryName(Node person) {
70  		String secondaryName = null;
71  		String nickName = ConnectJcrUtils.get(person, PEOPLE_NICKNAME);
72  
73  		if (notEmpty(nickName)) {
74  			secondaryName = "Nickname: " + nickName;
75  		}
76  
77  		String maidenName = ConnectJcrUtils.get(person, PEOPLE_MAIDEN_NAME);
78  		if (notEmpty(maidenName)) {
79  			if (secondaryName != null)
80  				secondaryName += "   ";
81  			secondaryName += "Maiden name: " + maidenName;
82  		}
83  		return secondaryName == null ? "" : secondaryName;
84  	}
85  
86  	/**
87  	 * Helper to retrieve a person given her first and last Name. Must be refined.
88  	 */
89  	public static Node getPersonWithLastAndFirstName(Session session, String lastName, String firstName)
90  			throws RepositoryException {
91  		QueryObjectModelFactory factory = session.getWorkspace().getQueryManager().getQOMFactory();
92  		final String typeSelector = "person";
93  		Selector source = factory.selector(PeopleTypes.PEOPLE_PERSON, typeSelector);
94  		DynamicOperand dynOp = factory.propertyValue(source.getSelectorName(), PEOPLE_LAST_NAME);
95  		DynamicOperand dynOp2 = factory.propertyValue(source.getSelectorName(), PEOPLE_FIRST_NAME);
96  		StaticOperand statOp = factory.literal(session.getValueFactory().createValue(lastName));
97  		StaticOperand statOp2 = factory.literal(session.getValueFactory().createValue(firstName));
98  		Constraint defaultC = factory.comparison(dynOp, QueryObjectModelFactory.JCR_OPERATOR_EQUAL_TO, statOp);
99  		Constraint defaultC2 = factory.comparison(dynOp2, QueryObjectModelFactory.JCR_OPERATOR_EQUAL_TO, statOp2);
100 		defaultC = factory.and(defaultC, defaultC2);
101 
102 		QueryObjectModel query = factory.createQuery(source, defaultC, null, null);
103 		QueryResult result = query.execute();
104 		NodeIterator ni = result.getNodes();
105 		// TODO clean this to handle multiple result
106 		if (ni.hasNext())
107 			return ni.nextNode();
108 		return null;
109 	}
110 
111 	public static Node getPersonWithUsername(Session session, String username)
112 			throws RepositoryException {
113 		QueryObjectModelFactory factory = session.getWorkspace().getQueryManager().getQOMFactory();
114 		final String typeSelector = "person";
115 		Selector source = factory.selector(PeopleTypes.PEOPLE_USER, typeSelector);
116 		DynamicOperand dynOp = factory.propertyValue(source.getSelectorName(), PEOPLE_USERNAME);
117 		StaticOperand statOp = factory.literal(session.getValueFactory().createValue(username));
118 		Constraint defaultC = factory.comparison(dynOp, QueryObjectModelFactory.JCR_OPERATOR_EQUAL_TO, statOp);
119 
120 		QueryObjectModel query = factory.createQuery(source, defaultC, null, null);
121 		QueryResult result = query.execute();
122 		NodeIterator ni = result.getNodes();
123 		// TODO clean this to handle multiple result
124 		if (ni.hasNext())
125 			return ni.nextNode();
126 		return null;
127 	}
128 
129 	/**
130 	 * Add a job for a given person
131 	 * 
132 	 * @param role
133 	 *            the role of the given entity in this group. Cannot be null
134 	 * @param title
135 	 *            OPTIONAL: the nature of the subject in this relation, for instance
136 	 *            "Actor" or "Engineer" - Not yet implemented
137 	 */
138 	public static Node addJob(ResourcesService resourcesService, PeopleService peopleService, Node person, Node org,
139 			String department, String role, String title, boolean isPrimary, Calendar dateBegin, Calendar dateEnd,
140 			Boolean isCurrent) throws RepositoryException {
141 
142 		Node jobs = JcrUtils.mkdirs(person, PEOPLE_JOBS);
143 		Node job = jobs.addNode(org.getName());
144 		job.addMixin(PeopleTypes.PEOPLE_JOB);
145 		job.setProperty(PEOPLE_REF_UID, org.getProperty(ConnectNames.CONNECT_UID).getString());
146 
147 		if (notEmpty(role))
148 			job.setProperty(PEOPLE_ROLE, role);
149 		if (notEmpty(department))
150 			job.setProperty(PEOPLE_DEPARTMENT, department);
151 		if (notEmpty(title))
152 			throw new PeopleException("Position Nature: Unimplemented property ");
153 		if (dateBegin != null)
154 			job.setProperty(PEOPLE_DATE_BEGIN, dateBegin);
155 		if (dateEnd != null)
156 			job.setProperty(PEOPLE_DATE_END, dateEnd);
157 		if (isCurrent != null)
158 			job.setProperty(PEOPLE_IS_CURRENT, isCurrent);
159 
160 		if (isPrimary)
161 			PeopleJcrUtils.markAsPrimary(resourcesService, peopleService, jobs, job);
162 		// TODO manage primary concept
163 		return job;
164 	}
165 
166 	/**
167 	 * Shortcut to add a job for a given person using default values
168 	 * 
169 	 * @param role
170 	 *            the role of the given entity in this group. Cannot be null
171 	 */
172 	public static Node addJob(ResourcesService resourcesService, PeopleService peopleService, Node person, Node org,
173 			String department, String role, boolean isPrimary) throws RepositoryException {
174 		return addJob(resourcesService, peopleService, person, org, department, role, null, isPrimary, null, null,
175 				null);
176 	}
177 
178 	/**
179 	 * Shortcut to add a job for a given person using default values
180 	 * 
181 	 * @param role
182 	 *            the role of the given entity in this group. Cannot be null
183 	 */
184 	public static Node addJob(ResourcesService resourcesService, PeopleService peopleService, Node person, Node org,
185 			String role, boolean isPrimary) throws RepositoryException {
186 		return addJob(resourcesService, peopleService, person, org, null, role, null, isPrimary, null, null, null);
187 	}
188 
189 	public static Node getPrimaryJob(Node person) throws RepositoryException {
190 		// Rather
191 		if (person.hasNode(PEOPLE_JOBS)) {
192 			NodeIterator ni = person.getNode(PEOPLE_JOBS).getNodes();
193 			while (ni.hasNext()) {
194 				Node pJob = ni.nextNode();
195 				if (pJob.hasProperty(PeopleNames.PEOPLE_IS_PRIMARY)
196 						&& pJob.getProperty(PeopleNames.PEOPLE_IS_PRIMARY).getBoolean())
197 					return pJob;
198 			}
199 		}
200 		return null;
201 	}
202 }