View Javadoc
1   /*
2    * Copyright (c) 2002-2025, City of 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.pluginwizard.service;
35  
36  import fr.paris.lutece.plugins.pluginwizard.business.Model;
37  import fr.paris.lutece.plugins.pluginwizard.business.ModelHome;
38  import fr.paris.lutece.plugins.pluginwizard.business.model.Application;
39  import fr.paris.lutece.plugins.pluginwizard.business.model.Attribute;
40  import fr.paris.lutece.plugins.pluginwizard.business.model.BusinessClass;
41  import fr.paris.lutece.plugins.pluginwizard.business.model.Feature;
42  import fr.paris.lutece.plugins.pluginwizard.business.model.PluginModel;
43  import fr.paris.lutece.plugins.pluginwizard.business.model.Portlet;
44  import fr.paris.lutece.plugins.pluginwizard.business.model.Rest;
45  import fr.paris.lutece.plugins.pluginwizard.web.formbean.BusinessClassFormBean;
46  import fr.paris.lutece.plugins.pluginwizard.web.formbean.ConfigurationFormBean;
47  import fr.paris.lutece.plugins.pluginwizard.web.formbean.DescriptionFormBean;
48  import fr.paris.lutece.portal.service.spring.SpringContextService;
49  import fr.paris.lutece.portal.service.util.AppException;
50  import fr.paris.lutece.util.ReferenceList;
51  
52  import com.fasterxml.jackson.databind.DeserializationFeature;
53  import com.fasterxml.jackson.databind.ObjectMapper;
54  
55  import java.io.IOException;
56  import java.lang.reflect.InvocationTargetException;
57  import java.util.ArrayList;
58  import java.util.List;
59  import java.util.Locale;
60  import org.apache.commons.beanutils.BeanUtils;
61  
62  /**
63   * Model Service provides all plugin'model manipulations
64   */
65  public final class ModelService
66  {
67      private static AttributeService _serviceAttribute = SpringContextService.getBean( "pluginwizard.attribute.service" );
68      private static ObjectMapper _mapper = new ObjectMapper( ).configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
69      private static final String ID = "id";
70      private static final String UNDERSCORE = "_";
71  
72      /** private constructor */
73      private ModelService( )
74      {
75      }
76  
77      /**
78       * Create a plugin model
79       * 
80       * @param strPluginName
81       *            The plugin name
82       * @return The plugin ID
83       */
84      public static int createModel( String strPluginName )
85      {
86          Modelins/pluginwizard/business/Model.html#Model">Model model = new Model( );
87          model.setName( strPluginName );
88  
89          PluginModel/pluginwizard/business/model/PluginModel.html#PluginModel">PluginModel pm = new PluginModel( );
90          pm.setPluginName( strPluginName );
91  
92          model = ModelHome.create( model );
93          pm.setIdPlugin( model.getIdPlugin( ) );
94          savePluginModel( pm );
95  
96          return pm.getIdPlugin( );
97      }
98  
99      /**
100      * Returns the plugin model
101      * 
102      * @param nPluginId
103      *            The plugin's ID
104      * @return The plugin model
105      */
106     public static PluginModel getPluginModel( int nPluginId )
107     {
108         PluginModel pm;
109         Model model = ModelHome.findByPrimaryKey( nPluginId );
110 
111         if ( model != null )
112         {
113             pm = MapperService.readJson( model.getModelJson( ) );
114         }
115         else
116         {
117             pm = new PluginModel( );
118             pm.setIdPlugin( nPluginId );
119         }
120 
121         return pm;
122     }
123 
124     /**
125      * Save the plugin model
126      * 
127      * @param pm
128      *            The plugin model
129      */
130     public static void savePluginModel( PluginModel pm )
131     {
132         Modelins/pluginwizard/business/Model.html#Model">Model model = new Model( );
133         model.setIdPlugin( pm.getIdPlugin( ) );
134         model.setName( pm.getPluginName( ) );
135 
136         String strJson = MapperService.getJson( pm );
137         model.setModelJson( strJson );
138 
139         if ( ModelHome.findByPrimaryKey( pm.getIdPlugin( ) ) != null )
140         {
141             ModelHome.update( model );
142         }
143         else
144         {
145             ModelHome.create( model );
146         }
147     }
148 
149     /**
150      * Save the plugin model from json
151      * 
152      * @param pm
153      *            The plugin model
154      * @return
155      */
156     public static int savePluginModelFromJson( PluginModel pm )
157     {
158         Model model;
159         int nPluginId = ModelHome.exists( pm.getPluginName( ) );
160 
161         if ( nPluginId == -1 )
162         {
163             // if the plugin doesn't exist
164             model = new Model( );
165             model.setName( pm.getPluginName( ) );
166 
167             ModelHome.create( model );
168         }
169         else
170         {
171             model = ModelHome.findByPrimaryKey( nPluginId );
172         }
173 
174         pm.setIdPlugin( model.getIdPlugin( ) );
175         String strJson = MapperService.getJson( pm );
176         model.setModelJson( strJson );
177         ModelHome.update( model );
178 
179         return model.getIdPlugin( );
180     }
181 
182     // //////////////////////////////////////////////////////////////////////////
183     // FEATURES
184     /**
185      * Add a feature
186      *
187      * @param nPluginId
188      *            The plugin's ID
189      * @param feature
190      *            The feature
191      */
192     public static void addFeature( int nPluginId, Feature feature )
193     {
194         PluginModel pm = getPluginModel( nPluginId );
195         feature.setId( getMaxFeatureId( pm ) + 1 );
196         List<Feature> list = pm.getFeatures( );
197         list.add( feature );
198         pm.setFeatures( list );
199 
200         savePluginModel( pm );
201     }
202 
203     /**
204      * Get a given feature
205      *
206      * @param nPluginId
207      *            The plugin's ID
208      * @param nFeatureId
209      *            The feature's ID
210      * @return The feature
211      */
212     public static Feature getFeature( int nPluginId, int nFeatureId )
213     {
214         PluginModel pm = getPluginModel( nPluginId );
215 
216         for ( Feature feature : pm.getFeatures( ) )
217         {
218             if ( feature.getId( ) == nFeatureId )
219             {
220                 return feature;
221             }
222         }
223 
224         return null;
225     }
226 
227     /**
228      * Get The max feature ID
229      *
230      * @param pm
231      *            The Plugin Model
232      * @return The max used ID
233      */
234     private static int getMaxFeatureId( PluginModel pm )
235     {
236         int nMax = 0;
237 
238         for ( Feature feature : pm.getFeatures( ) )
239         {
240             if ( feature.getId( ) > nMax )
241             {
242                 nMax = feature.getId( );
243             }
244         }
245 
246         return nMax;
247     }
248 
249     /**
250      * Update a feature
251      *
252      * @param nPluginId
253      *            The plugin's ID
254      * @param feature
255      *            The feature
256      */
257     public static void updateFeature( int nPluginId, Feature feature )
258     {
259         PluginModel pm = getPluginModel( nPluginId );
260         List<Feature> list = pm.getFeatures( );
261 
262         for ( int i = 0; i < list.size( ); i++ )
263         {
264             Feature f = list.get( i );
265 
266             if ( f.getId( ) == feature.getId( ) )
267             {
268                 list.set( i, feature );
269                 pm.setFeatures( list );
270 
271                 savePluginModel( pm );
272 
273                 break;
274             }
275         }
276     }
277 
278     /**
279      * Remove a feature
280      *
281      * @param nPluginId
282      *            The plugin's ID
283      * @param nFeatureId
284      *            The feature's ID
285      */
286     public static void removeFeature( int nPluginId, int nFeatureId )
287     {
288         PluginModel pm = getPluginModel( nPluginId );
289         List<Feature> list = pm.getFeatures( );
290 
291         for ( int i = 0; i < list.size( ); i++ )
292         {
293             Feature f = list.get( i );
294 
295             if ( f.getId( ) == nFeatureId )
296             {
297                 list.remove( i );
298                 pm.setFeatures( list );
299 
300                 savePluginModel( pm );
301 
302                 break;
303             }
304         }
305     }
306 
307     // //////////////////////////////////////////////////////////////////////////
308     // APPLICATIONS
309 
310     /**
311      * Get a given application
312      *
313      * @param nPluginId
314      *            The plugin's ID
315      * @param nApplicationId
316      *            The application ID
317      * @return The application
318      */
319     public static Application getApplication( int nPluginId, int nApplicationId )
320     {
321         PluginModel pm = getPluginModel( nPluginId );
322 
323         return getApplication( pm, nApplicationId );
324     }
325 
326     /**
327      * Get a given application
328      *
329      * @param pm
330      *            The plugin model
331      * @param nApplicationId
332      *            The application ID
333      * @return The application
334      */
335     public static Application getApplication( PluginModel pm, int nApplicationId )
336     {
337         for ( Application application : pm.getApplications( ) )
338         {
339             if ( application.getId( ) == nApplicationId )
340             {
341                 return application;
342             }
343         }
344 
345         return null;
346     }
347 
348     /**
349      * Add an application to the model
350      *
351      * @param nPluginId
352      *            The plugin's ID
353      * @param application
354      *            The application
355      */
356     public static void addApplication( int nPluginId, Application application )
357     {
358         PluginModel pm = getPluginModel( nPluginId );
359         application.setId( getMaxApplicationId( pm ) + 1 );
360         List<Application> list = pm.getApplications( );
361         list.add( application );
362         pm.setApplications( list );
363 
364         savePluginModel( pm );
365     }
366 
367     /**
368      * Get The max application ID
369      *
370      * @param pm
371      *            The Plugin Model
372      * @return The max used ID
373      */
374     private static int getMaxApplicationId( PluginModel pm )
375     {
376         int nMax = 0;
377 
378         for ( Application application : pm.getApplications( ) )
379         {
380             if ( application.getId( ) > nMax )
381             {
382                 nMax = application.getId( );
383             }
384         }
385 
386         return nMax;
387     }
388 
389     /**
390      * Update an application
391      *
392      * @param nPluginId
393      *            The plugin's ID
394      * @param application
395      *            The application
396      */
397     public static void updateApplication( int nPluginId, Application application )
398     {
399         PluginModel pm = getPluginModel( nPluginId );
400         List<Application> list = pm.getApplications( );
401 
402         for ( int i = 0; i < list.size( ); i++ )
403         {
404             Application app = list.get( i );
405 
406             if ( app.getId( ) == application.getId( ) )
407             {
408                 list.set( i, application );
409                 pm.setApplications( list );
410 
411                 savePluginModel( pm );
412 
413                 break;
414             }
415         }
416     }
417 
418     /**
419      * Remove an application
420      *
421      * @param nPluginId
422      *            The plugin's ID
423      * @param nApplicationId
424      *            The application's ID
425      */
426     public static void removeApplication( int nPluginId, int nApplicationId )
427     {
428         PluginModel pm = getPluginModel( nPluginId );
429         List<Application> list = pm.getApplications( );
430 
431         for ( int i = 0; i < list.size( ); i++ )
432         {
433             Application f = list.get( i );
434 
435             if ( f.getId( ) == nApplicationId )
436             {
437                 list.remove( i );
438                 pm.setApplications( list );
439 
440                 savePluginModel( pm );
441 
442                 break;
443             }
444         }
445     }
446 
447     // //////////////////////////////////////////////////////////////////////////
448     // PORTLET
449 
450     /**
451      * Get a given portlet
452      *
453      * @param nPluginId
454      *            The plugin's ID
455      * @param nPortletId
456      *            The portlet ID
457      * @return The portlet
458      */
459     public static Portlet getPortlet( int nPluginId, int nPortletId )
460     {
461         PluginModel pm = getPluginModel( nPluginId );
462 
463         for ( Portlet portlet : pm.getPortlets( ) )
464         {
465             if ( portlet.getId( ) == nPortletId )
466             {
467                 return portlet;
468             }
469         }
470 
471         return null;
472     }
473 
474     /**
475      * Add an portlet to the model
476      *
477      * @param nPluginId
478      *            The plugin's ID
479      * @param portlet
480      *            The portlet
481      */
482     public static void addPortlet( int nPluginId, Portlet portlet )
483     {
484         PluginModel pm = getPluginModel( nPluginId );
485         portlet.setId( getMaxPortletId( pm ) + 1 );
486         List<Portlet> portletList = pm.getPortlets( );
487         portletList.add( portlet );
488         pm.setPortlets( portletList );
489         savePluginModel( pm );
490     }
491 
492     /**
493      * Get The max portlet ID
494      *
495      * @param pm
496      *            The Plugin Model
497      * @return The max used ID
498      */
499     private static int getMaxPortletId( PluginModel pm )
500     {
501         int nMax = 0;
502 
503         for ( Portlet portlet : pm.getPortlets( ) )
504         {
505             if ( portlet.getId( ) > nMax )
506             {
507                 nMax = portlet.getId( );
508             }
509         }
510 
511         return nMax;
512     }
513 
514     /**
515      * Update an portlet
516      *
517      * @param nPluginId
518      *            The plugin's ID
519      * @param portlet
520      *            The portlet
521      */
522     public static void updatePortlet( int nPluginId, Portlet portlet )
523     {
524         PluginModel pm = getPluginModel( nPluginId );
525         List<Portlet> list = pm.getPortlets( );
526 
527         for ( int i = 0; i < list.size( ); i++ )
528         {
529             Portlet p = list.get( i );
530 
531             if ( p.getId( ) == portlet.getId( ) )
532             {
533                 list.set( i, portlet );
534                 pm.setPortlets( list );
535 
536                 savePluginModel( pm );
537 
538                 break;
539             }
540         }
541     }
542 
543     /**
544      * Remove an portlet
545      *
546      * @param nPluginId
547      *            The plugin's ID
548      * @param nPortletId
549      *            The portlet's ID
550      */
551     public static void removePortlet( int nPluginId, int nPortletId )
552     {
553         PluginModel pm = getPluginModel( nPluginId );
554         List<Portlet> list = pm.getPortlets( );
555 
556         for ( int i = 0; i < list.size( ); i++ )
557         {
558             Portlet p = list.get( i );
559 
560             if ( p.getId( ) == nPortletId )
561             {
562                 list.remove( i );
563                 pm.setPortlets( list );
564                 savePluginModel( pm );
565 
566                 break;
567             }
568         }
569     }
570 
571     // //////////////////////////////////////////////////////////////////////////
572     // BUSINESS CLASSES
573     /**
574      * Get a given business class
575      * 
576      * @param nPluginId
577      *            The plugin's ID
578      * @param nBusinessClassId
579      *            The business class ID
580      * @return The business class
581      */
582     public static BusinessClass getBusinessClass( int nPluginId, int nBusinessClassId )
583     {
584         PluginModel pm = getPluginModel( nPluginId );
585 
586         return getBusinessClass( pm, nBusinessClassId );
587     }
588 
589     /**
590      * Get a given business class
591      * 
592      * @param pm
593      *            The plugin model
594      * @param nBusinessClassId
595      *            The business class ID
596      * @return The business class
597      */
598     public static BusinessClass getBusinessClass( PluginModel pm, int nBusinessClassId )
599     {
600         for ( BusinessClass bc : pm.getBusinessClasses( ) )
601         {
602             if ( bc.getId( ) == nBusinessClassId )
603             {
604                 return bc;
605             }
606         }
607 
608         return null;
609     }
610 
611     /**
612      * Add an bc to the model
613      *
614      * @param nPluginId
615      *            The plugin's ID
616      * @param bc
617      *            The business class
618      * @return The business class with its ID
619      */
620     public static BusinessClass addBusinessClass( int nPluginId, BusinessClassFormBean bc )
621     {
622         PluginModel pm = getPluginModel( nPluginId );
623 
624         BusinessClass businessClass;
625 
626         try
627         {
628             businessClass = _mapper.readValue( _mapper.writeValueAsString( bc ), BusinessClass.class );
629         }
630         catch( IOException e )
631         {
632             throw new AppException( "Mapping exception", e );
633         }
634 
635         List<BusinessClass> businessClassesList;
636         businessClass.setId( getMaxBusinessClassId( pm ) + 1 );
637 
638         String strBusinessClass = "";
639         char charBusinessClass[] = bc.getBusinessClass( ).toCharArray( );
640         for ( int i = 0; i < charBusinessClass.length; i++ )
641         {
642             if ( Character.isUpperCase( charBusinessClass [i] ) )
643             {
644                 strBusinessClass += UNDERSCORE;
645             }
646             strBusinessClass += Character.toLowerCase( charBusinessClass [i] );
647         }
648         businessClass.setPrimaryKey( ID + strBusinessClass );
649 
650         businessClassesList = pm.getBusinessClasses( );
651         businessClassesList.add( businessClass );
652         pm.setBusinessClasses( businessClassesList );
653         savePluginModel( pm );
654 
655         return businessClass;
656     }
657 
658     /**
659      * Get The max bc ID
660      *
661      * @param pm
662      *            The Plugin Model
663      * @return The max used ID
664      */
665     private static int getMaxBusinessClassId( PluginModel pm )
666     {
667         int nMax = 0;
668 
669         for ( BusinessClass bc : pm.getBusinessClasses( ) )
670         {
671             if ( bc.getId( ) > nMax )
672             {
673                 nMax = bc.getId( );
674             }
675         }
676 
677         return nMax;
678     }
679 
680     /**
681      * Update an businessClass
682      *
683      * @param nPluginId
684      *            The plugin's ID
685      * @param businessClass
686      *            The businessClass
687      */
688     public static void updateBusinessClass( int nPluginId, BusinessClassFormBean businessClass )
689     {
690         PluginModel pm = getPluginModel( nPluginId );
691         List<BusinessClass> list = pm.getBusinessClasses( );
692 
693         for ( int i = 0; i < list.size( ); i++ )
694         {
695             BusinessClass bc = list.get( i );
696 
697             if ( bc.getId( ) == businessClass.getId( ) )
698             {
699 
700                 BusinessClass newBusinessClass = null;
701                 List<Attribute> attrList = bc.getAttributes( );
702 
703                 try
704                 {
705                     newBusinessClass = _mapper.readValue( _mapper.writeValueAsString( businessClass ), bc.getClass( ) );
706                 }
707                 catch( IOException e )
708                 {
709 
710                     throw new AppException( "JSON parsing error", e );
711                 }
712 
713                 if ( attrList != null && !attrList.isEmpty( ) )
714                 {
715                     newBusinessClass.setAttributes( attrList );
716                 }
717 
718                 list.set( i, newBusinessClass );
719                 pm.setBusinessClasses( list );
720 
721                 savePluginModel( pm );
722 
723                 break;
724             }
725         }
726     }
727 
728     /**
729      * Remove a business class
730      *
731      * @param nPluginId
732      *            The plugin's ID
733      * @param nBusinessClassId
734      *            The business class's ID
735      */
736     public static void removeBusinessClass( int nPluginId, int nBusinessClassId )
737     {
738         PluginModel pm = getPluginModel( nPluginId );
739         List<BusinessClass> list = pm.getBusinessClasses( );
740 
741         for ( int i = 0; i < list.size( ); i++ )
742         {
743             BusinessClass bc = list.get( i );
744 
745             if ( bc.getId( ) == nBusinessClassId )
746             {
747                 list.remove( i );
748                 pm.setBusinessClasses( list );
749                 savePluginModel( pm );
750 
751                 break;
752             }
753         }
754     }
755 
756     /**
757      * Remove all data for a given plugin
758      * 
759      * @param nPluginId
760      *            The plugin ID
761      */
762     public static void removeAll( int nPluginId )
763     {
764         PluginModel pm = getPluginModel( nPluginId );
765         pm.getApplications( ).clear( );
766         pm.getBusinessClasses( ).clear( );
767         pm.getFeatures( ).clear( );
768         pm.getPortlets( ).clear( );
769         savePluginModel( pm );
770     }
771 
772     // //////////////////////////////////////////////////////////////////////////
773     // BUSINESS CLASS ATTRIBUTES
774     /**
775      * Get a given attribute
776      *
777      * @param nPluginId
778      *            The plugin's ID
779      * @param nBusinessClassId
780      *            The business class ID
781      * @param nAttributeId
782      *            The attribute ID
783      * @return The attribute
784      */
785     public static Attribute getAttribute( int nPluginId, int nBusinessClassId, int nAttributeId )
786     {
787         PluginModel pm = getPluginModel( nPluginId );
788         BusinessClass bc = getBusinessClass( pm, nBusinessClassId );
789         if ( bc != null )
790         {
791             List<Attribute> listAttributes = bc.getAttributes( );
792 
793             for ( Attribute attribute : listAttributes )
794             {
795                 if ( attribute.getId( ) == nAttributeId )
796                 {
797                     return attribute;
798                 }
799             }
800 
801             return null;
802         }
803         else
804         {
805             return null;
806         }
807     }
808 
809     /**
810      * Add an attribute to a business class
811      *
812      * @param nPluginId
813      *            The plugin's ID
814      * @param nBusinessClassId
815      *            The business class ID
816      * @param attribute
817      *            The Attribute
818      */
819     public static void addAttribute( int nPluginId, int nBusinessClassId, Attribute attribute )
820     {
821         PluginModel pm = getPluginModel( nPluginId );
822         BusinessClass bc = getBusinessClass( pm, nBusinessClassId );
823         if ( bc != null )
824         {
825             List<Attribute> listAttributes = bc.getAttributes( );
826             attribute.setId( getMaxAttributeId( listAttributes ) + 1 );
827             attribute.setMaxLength( getAttributeMaxLength( attribute.getAttributeTypeId( ) ) );
828 
829             listAttributes.add( attribute );
830             bc.setAttributes( listAttributes );
831 
832             savePluginModel( pm );
833         }
834     }
835 
836     /**
837      * Get The max attribute ID from a list
838      *
839      * @param listAttributes
840      *            The attribute list
841      * @return The max used ID
842      */
843     private static int getMaxAttributeId( List<Attribute> listAttributes )
844     {
845         int nMax = 0;
846 
847         for ( Attribute attribute : listAttributes )
848         {
849             if ( attribute.getId( ) > nMax )
850             {
851                 nMax = attribute.getId( );
852             }
853         }
854 
855         return nMax;
856     }
857 
858     /**
859      * Update an attribute
860      *
861      * @param nPluginId
862      *            The plugin's ID
863      * @param nBusinessClassId
864      *            The business class ID
865      * @param attribute
866      *            The attribute
867      */
868     public static void updateAttribute( int nPluginId, int nBusinessClassId, Attribute attribute )
869     {
870         PluginModel pm = getPluginModel( nPluginId );
871         BusinessClass bc = getBusinessClass( pm, nBusinessClassId );
872         if ( bc != null )
873         {
874             List<Attribute> list = bc.getAttributes( );
875 
876             for ( int i = 0; i < list.size( ); i++ )
877             {
878                 Attribute attr = list.get( i );
879 
880                 if ( attr.getId( ) == attribute.getId( ) )
881                 {
882                     list.set( i, attribute );
883                     bc.setAttributes( list );
884                     savePluginModel( pm );
885 
886                     break;
887                 }
888             }
889         }
890     }
891 
892     /**
893      * Remove an attribute
894      *
895      * @param nPluginId
896      *            The plugin's ID
897      * @param nBusinessClassId
898      *            The business class ID
899      * @param nAttributeId
900      *            The attribute's ID
901      */
902     public static void removeAttribute( int nPluginId, int nBusinessClassId, int nAttributeId )
903     {
904         PluginModel pm = getPluginModel( nPluginId );
905         BusinessClass bc = getBusinessClass( pm, nBusinessClassId );
906         List<Attribute> list;
907         if ( bc != null )
908         {
909             list = bc.getAttributes( );
910             for ( int i = 0; i < list.size( ); i++ )
911             {
912                 Attribute attr = list.get( i );
913 
914                 if ( attr.getId( ) == nAttributeId )
915                 {
916                     list.remove( i );
917                     bc.setAttributes( list );
918                     savePluginModel( pm );
919 
920                     break;
921                 }
922             }
923         }
924 
925     }
926 
927     // //////////////////////////////////////////////////////////////////////////
928     // REST
929     /**
930      * Add the rest
931      *
932      * @param nPluginId
933      *            The plugin's ID
934      * @param rest
935      *            The rest
936      */
937     public static void addRest( int nPluginId, Rest rest )
938     {
939         PluginModel pm = getPluginModel( nPluginId );
940         pm.setRest( rest );
941         savePluginModel( pm );
942     }
943 
944     /**
945      * Get the rest
946      *
947      * @param nPluginId
948      *            The plugin's ID
949      * @return The rest
950      */
951     public static Rest getRest( int nPluginId )
952     {
953         PluginModel pm = getPluginModel( nPluginId );
954         return pm.getRest( );
955     }
956 
957     /**
958      * Update the rest
959      *
960      * @param nPluginId
961      *            The plugin's ID
962      * @param rest
963      *            The rest
964      */
965     public static void updateRest( int nPluginId, Rest rest )
966     {
967         PluginModel pm = getPluginModel( nPluginId );
968         pm.setRest( rest );
969         savePluginModel( pm );
970     }
971 
972     /**
973      * Gets all business classes for a given Application
974      * 
975      * @param pm
976      *            The plugin model
977      * @return The list of business class
978      */
979     public static List<BusinessClass> getBusinessClassesByRest( PluginModel pm )
980     {
981         List<BusinessClass> list = new ArrayList<>( );
982         List<BusinessClass> listAll = pm.getBusinessClasses( );
983         Rest rest = pm.getRest( );
984 
985         for ( int i : rest.getIdBusinessClasses( ) )
986         {
987             for ( BusinessClass bc : listAll )
988             {
989                 if ( bc.getId( ) == i )
990                 {
991                     list.add( bc );
992                 }
993             }
994         }
995         return list;
996     }
997 
998     /**
999      * Gets all business classes for a given Application
1000      * 
1001      * @param pm
1002      *            The plugin model
1003      * @param nApplicationId
1004      *            The Application's ID
1005      * @return The list of business class
1006      */
1007     public static List<BusinessClass> getBusinessClassesByApplication( PluginModel pm, int nApplicationId )
1008     {
1009         List<BusinessClass> list = new ArrayList<>( );
1010         List<Application> listAll = pm.getApplications( );
1011         List<BusinessClass> listAll2 = pm.getBusinessClasses( );
1012 
1013         for ( Application a : listAll )
1014         {
1015             if ( a.getId( ) == nApplicationId )
1016             {
1017                 for ( int i : a.getIdBusinessClasses( ) )
1018                 {
1019                     for ( BusinessClass bc : listAll2 )
1020                     {
1021                         if ( bc.getId( ) == i )
1022                         {
1023                             list.add( bc );
1024                         }
1025                     }
1026                 }
1027             }
1028         }
1029         return list;
1030     }
1031 
1032     /**
1033      * Gets all business classes for a given Feature
1034      * 
1035      * @param pm
1036      *            The plugin model
1037      * @param nFeatureId
1038      *            The feature's ID
1039      * @return The list of business class
1040      */
1041     public static List<BusinessClass> getBusinessClassesByFeature( PluginModel pm, int nFeatureId )
1042     {
1043         List<BusinessClass> list = new ArrayList<>( );
1044         List<Feature> listAll = pm.getFeatures( );
1045         List<BusinessClass> listAll2 = pm.getBusinessClasses( );
1046 
1047         for ( Feature f : listAll )
1048         {
1049             if ( f.getId( ) == nFeatureId )
1050             {
1051                 for ( int i : f.getIdBusinessClasses( ) )
1052                 {
1053                     for ( BusinessClass bc : listAll2 )
1054                     {
1055                         if ( bc.getId( ) == i )
1056                         {
1057                             list.add( bc );
1058                         }
1059                     }
1060                 }
1061             }
1062         }
1063         return list;
1064     }
1065 
1066     /**
1067      * Gets the feature for a given Business Class
1068      * 
1069      * @param pm
1070      *            The plugin model
1071      * @param nBusinessClassId
1072      *            The Business Class's ID
1073      * @return The feature
1074      */
1075     public static Feature getFeatureByBusinessClass( PluginModel pm, int nBusinessClassId )
1076     {
1077         Featurepluginwizard/business/model/Feature.html#Feature">Feature feature = new Feature( );
1078         List<Feature> listAll = pm.getFeatures( );
1079         List<BusinessClass> listAll2 = pm.getBusinessClasses( );
1080 
1081         for ( Feature f : listAll )
1082         {
1083             for ( int i : f.getIdBusinessClasses( ) )
1084             {
1085                 if ( i == nBusinessClassId )
1086                 {
1087                     for ( BusinessClass bc : listAll2 )
1088                     {
1089                         if ( bc.getId( ) == i )
1090                         {
1091                             feature = f;
1092                         }
1093                     }
1094                 }
1095             }
1096         }
1097 
1098         return feature;
1099     }
1100 
1101     /**
1102      * Gets the application for a given Business Class
1103      * 
1104      * @param pm
1105      *            The plugin model
1106      * @param nBusinessClassId
1107      *            The Business Class's ID
1108      * @return The application
1109      */
1110     public static Application getApplicationByBusinessClass( PluginModel pm, int nBusinessClassId )
1111     {
1112         Applicationzard/business/model/Application.html#Application">Application application = new Application( );
1113         List<Application> listAll = pm.getApplications( );
1114         List<BusinessClass> listAll2 = pm.getBusinessClasses( );
1115 
1116         for ( Application a : listAll )
1117         {
1118             for ( int i : a.getIdBusinessClasses( ) )
1119             {
1120                 if ( i == nBusinessClassId )
1121                 {
1122                     for ( BusinessClass bc : listAll2 )
1123                     {
1124                         if ( bc.getId( ) == i )
1125                         {
1126                             application = a;
1127                         }
1128                     }
1129                 }
1130             }
1131         }
1132 
1133         return application;
1134     }
1135 
1136     /**
1137      * Returns a Reference list with all Business Classes * @param nPluginId The Plugin's ID
1138      * 
1139      * @param nPluginId
1140      * @return The list
1141      */
1142     public static ReferenceList getComboBusinessClasses( int nPluginId )
1143     {
1144         ReferenceList list = new ReferenceList( );
1145 
1146         for ( BusinessClass bc : getPluginModel( nPluginId ).getBusinessClasses( ) )
1147         {
1148             list.addItem( bc.getId( ), bc.getBusinessClass( ) );
1149         }
1150 
1151         return list;
1152     }
1153 
1154     /**
1155      * Gets all attribute types
1156      * 
1157      * @return A list of attributes types
1158      */
1159     public static ReferenceList getAttributeTypes( Locale locale )
1160     {
1161         return _serviceAttribute.getAttributeTypes( locale );
1162     }
1163 
1164     /**
1165      * Returns the attribute type corresponding to an ID
1166      * 
1167      * @param nAttributeTypeId
1168      *            The attribute type ID
1169      * @return The type
1170      */
1171     public static String getAttributeType( int nAttributeTypeId )
1172     {
1173         AttributeType type = _serviceAttribute.getType( nAttributeTypeId );
1174 
1175         return type.getJavaType( );
1176     }
1177 
1178     /**
1179      * Return the attribute type max length
1180      * 
1181      * @param nAttributeTypeId
1182      *            The attribute type ID
1183      * @return The max length
1184      */
1185     private static int getAttributeMaxLength( int nAttributeTypeId )
1186     {
1187         AttributeType type = _serviceAttribute.getType( nAttributeTypeId );
1188 
1189         return type.getMaxLength( );
1190     }
1191 
1192     /**
1193      * Returns the attribute prefix corresponding to an ID
1194      * 
1195      * @param nAttributeTypeId
1196      *            The attribute type ID
1197      * @return The type
1198      */
1199     public static String getAttributePrefix( int nAttributeTypeId )
1200     {
1201         return _serviceAttribute.getType( nAttributeTypeId ).getPrefix( );
1202     }
1203 
1204     /**
1205      * Returns the attribute constraint corresponding to an ID
1206      * 
1207      * @param nAttributeTypeId
1208      *            The attribute type ID
1209      * @return The type
1210      */
1211     public static String getAttributeConstraint( int nAttributeTypeId )
1212     {
1213         return _serviceAttribute.getType( nAttributeTypeId ).getConstraint( );
1214     }
1215 
1216     /**
1217      * Returns the attribute type description
1218      * 
1219      * @param nAttributeTypeId
1220      *            The attribute type ID
1221      * @return The type description
1222      */
1223     public static String getAttributeTypeDescription( int nAttributeTypeId )
1224     {
1225         return _serviceAttribute.getType( nAttributeTypeId ).getDescription( );
1226     }
1227 
1228     /**
1229     * Update the description
1230     *
1231     * @param nPluginId
1232     *            The plugin's ID
1233     * @param description
1234     *            The description with new values from pluginwizard form
1235     */
1236     public static void updateDescription( int nPluginId, DescriptionFormBean description )
1237     {
1238         PluginModel pm = getPluginModel( nPluginId );
1239 
1240         try
1241         {
1242             BeanUtils.copyProperties( pm, description );
1243         }
1244         catch( IllegalAccessException | InvocationTargetException e )
1245         {
1246             throw new AppException( "Bean exception", e );
1247         }
1248 
1249         savePluginModel( pm );
1250     }
1251 
1252     /**
1253     * Get description
1254     *
1255     * @param nPluginId
1256     *            The plugin's ID
1257     * @return descriptionFormBean
1258     *            Description of plugin
1259     */
1260     public static DescriptionFormBean getDescription( int nPluginId )
1261     {
1262         PluginModel pm = getPluginModel( nPluginId );
1263 
1264         try
1265         {
1266             return _mapper.readValue( _mapper.writeValueAsString( pm ), DescriptionFormBean.class );
1267         }
1268         catch( IOException e )
1269         {
1270             throw new AppException( "JSON exception", e );
1271 
1272         }
1273     }
1274 
1275     /**
1276     * Get Form Business Class
1277     *
1278     * @param nPluginId
1279     *            The plugin's ID
1280     * @return businessClassFormBean
1281     *            A business class of plugin
1282     */
1283     public static BusinessClassFormBean getFormBusinessClass( int nPluginId, int nBusinessClassId )
1284     {
1285         PluginModel pm = getPluginModel( nPluginId );
1286 
1287         try
1288         {
1289             return _mapper.readValue( _mapper.writeValueAsString( getBusinessClass( pm, nBusinessClassId ) ), BusinessClassFormBean.class );
1290         }
1291         catch( IOException e )
1292         {
1293             throw new AppException( "JSON exception", e );
1294         }
1295     }
1296 
1297    /**
1298      * Set the configuration in plugin model
1299      * 
1300      * @param nPLuginId
1301      *            PLugin ID
1302      * @param configurationFormBean 
1303      */
1304     public static void updateConfiguration( int nPluginId, ConfigurationFormBean configuration )
1305     {
1306         PluginModel pm = getPluginModel( nPluginId );
1307         		
1308         try
1309         {
1310             BeanUtils.copyProperties( pm.getConfiguration( ), configuration );
1311         }
1312         catch( IllegalAccessException | InvocationTargetException e )
1313         {
1314             throw new AppException( "Bean exception", e );
1315         }
1316 
1317         savePluginModel( pm );
1318     }
1319 
1320 }