View Javadoc
1   package org.argeo.people.ui.exports;
2   
3   import static org.argeo.eclipse.ui.EclipseUiUtils.notEmpty;
4   
5   import javax.jcr.Node;
6   import javax.jcr.Property;
7   import javax.jcr.RepositoryException;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  import org.argeo.connect.util.ConnectJcrUtils;
12  import org.argeo.eclipse.ui.EclipseUiUtils;
13  import org.argeo.people.PeopleException;
14  import org.argeo.people.PeopleNames;
15  import org.argeo.people.PeopleService;
16  import org.argeo.people.PeopleTypes;
17  import org.argeo.people.util.PeopleJcrUtils;
18  import org.eclipse.jface.viewers.ColumnLabelProvider;
19  
20  /**
21   * Enable simple retrieval of primary organisation name for persons.
22   * 
23   * The requested primary organisation name is the one of the organisation linked
24   * with the primary address if such exists and is a work address. See v0.4
25   * specification for more info
26   */
27  public class PrimOrgNameLP extends ColumnLabelProvider {
28  	private final static Log log = LogFactory.getLog(PrimOrgNameLP.class);
29  	private static final long serialVersionUID = 1L;
30  
31  	private PeopleService peopleService;
32  	private String selectorName;
33  
34  	public PrimOrgNameLP(PeopleService peopleService, String selectorName) {
35  		this.peopleService = peopleService;
36  		if (notEmpty(selectorName))
37  			this.selectorName = selectorName;
38  	}
39  
40  	@Override
41  	public String getText(Object element) {
42  		Node currNode = ConnectJcrUtils.getNodeFromElement(element, selectorName);
43  		try {
44  			Node currContact = PeopleJcrUtils.getPrimaryContact(currNode, PeopleTypes.PEOPLE_POSTAL_ADDRESS);
45  			// Sanity check
46  			if (currContact == null || !currContact.isNodeType(PeopleTypes.PEOPLE_CONTACT_REF))
47  				return "";
48  
49  			String refUid = ConnectJcrUtils.get(currContact, PeopleNames.PEOPLE_REF_UID);
50  			if (EclipseUiUtils.isEmpty(refUid)) {
51  				log.error("Empty UID: unable to get linked contact for " + currNode
52  						+ "\nThis usually happens when legacy " + "imported address are not correctly defined");
53  				return null;
54  			}
55  			Node referencedEntity = peopleService.getEntityByUid(ConnectJcrUtils.getSession(currContact), null, refUid);
56  			if (referencedEntity == null)
57  				return "";
58  			return ConnectJcrUtils.get(referencedEntity, Property.JCR_TITLE);
59  		} catch (RepositoryException re) {
60  			throw new PeopleException("Unable to get primary org name from row " + element, re);
61  		}
62  	}
63  }