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.RepositoryException;
7   
8   import org.argeo.connect.util.ConnectJcrUtils;
9   import org.argeo.connect.util.DateTimeUtils;
10  import org.argeo.eclipse.ui.jcr.lists.SimpleJcrNodeLabelProvider;
11  import org.argeo.people.PeopleException;
12  
13  /**
14   * Provides a length given a row that contains a selector that has a length in
15   * seconds LONG property and format it according to one of the pre-defined
16   * format
17   */
18  public class LengthLP extends SimpleJcrNodeLabelProvider {
19  	private static final long serialVersionUID = 1L;
20  
21  	private String selectorName;
22  	private String propertyName;
23  
24  	public final static String IN_MINUTES = "MM";
25  	public final static String HH_MM_SS = "HHMMSS";
26  	private String format;
27  
28  	public LengthLP(String selectorName, String propertyName, String format) {
29  		super(propertyName);
30  		if (notEmpty(selectorName))
31  			this.selectorName = selectorName;
32  		this.propertyName = propertyName;
33  		this.format = format;
34  	}
35  
36  	@Override
37  	public String getText(Object element) {
38  		try {
39  			Node currNode = ConnectJcrUtils.getNodeFromElement(element, selectorName);
40  			String result = "";
41  			if (currNode != null && currNode.hasProperty(propertyName)) {
42  				long length = currNode.getProperty(propertyName).getLong();
43  				if (HH_MM_SS.equals(format)) {
44  					result = String.format("%d:%02d:%02d", length / 3600, (length % 3600) / 60, (length % 60));
45  				} else if (IN_MINUTES.equals(format))
46  					result = "" + DateTimeUtils.roundSecondsToMinutes(length);
47  			}
48  			return result;
49  		} catch (RepositoryException re) {
50  			throw new PeopleException("Unable to get selector " + selectorName + " from row " + element, re);
51  		}
52  	}
53  }