View Javadoc
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.document.modules.cmis.service;
35  
36  import org.apache.chemistry.opencmis.commons.data.PermissionMapping;
37  import org.apache.chemistry.opencmis.commons.definitions.PermissionDefinition;
38  import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
39  import org.apache.chemistry.opencmis.commons.impl.dataobjects.*;
40  
41  import java.math.BigDecimal;
42  import java.math.BigInteger;
43  
44  import java.util.*;
45  
46  
47  /**
48   * Base Repository - Utils for repositories
49   */
50  public abstract class BaseRepository
51  {
52      /**
53       * Create permission
54       * @param permission The permission
55       * @param description The description
56       * @return
57       */
58      protected static PermissionDefinition createPermission( String permission, String description )
59      {
60          PermissionDefinitionDataImpl pd = new PermissionDefinitionDataImpl(  );
61          pd.setPermission( permission );
62          pd.setDescription( description );
63  
64          return pd;
65      }
66  
67      /**
68       * Create a mapping
69       * @param key The key
70       * @param permission The permission
71       * @return
72       */
73      protected static PermissionMapping createMapping( String key, String permission )
74      {
75          PermissionMappingDataImpl pm = new PermissionMappingDataImpl(  );
76          pm.setKey( key );
77          pm.setPermissions( Collections.singletonList( permission ) );
78  
79          return pm;
80      }
81  
82      /**
83       * Add an Id property
84       * @param props The properties
85       * @param typeId The typeId
86       * @param filter the Filter
87       * @param id The id
88       * @param value The value
89       */
90      protected void addPropertyId( PropertiesImpl props, String typeId, Set<String> filter, String id, String value )
91      {
92          if ( !checkAddProperty( props, typeId, filter, id ) )
93          {
94              return;
95          }
96  
97          props.addProperty( new PropertyIdImpl( id, value ) );
98      }
99  
100     /**
101      * Add an Id list property
102      * @param props The properties
103      * @param typeId The typeId
104      * @param filter the Filter
105      * @param id The id
106      * @param value The value
107      */
108     protected void addPropertyIdList( PropertiesImpl props, String typeId, Set<String> filter, String id,
109         List<String> value )
110     {
111         if ( !checkAddProperty( props, typeId, filter, id ) )
112         {
113             return;
114         }
115 
116         props.addProperty( new PropertyIdImpl( id, value ) );
117     }
118 
119     /**
120      * Add a String property
121      * @param props The properties
122      * @param typeId The typeId
123      * @param filter the Filter
124      * @param id The id
125      * @param value The value
126      */
127     protected void addPropertyString( PropertiesImpl props, String typeId, Set<String> filter, String id, String value )
128     {
129         if ( !checkAddProperty( props, typeId, filter, id ) )
130         {
131             return;
132         }
133 
134         props.addProperty( new PropertyStringImpl( id, value ) );
135     }
136 
137     /**
138      * Add an Integer property
139      * @param props The properties
140      * @param typeId The typeId
141      * @param filter the Filter
142      * @param id The id
143      * @param value The value
144      */
145     protected void addPropertyInteger( PropertiesImpl props, String typeId, Set<String> filter, String id, long value )
146     {
147         addPropertyBigInteger( props, typeId, filter, id, BigInteger.valueOf( value ) );
148     }
149 
150     /**
151      * Add a Big Integer property
152      * @param props The properties
153      * @param typeId The typeId
154      * @param filter the Filter
155      * @param id The id
156      * @param value The value
157      */
158     protected void addPropertyBigInteger( PropertiesImpl props, String typeId, Set<String> filter, String id,
159         BigInteger value )
160     {
161         if ( !checkAddProperty( props, typeId, filter, id ) )
162         {
163             return;
164         }
165 
166         props.addProperty( new PropertyIntegerImpl( id, value ) );
167     }
168 
169     /**
170      * Add a Boolean property
171      * @param props The properties
172      * @param typeId The typeId
173      * @param filter the Filter
174      * @param id The id
175      * @param value The value
176      */
177     protected void addPropertyBoolean( PropertiesImpl props, String typeId, Set<String> filter, String id, boolean value )
178     {
179         if ( !checkAddProperty( props, typeId, filter, id ) )
180         {
181             return;
182         }
183 
184         props.addProperty( new PropertyBooleanImpl( id, value ) );
185     }
186 
187     /**
188      * Add a DateTime property
189      * @param props The properties
190      * @param typeId The typeId
191      * @param filter the Filter
192      * @param id The id
193      * @param value The value
194      */
195     protected void addPropertyDateTime( PropertiesImpl props, String typeId, Set<String> filter, String id,
196         GregorianCalendar value )
197     {
198         if ( !checkAddProperty( props, typeId, filter, id ) )
199         {
200             return;
201         }
202 
203         props.addProperty( new PropertyDateTimeImpl( id, value ) );
204     }
205 
206     /**
207      * Check add property
208      * @param props The properties
209      * @param typeId The typeId
210      * @param filter the Filter
211      * @param id The id
212      * @return
213      */
214     protected boolean checkAddProperty( org.apache.chemistry.opencmis.commons.data.Properties properties,
215         String typeId, Set<String> filter, String id )
216     {
217         if ( ( properties == null ) || ( properties.getProperties(  ) == null ) )
218         {
219             throw new IllegalArgumentException( "Properties must not be null!" );
220         }
221 
222         if ( id == null )
223         {
224             throw new IllegalArgumentException( "Id must not be null!" );
225         }
226 
227         return true;
228     }
229 
230     /**
231      * Adds the default value of property if defined.
232      * @param props
233      * @param propDef
234      * @return
235      */
236     protected static boolean addPropertyDefault( PropertiesImpl props, PropertyDefinition<?> propDef )
237     {
238         if ( ( props == null ) || ( props.getProperties(  ) == null ) )
239         {
240             throw new IllegalArgumentException( "Props must not be null!" );
241         }
242 
243         if ( propDef == null )
244         {
245             return false;
246         }
247 
248         List<?> defaultValue = propDef.getDefaultValue(  );
249 
250         if ( ( defaultValue != null ) && ( !defaultValue.isEmpty(  ) ) )
251         {
252             switch ( propDef.getPropertyType(  ) )
253             {
254                 case BOOLEAN:
255                     props.addProperty( new PropertyBooleanImpl( propDef.getId(  ), (List<Boolean>) defaultValue ) );
256 
257                     break;
258 
259                 case DATETIME:
260                     props.addProperty( new PropertyDateTimeImpl( propDef.getId(  ),
261                             (List<GregorianCalendar>) defaultValue ) );
262 
263                     break;
264 
265                 case DECIMAL:
266                     props.addProperty( new PropertyDecimalImpl( propDef.getId(  ), (List<BigDecimal>) defaultValue ) );
267 
268                     break;
269 
270                 case HTML:
271                     props.addProperty( new PropertyHtmlImpl( propDef.getId(  ), (List<String>) defaultValue ) );
272 
273                     break;
274 
275                 case ID:
276                     props.addProperty( new PropertyIdImpl( propDef.getId(  ), (List<String>) defaultValue ) );
277 
278                     break;
279 
280                 case INTEGER:
281                     props.addProperty( new PropertyIntegerImpl( propDef.getId(  ), (List<BigInteger>) defaultValue ) );
282 
283                     break;
284 
285                 case STRING:
286                     props.addProperty( new PropertyStringImpl( propDef.getId(  ), (List<String>) defaultValue ) );
287 
288                     break;
289 
290                 case URI:
291                     props.addProperty( new PropertyUriImpl( propDef.getId(  ), (List<String>) defaultValue ) );
292 
293                     break;
294 
295                 default:
296                     throw new RuntimeException( "Unknown datatype! Spec change?" );
297             }
298 
299             return true;
300         }
301 
302         return false;
303     }
304 
305     /**
306     * Converts milliseconds into a calendar object.
307     * @param millis
308     * @return
309     */
310     protected static GregorianCalendar millisToCalendar( long millis )
311     {
312         GregorianCalendar result = new GregorianCalendar(  );
313         result.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
314         result.setTimeInMillis( (long) ( Math.ceil( millis / 1000 ) * 1000 ) );
315 
316         return result;
317     }
318 }