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.dansmarue.modules.rest.service.formatters;
35  
36  import java.util.List;
37  
38  import org.apache.commons.lang.StringUtils;
39  
40  import fr.paris.lutece.plugins.dansmarue.business.entities.TypeSignalement;
41  import fr.paris.lutece.plugins.dansmarue.modules.rest.util.constants.SignalementRestConstants;
42  import fr.paris.lutece.plugins.dansmarue.service.ITypeSignalementService;
43  import fr.paris.lutece.plugins.rest.service.formatters.IFormatter;
44  import fr.paris.lutece.plugins.rest.util.json.JSONUtil;
45  import fr.paris.lutece.portal.service.spring.SpringContextService;
46  import net.sf.json.JSONArray;
47  import net.sf.json.JSONObject;
48  
49  /**
50   * CategoriesFormatterJson.
51   */
52  public class CategoriesFormatterJson implements IFormatter<TypeSignalement>
53  {
54  
55      /** The type signalement service. */
56      // SERVICES
57      private ITypeSignalementService _typeSignalementService = SpringContextService.getBean( "typeSignalementService" );
58  
59      /**
60       * {@inheritDoc}
61       */
62      @Override
63      public String format( TypeSignalement typeSignalement )
64      {
65          JSONObject jsonObject = new JSONObject( );
66  
67          if ( typeSignalement != null )
68          {
69              if ( typeSignalement.getTypeSignalementParent( ) != null )
70              {
71                  jsonObject.accumulate( SignalementRestConstants.JSON_TAG_CATEGORIES_PARENT_ID, typeSignalement.getTypeSignalementParent( ).getId( ) );
72              }
73  
74              jsonObject.accumulate( SignalementRestConstants.JSON_TAG_CATEGORIES_NAME, typeSignalement.getLibelle( ) );
75              jsonObject.accumulate( SignalementRestConstants.JSON_TAG_IS_AGENT, typeSignalement.getIsAgent( ) );
76              if ( StringUtils.isNotBlank( typeSignalement.getAlias( ) ) )
77              {
78                  jsonObject.accumulate( SignalementRestConstants.JSON_TAG_CATEGORIES_ALIAS, typeSignalement.getAliasMobileDefault( ) );
79              }
80              if ( typeSignalement.isHorsDMR( ) )
81              {
82                  jsonObject.accumulate( SignalementRestConstants.JSON_TAG_HORS_DMR, typeSignalement.isHorsDMR( ) );
83                  jsonObject.accumulate( SignalementRestConstants.JSON_TAG_MESAGE_HORS_DMR, typeSignalement.getMessageHorsDMR( ) );
84  
85              }
86              List<TypeSignalement> listeTypeSignalement = _typeSignalementService.getAllSousTypeSignalement( typeSignalement.getId( ) );
87  
88              if ( ( listeTypeSignalement != null ) && !listeTypeSignalement.isEmpty( ) )
89              {
90                  JSONArray jsonArray = new JSONArray( );
91  
92                  for ( TypeSignalement ts : listeTypeSignalement )
93                  {
94                      jsonArray.element( ts.getId( ) );
95                  }
96  
97                  jsonObject.accumulate( SignalementRestConstants.JSON_TAG_CATEGORIES_CHILDREN_ID, jsonArray );
98              }
99          }
100 
101         return jsonObject.toString( );
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public String format( List<TypeSignalement> listTypeSignalement )
109     {
110         JSONObject jsonObject = new JSONObject( );
111 
112         List<TypeSignalement> listeTypeSignalement = _typeSignalementService.getAllTypeSignalementWithoutParent( );
113         JSONArray jsonArray = new JSONArray( );
114 
115         for ( TypeSignalement ts : listeTypeSignalement )
116         {
117             if ( ts.getActif( ) )
118             {
119                 jsonArray.element( ts.getId( ) );
120             }
121         }
122 
123         JSONObject jsonObjectWithoutParent = new JSONObject( );
124         jsonObjectWithoutParent.accumulate( SignalementRestConstants.JSON_TAG_CATEGORIES_CHILDREN_ID, jsonArray );
125         jsonObject.accumulate( "0", jsonObjectWithoutParent );
126 
127         for ( TypeSignalement typeSignalement : listTypeSignalement )
128         {
129             if ( typeSignalement.getActif( ) )
130             {
131                 jsonObject.accumulate( String.valueOf( typeSignalement.getId( ) ), format( typeSignalement ) );
132             }
133         }
134 
135         return jsonObject.toString( 4 );
136     }
137 
138     /**
139      * {@inheritDoc}
140      */
141     @Override
142     public String formatError( String strCode, String strMessage )
143     {
144         if ( StringUtils.isNumeric( strCode ) )
145         {
146             return JSONUtil.formatError( strMessage, Integer.parseInt( strCode ) );
147         }
148         else
149         {
150             return JSONUtil.formatError( strMessage, -1 );
151         }
152     }
153 }