View Javadoc
1   package fr.paris.lutece.plugins.atelieraba.web;
2   
3   import java.util.ArrayList;
4   import java.util.Arrays;
5   import java.util.Collection;
6   import java.util.HashMap;
7   import java.util.List;
8   import java.util.Map;
9   
10  import javax.servlet.http.HttpServletRequest;
11  
12  import fr.paris.lutece.plugins.atelieraba.business.AtelierHome;
13  import fr.paris.lutece.plugins.atelieraba.business.Cours;
14  import fr.paris.lutece.plugins.atelieraba.business.CoursHome;
15  import fr.paris.lutece.plugins.atelieraba.business.Inscrit;
16  import fr.paris.lutece.plugins.atelieraba.business.InscritHome;
17  import fr.paris.lutece.plugins.atelieraba.utils.constants.AtelierabaConstants;
18  import fr.paris.lutece.plugins.workflowcore.business.state.State;
19  import fr.paris.lutece.portal.service.util.AppPropertiesService;
20  import fr.paris.lutece.portal.service.workflow.WorkflowService;
21  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
22  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
23  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
24  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
25  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
26  import fr.paris.lutece.util.html.Paginator;
27  import fr.paris.lutece.util.url.UrlItem;
28  
29  /**
30   * This class provides the user interface to manage DedoublonnageManuel features ( manage )
31   */
32  @Controller( controllerJsp = "ManageDedoublonnage.jsp", controllerPath = "jsp/admin/plugins/atelieraba/", right = "ATELIERABA_DEDOUBLOUNAGE_MANUEL" )
33  public class DedoublonnageManuelJspBean extends MVCAdminJspBean
34  {
35  
36      /**
37       *
38       */
39      private static final long   serialVersionUID                         = 5827962602778559532L;
40  
41      // Views
42      private static final String VIEW_MANAGE_DEDOUBLONNAGE                = "manageDedoublonnage";
43      private static final String PROPERTY_PAGE_TITLE_MANAGE_DEDOUBLONNAGE = "atelieraba.manage_doublons_manuel.pageTitle";
44      // Templates
45      private static final String TEMPLATE_MANAGE_CANDIDATURES             = "/admin/plugins/atelieraba/manage_dedoublonnage.html";
46      private static final String   JSP_MANAGE_DEDOUBLONNAGE               = "jsp/admin/plugins/atelieraba/ManageDedoublonnage.jsp";
47      
48      //Model
49      private static final String MARK_DEDOUBLONNAGE_CHAMP                   = "dedoublonnage_champ";
50      private static final String MARK_ALL_DEDOUBLONNAGE_CHAMP               = "all_dedoublonnage_champ";
51      protected static final String MARK_PAGINATOR                         = "paginator";
52      protected static final String MARK_NB_ITEMS_PER_PAGE                 = "nb_items_per_page";
53      
54      protected static final String PARAMETER_PAGE_INDEX                   = "page_index";
55      
56      private static final String ACTION_DEDOUBLONNAGE_MANUEL                = "dedoublonnage";
57      private static final String PARAMETER_DEDOUBLONNAGE_CHAMP              = "dedoublonnage_manuel_champ";
58      private static final String       PARAMETER_ID_ATELIER1                    = "id_atelier1";
59      private static final String       PARAMETER_ID_ATELIER2                    = "id_atelier2";
60      private static final String       PARAMETER_ID_ACTION                     = "id_action";
61      private static final String       PARAMETER_ID_INSCRIT                    = "id_inscrit";
62      
63      
64      
65      private static final String       PROPERTY_DEFAULT_LIST_CANDIDAT_PER_PAGE = "atelieraba.listCandidats.itemsPerPage";
66      private static final String       MARK_INSCRIT                            = "inscrit";
67      private static final String       MARK_COURS_1                            = "premier_cours";
68      private static final String       MARK_ATELIER_1                          = "premier_atelier";
69      private static final String       MARK_STATUT_1                           = "statut_premier_atelier";
70      private static final String       MARK_ACTIONS_1                          = "actions_premier_atelier";
71      private static final String       MARK_COURS_2                            = "second_cours";
72      private static final String       MARK_ATELIER_2                          = "second_atelier";
73      private static final String       MARK_STATUT_2                           = "statut_second_atelier";
74      private static final String       MARK_ACTIONS_2                          = "actions_second_atelier";
75      private static final String       MARK_CANDIDATS_LIST                     = "candidats_list";
76      
77      //Constants
78      private static final int          ACTION_SUPPRIMER1                   = 16;
79      private static final int          ACTION_SUPPRIMER2                   = 17;
80      private static final int          PROPERTY_ID_WORKFLOW_1                  = AppPropertiesService.getPropertyInt( AtelierabaConstants.PROPERTY_WORKFLOW_ID,
81              AtelierabaConstants.PROPERTY_ID_WORKFLOW_ATELIER_1 );
82      private static final int          PROPERTY_ID_WORKFLOW_2                  = AppPropertiesService.getPropertyInt( AtelierabaConstants.PROPERTY_WORKFLOW_ID,
83              AtelierabaConstants.PROPERTY_ID_WORKFLOW_ATELIER_2 );
84      
85      // Session
86      private String              _dedoublonnageManuelExceptField;
87      protected int               _nItemsPerPage;
88      protected String            _strCurrentPageIndex;
89      private int                 _nDefaultItemsPerPage;
90  
91      /**
92       * Build the Manage View
93       *
94       * @param request
95       *            The HTTP request
96       * @return The page
97       */
98      @View( value = VIEW_MANAGE_DEDOUBLONNAGE, defaultView = true )
99      public String getManageDedoublonnageManuel( HttpServletRequest request )
100     {
101 
102         Map<String, Object> model = getModel( );
103         
104         List<Map<String, Object>> listInscrits;
105                 
106         model.put( MARK_ALL_DEDOUBLONNAGE_CHAMP, InscritHome.potential_duplicate_fields );
107         model.put( MARK_DEDOUBLONNAGE_CHAMP, _dedoublonnageManuelExceptField );
108         
109         HashMap<String[], String> validatedFilters = new HashMap<>( );
110         String[] field_state_atelier = { "search_statut_un", "t1.id_state = ?", "INT" };
111         String value_demande_recue = "1";
112         validatedFilters.put( field_state_atelier, value_demande_recue ); 
113         List<Map<String, Object>> listCandidats = InscritHome.getInscritsListByFilter(validatedFilters);
114         List<Map<String, Object>> listCandidatsDetailed = new ArrayList<>( );
115         
116         for ( Map<String, Object> candidat : listCandidats )
117         {
118             Map<String, Object> candidatDetailed = new HashMap<>( );
119 
120             Inscrit inscrit = ( Inscrit ) candidat.get( MARK_INSCRIT );
121             Cours cours1 = CoursHome.findByPrimaryKey( ( int ) candidat.get( MARK_COURS_1 ) );
122             State statut1 = WorkflowService.getInstance( ).getState( ( int ) candidat.get( MARK_ATELIER_1 ), AtelierabaConstants.WORKFLOW_RESOURCE_TYPE, PROPERTY_ID_WORKFLOW_1, -1 );
123 
124             candidatDetailed.put( MARK_INSCRIT, inscrit );
125             candidatDetailed.put( MARK_COURS_1, cours1 );
126             candidatDetailed.put( MARK_STATUT_1, statut1 );
127             candidatDetailed.put( MARK_ATELIER_1, ( int ) candidat.get( MARK_ATELIER_1 ) );
128 
129             Cours cours2 = CoursHome.findByPrimaryKey( ( int ) candidat.get( MARK_COURS_2 ) );
130             if ( cours2 != null )
131             {
132                 State statut2 = WorkflowService.getInstance( ).getState( ( int ) candidat.get( MARK_ATELIER_2 ), AtelierabaConstants.WORKFLOW_RESOURCE_TYPE, PROPERTY_ID_WORKFLOW_2, -1 );
133                 
134                 candidatDetailed.put( MARK_COURS_2, cours2 );
135                 candidatDetailed.put( MARK_STATUT_2, statut2 );
136                 candidatDetailed.put( MARK_ATELIER_2, ( int ) candidat.get( MARK_ATELIER_2 ) );
137             }
138 
139             listCandidatsDetailed.add( candidatDetailed );
140         }
141         
142         if ( _dedoublonnageManuelExceptField != null )
143         {
144             listInscrits = ( List<Map<String, Object>> ) InscritHome.getInscritsListOrderedPartialDuplicates( listCandidatsDetailed,_dedoublonnageManuelExceptField );
145         } else
146         {
147             listInscrits = new ArrayList<Map<String, Object>>( );
148         }
149         
150         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
151         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_CANDIDAT_PER_PAGE, 50 );
152         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
153 
154         UrlItem url = new UrlItem( JSP_MANAGE_DEDOUBLONNAGE );
155         LocalizedPaginator<Map<String, Object>> paginator = new LocalizedPaginator<>( listInscrits, _nItemsPerPage, url.getUrl( ), PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
156 
157         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
158         model.put( MARK_PAGINATOR, paginator );
159         model.put( MARK_CANDIDATS_LIST, paginator.getPageItems( ) );
160         
161         
162         
163 
164         return getPage( PROPERTY_PAGE_TITLE_MANAGE_DEDOUBLONNAGE, TEMPLATE_MANAGE_CANDIDATURES, model );
165 
166     }
167     
168     /**
169      * Process the search for doublons manuel
170      *
171      * @param request
172      *            The Http request
173      * @return the html code of the process of doublons manuel
174      */
175     @Action( ACTION_DEDOUBLONNAGE_MANUEL )
176     public String doDedoublonnageManuel( HttpServletRequest request )
177     {
178         String champDedoublonnageManuel = request.getParameter( PARAMETER_DEDOUBLONNAGE_CHAMP );
179 
180         if ( ( champDedoublonnageManuel != null ) && Arrays.asList( InscritHome.potential_duplicate_fields ).contains( champDedoublonnageManuel ) )
181         {
182             _dedoublonnageManuelExceptField = champDedoublonnageManuel;
183         }
184         
185         String strIdAtelier1 = request.getParameter( PARAMETER_ID_ATELIER1 );
186         String strIdAtelier2 = request.getParameter( PARAMETER_ID_ATELIER2 );
187 
188         if ( strIdAtelier1 != null )
189         {
190             int nIdAtelier1;
191 
192             try
193             {
194                 nIdAtelier1 = Integer.parseInt( strIdAtelier1 );                
195             } catch ( NumberFormatException nfe )
196             {
197                 return redirectView( request, VIEW_MANAGE_DEDOUBLONNAGE );
198             }
199          
200             int idWorkflow = AtelierHome.findByPrimaryKey( nIdAtelier1 ).getNumeroAtelier( );
201             WorkflowService.getInstance( ).doProcessAction( nIdAtelier1, AtelierabaConstants.WORKFLOW_RESOURCE_TYPE, ACTION_SUPPRIMER1, -1, request, getLocale( ), false );
202             WorkflowService.getInstance( ).executeActionAutomatic( nIdAtelier1, AtelierabaConstants.WORKFLOW_RESOURCE_TYPE, idWorkflow, -1 );
203         }
204         if ( strIdAtelier2 != null)
205         {
206             int nIdAtelier2;
207 
208             try
209             {
210                 nIdAtelier2 = Integer.parseInt( strIdAtelier2 );
211             } catch ( NumberFormatException nfe )
212             {
213                 return redirectView( request, VIEW_MANAGE_DEDOUBLONNAGE );
214             }
215          
216             int idWorkflow = AtelierHome.findByPrimaryKey( nIdAtelier2 ).getNumeroAtelier( );
217             WorkflowService.getInstance( ).doProcessAction( nIdAtelier2, AtelierabaConstants.WORKFLOW_RESOURCE_TYPE, ACTION_SUPPRIMER2, -1, request, getLocale( ), false );
218             WorkflowService.getInstance( ).executeActionAutomatic( nIdAtelier2, AtelierabaConstants.WORKFLOW_RESOURCE_TYPE, idWorkflow, -1 );
219         }
220 
221         return redirectView( request, VIEW_MANAGE_DEDOUBLONNAGE );
222     }
223 }