1 /* 2 * Copyright (c) 2002-2014, Mairie de Paris 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice 10 * and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice 13 * and the following disclaimer in the documentation and/or other materials 14 * provided with the distribution. 15 * 16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 * 32 * License 1.0 33 */ 34 package fr.paris.lutece.plugins.crm.util; 35 36 /* 37 * Copyright (c) 2002-2014, Mairie de Paris 38 * All rights reserved. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 44 * 1. Redistributions of source code must retain the above copyright notice 45 * and the following disclaimer. 46 * 47 * 2. Redistributions in binary form must reproduce the above copyright notice 48 * and the following disclaimer in the documentation and/or other materials 49 * provided with the distribution. 50 * 51 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its 52 * contributors may be used to endorse or promote products derived from 53 * this software without specific prior written permission. 54 * 55 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 56 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE 59 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 60 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 61 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 62 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 63 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 64 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 65 * POSSIBILITY OF SUCH DAMAGE. 66 * 67 * License 1.0 68 */ 69 import fr.paris.lutece.portal.service.util.AppPropertiesService; 70 import fr.paris.lutece.util.ReferenceList; 71 72 import org.apache.commons.beanutils.BeanUtils; 73 74 import org.apache.log4j.Logger; 75 76 import java.lang.reflect.InvocationTargetException; 77 78 import java.util.Arrays; 79 import java.util.List; 80 81 /** 82 * Utility to manipulate lists and referenceLists 83 * 84 */ 85 public class ListUtils 86 { 87 private static final String PROPERTY_LIST_SEPARATOR = ";"; 88 private static final Logger LOGGER = Logger.getLogger( ListUtils.class ); 89 90 /** 91 * 92 * Transforms a basical list to a reference list 93 * 94 * @param list 95 * @param key 96 * @param value 97 * @param firstItem 98 * @return a reference list 99 */ 100 public static ReferenceList toReferenceList( List<?> list, String key, String value, String firstItem ) 101 { 102 ReferenceList referenceList = new ReferenceList( ); 103 String valeurKey; 104 String valeurValue; 105 106 try 107 { 108 if ( firstItem != null ) 109 { 110 referenceList.addItem( "-1", firstItem ); 111 } 112 113 for ( Object element : list ) 114 { 115 valeurKey = BeanUtils.getSimpleProperty( element, key ); 116 valeurValue = BeanUtils.getSimpleProperty( element, value ); 117 referenceList.addItem( valeurKey, valeurValue ); 118 } 119 } 120 catch( IllegalAccessException e ) 121 { 122 LOGGER.warn( "Erreur lors de la création d'une liste pour combo : " + e.getMessage( ), e ); 123 } 124 catch( InvocationTargetException e ) 125 { 126 LOGGER.warn( "Erreur lors de la création d'une liste pour combo : " + e.getMessage( ), e ); 127 } 128 catch( NoSuchMethodException e ) 129 { 130 LOGGER.warn( "Erreur lors de la création d'une liste pour combo : " + e.getMessage( ), e ); 131 } 132 catch( Exception e ) 133 { 134 LOGGER.warn( "Erreur lors de la création d'une liste pour combo : " + e.getMessage( ), e ); 135 } 136 137 return referenceList; 138 } 139 140 /** 141 * Get the property list 142 * 143 * @param propertyKey 144 * @return a list of property 145 */ 146 public static List<String> getPropertyList( String propertyKey ) 147 { 148 String property = AppPropertiesService.getProperty( propertyKey ); 149 150 if ( property != null ) 151 { 152 String [ ] items = property.split( PROPERTY_LIST_SEPARATOR ); 153 154 if ( items != null ) 155 { 156 return Arrays.asList( items ); 157 } 158 } 159 160 return null; 161 } 162 }