View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.ctv.bo.controller.referentiels;
35  
36  import java.util.Map;
37  
38  import javax.servlet.http.HttpServletRequest;
39  
40  import org.apache.commons.beanutils.ConvertUtils;
41  
42  import fr.paris.lutece.plugins.ctv.bo.ItemReferentiel;
43  import fr.paris.lutece.plugins.ctv.bo.ItemReferentiel.TypeReferentiel;
44  import fr.paris.lutece.plugins.ctv.business.IReferentielsService;
45  import fr.paris.lutece.plugins.ctv.converter.EnumConverter;
46  import fr.paris.lutece.portal.service.spring.SpringContextService;
47  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
48  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
49  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
50  
51  @Controller( controllerPath = ManageReferentielsJspBean.CTV_GESTION_REFERENTIELS_PATH, controllerJsp = ManageReferentielsJspBean.MANAGE_REFERENTIELS_JSP, right = "MANAGE_REFERENTIELS" )
52  public class ManageReferentielsJspBean extends MVCAdminJspBean
53  {
54  
55      static final String MANAGE_REFERENTIELS_JSP = "ManageReferentiels.jsp";
56  
57      static final String CTV_GESTION_REFERENTIELS_PATH = "jsp/admin/plugins/ctv/gestionReferentiels";
58  
59      private static final long serialVersionUID = 7395395576430410733L;
60  
61      protected IReferentielsService referentielsService = SpringContextService.getBean( "referentielsService" );
62  
63      @View( defaultView = true, value = "manageReferentiels" )
64      public String getNaturesTavaux( HttpServletRequest request )
65      {
66          TypeReferentiel type = TypeReferentiel.NATURE_TRAVAUX;
67          String parameterType = request.getParameter( "type" );
68          if ( parameterType != null )
69          {
70              type = TypeReferentiel.valueOf( parameterType );
71          }
72          return manageReferentielsByType( type );
73      }
74  
75      private String manageReferentielsByType( TypeReferentiel type )
76      {
77          Map<String, Object> model = getModel( );
78          model.put( "referentiels_list", referentielsService.getItemReferentielList( type ) );
79          model.put( "type", type );
80          model.put( "typesReferentiels", TypeReferentiel.values( ) );
81          return getPage( "ctv.manageReferentiels.pageTitle", "/admin/plugins/ctv/gestionReferentiels/manage_referentiels.html", model );
82      }
83  
84      public String activateItemReferentiel( HttpServletRequest request )
85      {
86          int id = Integer.valueOf( request.getParameter( "itemId" ) );
87          referentielsService.activateItemReferentiel( id );
88          return redirectInViewOfType( request );
89      }
90  
91      public String deactivateItemReferentiel( HttpServletRequest request )
92      {
93          int id = Integer.valueOf( request.getParameter( "itemId" ) );
94          referentielsService.deactivateItemReferentiel( id );
95          return redirectInViewOfType( request );
96      }
97  
98      public String attributeToOccupationItemReferentiel( HttpServletRequest request )
99      {
100         int id = Integer.valueOf( request.getParameter( "itemId" ) );
101         ItemReferentiel item = referentielsService.findItemById( id );
102         item.setOccupation( true );
103         item.setIntentionIntervention( false );
104         referentielsService.updateItem( item );
105         return redirectInViewOfType( request );
106     }
107 
108     public String attributeToIntentionInterventionItemReferentiel( HttpServletRequest request )
109     {
110         int id = Integer.valueOf( request.getParameter( "itemId" ) );
111         ItemReferentiel item = referentielsService.findItemById( id );
112         item.setOccupation( false );
113         item.setIntentionIntervention( true );
114         referentielsService.updateItem( item );
115         return redirectInViewOfType( request );
116     }
117 
118     public String addNewItemReferentiel( HttpServletRequest request )
119     {
120         ItemReferentiel item = new ItemReferentiel( );
121         ConvertUtils.register( new EnumConverter( ), TypeReferentiel.class );
122         populate( item, request );
123         referentielsService.addItemReferentiel( item );
124         return redirectInViewOfType( request );
125     }
126 
127     public String modifyItemReferentiel( HttpServletRequest request )
128     {
129         Map<String, Object> model = getModel( );
130         int id = Integer.valueOf( request.getParameter( "itemId" ) );
131         ItemReferentiel item = referentielsService.findItemById( id );
132         model.put( "item", item );
133         return getPage( "ctv.modify.referentiel.pageTitle", "/admin/plugins/ctv/gestionReferentiels/modify_referentiel.html", model );
134     }
135 
136     public String doModifyItemReferentiel( HttpServletRequest request )
137     {
138         ItemReferentiel item = new ItemReferentiel( );
139         ConvertUtils.register( new EnumConverter( ), TypeReferentiel.class );
140         populate( item, request );
141         item.setActive( true );
142         referentielsService.updateItem( item );
143         return redirectInViewOfType( request );
144     }
145 
146     private String redirectInViewOfType( HttpServletRequest request )
147     {
148         String type = request.getParameter( "type" );
149         return MANAGE_REFERENTIELS_JSP + "?type=" + type;
150     }
151 
152 }