View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.slc.client.ui.dist.utils;
17  
18  import java.text.DateFormat;
19  import java.text.SimpleDateFormat;
20  import java.util.Calendar;
21  
22  import javax.jcr.PropertyType;
23  import javax.jcr.RepositoryException;
24  import javax.jcr.Value;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.argeo.slc.SlcException;
29  import org.argeo.slc.SlcNames;
30  import org.argeo.slc.SlcTypes;
31  import org.argeo.slc.client.ui.dist.DistConstants;
32  
33  public class DistUiHelpers implements DistConstants, SlcTypes, SlcNames {
34  	private final static Log log = LogFactory.getLog(DistUiHelpers.class);
35  	private final static DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT);
36  
37  	/**
38  	 * Returns a user-friendly label for a given jcr property name. If the
39  	 * corresponding mapping is not found, the input String is returned. If
40  	 * input String is null "(No name)" is returned
41  	 */
42  	public static String getLabelJcrName(String jcrName) {
43  		return (String) getLabelAndDefaultValueWidth(jcrName)[0];
44  	}
45  
46  	/**
47  	 * Returns a label ( (String) object[0] )and default value width ( (int)
48  	 * object[1] ) for a given property name
49  	 */
50  	public static Object[] getLabelAndDefaultValueWidth(String propertyName) {
51  		// to avoid npe :
52  		if (propertyName == null)
53  			return new Object[] { "(No name)", 60 };
54  
55  		// ArtifactId
56  		if (propertyName.equals(SLC_ARTIFACT + "." + SLC_ARTIFACT_ID)
57  				|| propertyName.equals(SLC_ARTIFACT_BASE + "."
58  						+ SLC_ARTIFACT_ID)
59  				|| propertyName.equals(SLC_ARTIFACT_VERSION_BASE + "."
60  						+ SLC_ARTIFACT_ID)
61  				|| propertyName.equals(SLC_ARTIFACT_ID)) {
62  			return new Object[] { "Artifact ID", 200 };
63  		} // GroupId
64  		else if (propertyName.equals(SLC_ARTIFACT + "." + SLC_GROUP_ID)
65  				|| propertyName.equals(SLC_ARTIFACT_BASE + "." + SLC_GROUP_ID)
66  				|| propertyName.equals(SLC_ARTIFACT_VERSION_BASE + "."
67  						+ SLC_GROUP_ID) || propertyName.equals(SLC_GROUP_ID)) {
68  			return new Object[] { "Group ID", 120 };
69  		} // Version
70  		else if (propertyName.equals(SLC_ARTIFACT + "." + SLC_ARTIFACT_VERSION)
71  				|| propertyName.equals(SLC_ARTIFACT_VERSION_BASE + "."
72  						+ SLC_ARTIFACT_VERSION)
73  				|| propertyName.equals(SLC_ARTIFACT_VERSION)) {
74  			return new Object[] { "Version", 60 };
75  		} else if (propertyName.equals(SLC_ARTIFACT + "."
76  				+ SLC_ARTIFACT_CLASSIFIER)
77  				|| propertyName.equals(SLC_ARTIFACT_CLASSIFIER)) {
78  			return new Object[] { "Classifier", 60 };
79  		} else if (propertyName.equals(SLC_ARTIFACT + "."
80  				+ SLC_ARTIFACT_EXTENSION)
81  				|| propertyName.equals(SLC_ARTIFACT_EXTENSION)) {
82  			return new Object[] { "Type", 40 };
83  		} else if (propertyName.equals(SLC_BUNDLE_ARTIFACT + "."
84  				+ SLC_SYMBOLIC_NAME)
85  				|| propertyName.equals(SLC_SYMBOLIC_NAME)) {
86  			return new Object[] { "Symbolic name", 180 };
87  		} else if (propertyName.equals(SLC_BUNDLE_ARTIFACT + "."
88  				+ SLC_BUNDLE_VERSION)
89  				|| propertyName.equals(SLC_BUNDLE_VERSION)) {
90  			return new Object[] { "Bundle version", 120 };
91  		} else if (propertyName
92  				.equals(SLC_BUNDLE_ARTIFACT + "." + SLC_MANIFEST)
93  				|| propertyName.equals(SLC_MANIFEST)) {
94  			return new Object[] { "Manifest", 60 };
95  		} // TODO remove hard coded strings
96  		else if (propertyName.equals("slc:Bundle-ManifestVersion")) {
97  			return new Object[] { "Bundle Manifest Version", 60 };
98  		} else if (propertyName.equals("slc:Manifest-Version")) {
99  			return new Object[] { "Manifest Version", 60 };
100 		} else if (propertyName.equals("slc:Bundle-Vendor")) {
101 			return new Object[] { "Bundle Vendor", 60 };
102 		} else if (propertyName.equals("slc:Bundle-SymbolicName")) {
103 			return new Object[] { "Bundle symbolic name", 60 };
104 		} else if (propertyName.equals("slc:Bundle-Name")) {
105 			return new Object[] { "Bundle name", 60 };
106 		} else if (propertyName.equals("slc:Bundle-DocURL")) {
107 			return new Object[] { "Doc URL", 120 };
108 		} else if (propertyName.equals("slc:Bundle-Licence")) {
109 			return new Object[] { "Bundle licence", 120 };
110 		} else if (propertyName.equals(SLC_ARTIFACT_VERSION_BASE + "."
111 				+ JCR_IDENTIFIER)) {
112 			return new Object[] { "UUID", 0 };
113 		} else {
114 			if (log.isTraceEnabled())
115 				log.trace("No Column label provider defined for property: ["
116 						+ propertyName + "]");
117 			return new Object[] { propertyName, 60 };
118 		}
119 	}
120 
121 	public static String formatValueAsString(Value value) {
122 		try {
123 			String strValue;
124 
125 			if (value.getType() == PropertyType.BINARY)
126 				strValue = "<binary>";
127 			else if (value.getType() == PropertyType.DATE)
128 				strValue = df.format(value.getDate().getTime());
129 			else
130 				strValue = value.getString();
131 			return strValue;
132 		} catch (RepositoryException e) {
133 			throw new SlcException("unexpected error while formatting value",
134 					e);
135 		}
136 	}
137 
138 	public static String formatAsString(Object value) {
139 		String strValue;
140 		if (value instanceof Calendar)
141 			strValue = df.format(((Calendar) value).getTime());
142 		else
143 			strValue = value.toString();
144 		return strValue;
145 	}
146 }