This project has retired. For details please refer to its
Attic page.
PropertyHelper xref
1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing,13 * software distributed under the License is distributed on an14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15 * KIND, either express or implied. See the License for the16 * specific language governing permissions and limitations17 * under the License.18 */1920package org.apache.chemistry.opencmis.jcr;
2122import org.apache.chemistry.opencmis.commons.PropertyIds;
23import org.apache.chemistry.opencmis.commons.data.Properties;
24import org.apache.chemistry.opencmis.commons.data.PropertyData;
25import org.apache.chemistry.opencmis.commons.data.PropertyId;
26import org.apache.chemistry.opencmis.commons.data.PropertyString;
27import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
28import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
29import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
30import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyBooleanImpl;
31import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyDateTimeImpl;
32import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyDecimalImpl;
33import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyHtmlImpl;
34import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyIdImpl;
35import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyIntegerImpl;
36import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyStringImpl;
37import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyUriImpl;
3839import java.math.BigDecimal;
40import java.math.BigInteger;
41import java.util.GregorianCalendar;
42import java.util.List;
4344/**45 * Helper class with utility functions for handling {@link Properties}.46 */47publicfinalclassPropertyHelper {
48privatePropertyHelper() {
49 }
5051/**52 * Retrieve a string value.53 *54 * @param properties55 * @param name the name of the value to retrieve56 * @return the first value of the given <code>name</code> or <code>null</code> if either57 * these are no string properties or no property of <code>name</code> exists. 58 */59publicstatic String getStringProperty(Properties properties, String name) {
60 PropertyData<?> property = properties.getProperties().get(name);
61if (!(property instanceof PropertyString)) {
62returnnull;
63 }
6465return ((PropertyString) property).getFirstValue();
66 }
6768/**69 * Gets the type id from a set of properties.70 */71publicstatic String getTypeId(Properties properties) {
72 PropertyData<?> typeProperty = properties.getProperties().get(PropertyIds.OBJECT_TYPE_ID);
73if (!(typeProperty instanceof PropertyId)) {
74thrownew CmisInvalidArgumentException("Type id must be set!");
75 }
7677 String typeId = ((PropertyId) typeProperty).getFirstValue();
78if (typeId == null) {
79thrownew CmisInvalidArgumentException("Type id must be set!");
80 }
8182return typeId;
83 }
8485/**86 * @param prop87 * @return <code>true</code> iff <code>prop</code> denotes an empty property data value88 */89publicstaticboolean isPropertyEmpty(PropertyData<?> prop) {
90return prop == null || prop.getValues() == null || prop.getValues().isEmpty();
91 }
9293/**94 * Determine the default property data value for a given property definition.95 * @param propDef96 * @return97 * @throws CmisRuntimeException if <code>propDef</code> is invalid or unknown.98 */99 @SuppressWarnings("unchecked")
100publicstatic PropertyData<?> getDefaultValue(PropertyDefinition<?> propDef) {
101if (propDef == null) {
102returnnull;
103 }
104105 List<?> defaultValue = propDef.getDefaultValue();
106if (defaultValue != null && !defaultValue.isEmpty()) {
107switch (propDef.getPropertyType()) {
108case BOOLEAN:
109returnnew PropertyBooleanImpl(propDef.getId(), (List<Boolean>) defaultValue);
110case DATETIME:
111returnnew PropertyDateTimeImpl(propDef.getId(), (List<GregorianCalendar>) defaultValue);
112case DECIMAL:
113returnnew PropertyDecimalImpl(propDef.getId(), (List<BigDecimal>) defaultValue);
114case HTML:
115returnnew PropertyHtmlImpl(propDef.getId(), (List<String>) defaultValue);
116case ID:
117returnnew PropertyIdImpl(propDef.getId(), (List<String>) defaultValue);
118case INTEGER:
119returnnew PropertyIntegerImpl(propDef.getId(), (List<BigInteger>) defaultValue);
120case STRING:
121returnnew PropertyStringImpl(propDef.getId(), (List<String>) defaultValue);
122case URI:
123returnnew PropertyUriImpl(propDef.getId(), (List<String>) defaultValue);
124default:
125thrownew CmisRuntimeException("Unknown datatype: " + propDef.getPropertyType());
126 }
127 }
128returnnull;
129 }
130131 }