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.crm.service.demand;
35  
36  import fr.paris.lutece.plugins.crm.business.demand.Demand;
37  import fr.paris.lutece.plugins.crm.business.demand.DemandFilter;
38  import fr.paris.lutece.plugins.crm.business.demand.MokeDemand;
39  
40  import java.sql.Timestamp;
41  
42  import java.util.ArrayList;
43  import java.util.Date;
44  import java.util.HashMap;
45  import java.util.List;
46  import java.util.Locale;
47  import java.util.Map;
48  
49  public class MokeDemandService extends DemandService
50  {
51      private static final int ID_DEMAND1 = 1;
52      private static final int ID_DEMAND2 = 2;
53      private static final int ID_DEMAND3 = 3;
54      private static final int ID_DEMAND4 = 4;
55      private static final String ID_STATUS_CRM1 = "1";
56      private static final String ID_STATUS_CRM2 = "2";
57  
58      /**
59       * Find a demand by its primary key
60       * 
61       * @param nIdDemand
62       *            the id demand
63       * @return a {@link Demand}
64       */
65      public Demand findByPrimaryKey( int nIdDemand )
66      {
67          Demand demand = new MokeDemand( nIdDemand );
68          System.out.println( trace( demand ) );
69  
70          return demand;
71      }
72  
73      /**
74       * Create a new demand
75       * 
76       * @param demand
77       *            the demand
78       * @return the newly created demand id
79       */
80      public int create( Demand demand )
81      {
82          if ( demand != null )
83          {
84              demand.setDateModification( new Timestamp( new Date( ).getTime( ) ) );
85              demand.setIdDemand( ID_DEMAND1 );
86          }
87  
88          System.out.println( trace( demand ) );
89  
90          return demand.getIdDemand( );
91      }
92  
93      /**
94       * Update a demand
95       * 
96       * @param demand
97       *            the demand
98       */
99      public void update( Demand demand )
100     {
101         if ( demand != null )
102         {
103             demand.setDateModification( new Timestamp( new Date( ).getTime( ) ) );
104         }
105 
106         System.out.println( trace( demand ) );
107     }
108 
109     /**
110      * Remove a demand
111      * 
112      * @param nIdDemand
113      *            the id demand
114      */
115     public void remove( int nIdDemand )
116     {
117         System.out.println( trace( ) );
118     }
119 
120     /**
121      * Remove a demand and its resource
122      * 
123      * @param nIdDemand
124      *            the id demand
125      */
126     public void removeWithItsResource( int nIdDemand )
127     {
128         System.out.println( trace( ) );
129     }
130 
131     /**
132      * Remove the demands given an id demand type
133      * 
134      * @param nIdDemandType
135      *            the id demand type
136      */
137     public void removeByIdDemandType( int nIdDemandType )
138     {
139         System.out.println( trace( ) );
140     }
141 
142     /**
143      * Find all demands
144      * 
145      * @return a list of {@link Demand}
146      */
147     public List<Demand> findAll( )
148     {
149         List<Demand> listDemands = new ArrayList<Demand>( );
150         listDemands.add( new MokeDemand( ID_DEMAND1 ) );
151         listDemands.add( new MokeDemand( ID_DEMAND2 ) );
152         System.out.println( trace( listDemands ) );
153 
154         return listDemands;
155     }
156 
157     /**
158      * Find by filter
159      * 
160      * @param dFilter
161      *            the filter
162      * @return a list of {@link Demand}
163      */
164     public List<Demand> findByFilter( DemandFilter dFilter )
165     {
166         List<Demand> listDemands = new ArrayList<Demand>( );
167         listDemands.add( new MokeDemand( ID_DEMAND1 ) );
168         listDemands.add( new MokeDemand( ID_DEMAND2 ) );
169         System.out.println( trace( listDemands, dFilter ) );
170 
171         return listDemands;
172     }
173 
174     /**
175      * Find the demands given an user crm id
176      * 
177      * @param nIdCRMUser
178      *            the user crm id
179      * @param locale
180      *            {@link Locale}
181      * @return a map of (id_status_crm, List&lt;Demand&gt;)
182      */
183     public Map<String, List<Demand>> findByIdCRMUser( int nIdCRMUser, Locale locale )
184     {
185         Map<String, List<Demand>> map = new HashMap<String, List<Demand>>( );
186 
187         List<Demand> listDemands = new ArrayList<Demand>( );
188         listDemands.add( new MokeDemand( ID_DEMAND1 ) );
189         listDemands.add( new MokeDemand( ID_DEMAND2 ) );
190         map.put( ID_STATUS_CRM1, listDemands );
191         System.out.println( trace( listDemands ) );
192 
193         listDemands = new ArrayList<Demand>( );
194         listDemands.add( new MokeDemand( ID_DEMAND3 ) );
195         listDemands.add( new MokeDemand( ID_DEMAND4 ) );
196         map.put( ID_STATUS_CRM2, listDemands );
197         System.out.println( trace( listDemands ) );
198 
199         return map;
200     }
201 
202     /**
203      * Trace
204      * 
205      * @return trace
206      */
207     private String trace( )
208     {
209         return trace( null, null );
210     }
211 
212     /**
213      * Trace
214      * 
215      * @param demand
216      *            demand
217      * @return trace
218      */
219     private String trace( Demand demand )
220     {
221         List<Demand> listDemands = new ArrayList<Demand>( );
222         listDemands.add( demand );
223 
224         return trace( listDemands, null );
225     }
226 
227     /**
228      * Trace
229      * 
230      * @param listDemands
231      *            the list of demands
232      * @return trace
233      */
234     private String trace( List<Demand> listDemands )
235     {
236         return trace( listDemands, null );
237     }
238 
239     /**
240      * Trace
241      * 
242      * @param listDemands
243      *            the list of demands
244      * @return trace
245      */
246     private String trace( List<Demand> listDemands, DemandFilter dFilter )
247     {
248         StringBuilder sbTrace = new StringBuilder( );
249         sbTrace.append( "\n ---------------------- Demand Service -------------------" );
250         sbTrace.append( "\nMethod name : " + Thread.currentThread( ).getStackTrace( ) [2].getMethodName( ) );
251 
252         if ( ( listDemands != null ) && ( listDemands.size( ) > 0 ) )
253         {
254             for ( Demand demand : listDemands )
255             {
256                 sbTrace.append( "\n-- Demand --" );
257                 sbTrace.append( "\nid_demand : " + demand.getIdDemand( ) );
258                 sbTrace.append( "\ndata : " + demand.getData( ) );
259                 sbTrace.append( "\nid_crm_user : " + demand.getIdCRMUser( ) );
260                 sbTrace.append( "\nid_demand_type : " + demand.getIdDemandType( ) );
261                 sbTrace.append( "\nid_status_crm : " + demand.getIdStatusCRM( ) );
262                 sbTrace.append( "\nstatus_text : " + demand.getStatusText( ) );
263                 sbTrace.append( "\ndate_modifiaction : " + demand.getDateModification( ) );
264             }
265         }
266 
267         if ( dFilter != null )
268         {
269             sbTrace.append( "\n-- Demand Filter --" );
270             sbTrace.append( "\nid_crm_user : " + dFilter.getIdCRMUser( ) );
271             sbTrace.append( "\nid_demand_type : " + dFilter.getIdDemandType( ) );
272             sbTrace.append( "\nid_status_crm : " + dFilter.getIdStatusCRM( ) );
273             sbTrace.append( "\ndate_modification : " + dFilter.getDateModification( ) );
274             sbTrace.append( "\nis_wide_search : " + dFilter.getIsWideSearch( ) );
275             sbTrace.append( "\noperator_date_modification : " + dFilter.getOperatorDateModification( ) );
276         }
277 
278         sbTrace.append( "\n --------------------------------------------------------------------" );
279 
280         return sbTrace.toString( );
281     }
282 }