001/* 002 * Copyright 2009 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.workspaces.repository; 017 018import java.util.List; 019 020import javax.jcr.Node; 021import javax.jcr.Property; 022import javax.jcr.PropertyType; 023import javax.jcr.RepositoryException; 024import javax.jcr.Value; 025 026import org.apache.commons.lang3.ArrayUtils; 027import org.apache.commons.lang3.StringUtils; 028 029/** 030 * Helper methods to manipulating JCR nodes in JCR repository console 031 */ 032public final class ConsoleHelper 033{ 034 private ConsoleHelper() 035 { 036 // Utility class 037 } 038 039 /** 040 * Helper to set ambiguous object values from a node property 041 * @param node the node 042 * @param name the property name 043 * @param values the property values as String[] or Value[] 044 * @throws RepositoryException if an error occurred 045 */ 046 public static void setProperty(Node node, String name, Object values) throws RepositoryException 047 { 048 Object javaValues = values; 049 if (values instanceof List) 050 { 051 List<?> list = (List<?>) values; 052 if (list.isEmpty()) 053 { 054 javaValues = new Value[0]; 055 } 056 else if (list.get(0) instanceof String) 057 { 058 javaValues = list.toArray(new String[list.size()]); 059 } 060 else if (list.get(0) instanceof Value) 061 { 062 javaValues = list.toArray(new Value[list.size()]); 063 } 064 } 065 _setProperty(node, name, javaValues); 066 } 067 068 private static void _setProperty(Node node, String name, Object values) throws RepositoryException 069 { 070 if (values instanceof String[]) 071 { 072 node.setProperty(name, (String[]) values); 073 } 074 else if (values instanceof Value[]) 075 { 076 node.setProperty(name, (Value[]) values); 077 } 078 else if (values instanceof String) 079 { 080 node.setProperty(name, (String) values); 081 } 082 else if (values instanceof Boolean) 083 { 084 node.setProperty(name, (Boolean) values); 085 } 086 else if (values instanceof Long) 087 { 088 node.setProperty(name, (Long) values); 089 } 090 else if (values instanceof Integer) 091 { 092 node.setProperty(name, ((Integer) values).longValue()); 093 } 094 else if (values instanceof Double) 095 { 096 node.setProperty(name, (Double) values); 097 } 098 else if (values instanceof Float) 099 { 100 node.setProperty(name, ((Float) values).doubleValue()); 101 } 102 else 103 { 104 Class< ? > clazz = values.getClass(); 105 throw new IllegalArgumentException("argument values (" + values + ") is of unsupported type by ConsoleHelper#setProperty: '" + clazz + "'"); 106 } 107 } 108 109 /** 110 * Helper to convert a single-valued property to a multi-valued property. 111 * This helper checks that property exists and that it is not already multiple. 112 * @param node the node holding the property 113 * @param propertyName the property's name 114 * @return true if changes was made 115 * @throws RepositoryException if an error occurred 116 */ 117 public static boolean convertSingleToMultipleProperty (Node node, String propertyName) throws RepositoryException 118 { 119 boolean needSave = false; 120 if (node.hasProperty(propertyName)) 121 { 122 Property property = node.getProperty(propertyName); 123 if (!property.getDefinition().isMultiple()) 124 { 125 Value value = property.getValue(); 126 if (value.getType() == PropertyType.STRING) 127 { 128 String valueAsStr = value.getString(); 129 property.remove(); 130 131 if (!StringUtils.isEmpty(valueAsStr)) 132 { 133 String[] strArray = value.getString().split(","); 134 node.setProperty(propertyName, strArray); 135 } 136 else 137 { 138 node.setProperty(propertyName, new String[0]); 139 } 140 } 141 else 142 { 143 Value[] values = ArrayUtils.toArray(value); 144 node.setProperty(propertyName, values); 145 } 146 147 needSave = true; 148 } 149 } 150 151 return needSave; 152 } 153}