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