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 javax.jcr.Node; 019import javax.jcr.Property; 020import javax.jcr.PropertyType; 021import javax.jcr.RepositoryException; 022import javax.jcr.Value; 023 024import org.apache.commons.lang3.ArrayUtils; 025import org.apache.commons.lang3.StringUtils; 026 027/** 028 * Helper methods to manipulating JCR nodes in JCR repository console 029 */ 030public final class ConsoleHelper 031{ 032 private ConsoleHelper() 033 { 034 // Utility class 035 } 036 037 /** 038 * Helper to set ambiguous object values from a node property 039 * @param node the node 040 * @param name the property name 041 * @param values the property values as String[] or Value[] 042 * @throws RepositoryException if an error occurred 043 */ 044 public static void setProperty (Node node, String name, Object values) throws RepositoryException 045 { 046 if (values instanceof String[]) 047 { 048 node.setProperty(name, (String[]) values); 049 } 050 else if (values instanceof Value[]) 051 { 052 node.setProperty(name, (Value[]) values); 053 } 054 else if (values instanceof Long) 055 { 056 node.setProperty(name, ((Long) values).longValue()); 057 } 058 else if (values instanceof Double) 059 { 060 node.setProperty(name, ((Double) values).doubleValue()); 061 } 062 else 063 { 064 System.out.println(values.getClass().toString() + " is not recognized"); 065 node.setProperty(name, (String[]) values); 066 } 067 } 068 069 /** 070 * Helper to convert a single-valued property to a multi-valued property. 071 * This helper checks that property exists and that it is not already multiple. 072 * @param node the node holding the property 073 * @param propertyName the property's name 074 * @return true if changes was made 075 * @throws RepositoryException if an error occurred 076 */ 077 public static boolean convertSingleToMultipleProperty (Node node, String propertyName) throws RepositoryException 078 { 079 boolean needSave = false; 080 if (node.hasProperty(propertyName)) 081 { 082 Property property = node.getProperty(propertyName); 083 if (!property.getDefinition().isMultiple()) 084 { 085 Value value = property.getValue(); 086 if (value.getType() == PropertyType.STRING) 087 { 088 String valueAsStr = value.getString(); 089 property.remove(); 090 091 if (!StringUtils.isEmpty(valueAsStr)) 092 { 093 String[] strArray = value.getString().split(","); 094 node.setProperty(propertyName, strArray); 095 } 096 else 097 { 098 node.setProperty(propertyName, new String[0]); 099 } 100 } 101 else 102 { 103 Value[] values = ArrayUtils.toArray(value); 104 node.setProperty(propertyName, values); 105 } 106 107 needSave = true; 108 } 109 } 110 111 return needSave; 112 } 113}