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.genericattributes.web.admin;
35  
36  import java.util.Map;
37  
38  import javax.servlet.http.HttpServletRequest;
39  
40  import org.apache.commons.lang3.math.NumberUtils;
41  
42  import fr.paris.lutece.plugins.genericattributes.business.EntryType;
43  import fr.paris.lutece.plugins.genericattributes.business.EntryTypeHome;
44  import fr.paris.lutece.portal.service.template.AppTemplateService;
45  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
46  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
47  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
48  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
49  import fr.paris.lutece.util.html.HtmlTemplate;
50  
51  @Controller( controllerJsp = "ManageEntryType.jsp", controllerPath = "jsp/admin/plugins/genericattributes/", right = "ENTRY_TYPE_MANAGEMENT" )
52  public class EntryTypeJspBean extends MVCAdminJspBean
53  {
54      private static final long serialVersionUID = -346665476651669896L;
55  
56      // Parameters
57      private static final String PARAMETER_ID = "id_type";
58      private static final String PARAMETER_TITLE = "title";
59      private static final String PARAMETER_ICON = "iconName";
60  
61      // Views
62      private static final String VIEW_MANAGE_ENTRY_TYPE = "manageEntryType";
63      private static final String VIEW_EDIT_ENTRY_TYPE = "editEntryType";
64  
65      // Templates
66      private static final String TEMPLATE_MANAGE_ENTRY_TYPE = "/admin/plugins/genericattributes/manage_entry_type.html";
67      private static final String TEMPLATE_EDIT_ENTRY_TYPE = "/admin/plugins/genericattributes/modify_entry_type.html";
68  
69      // Marks
70      private static final String MARK_ENTRY_TYPE_LIST = "entryTypeList";
71      private static final String MARK_ENTRY_TYPE = "entryType";
72  
73      // Actions
74      private static final String ACTION_CHANGE_ACTIVE = "doChangeActive";
75      private static final String ACTION_MOVE_UP = "doMoveUp";
76      private static final String ACTION_MOVE_DOWN = "doMoveDown";
77      private static final String ACTION_DO_EDIT = "modifyEntryType";
78  
79      /**
80       * Build the Manage View
81       * 
82       * @param request
83       *            The HTTP request
84       * @return The page
85       */
86      @View( value = VIEW_MANAGE_ENTRY_TYPE, defaultView = true )
87      public String getManageEntryType( HttpServletRequest request )
88      {
89          Map<String, Object> model = getModel( );
90          model.put( MARK_ENTRY_TYPE_LIST, EntryTypeHome.getCompleteList( ) );
91  
92          HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_ENTRY_TYPE, getLocale( ), model );
93  
94          return getAdminPage( templateList.getHtml( ) );
95      }
96  
97      /**
98       * Change the inactive value of the EntryType
99       * 
100      * @param request
101      * @return
102      */
103     @Action( ACTION_CHANGE_ACTIVE )
104     public String doChangeActive( HttpServletRequest request )
105     {
106         int idType = NumberUtils.toInt( request.getParameter( PARAMETER_ID ), -1 );
107 
108         if ( idType != -1 )
109         {
110             EntryType entryType = EntryTypeHome.findByPrimaryKey( idType );
111             if ( entryType != null )
112             {
113                 entryType.setInactive( !entryType.isInactive( ) );
114                 EntryTypeHome.update( entryType );
115             }
116         }
117 
118         return redirectView( request, VIEW_MANAGE_ENTRY_TYPE );
119     }
120 
121     /**
122      * Increase the priority of the EntryType
123      * 
124      * @param request
125      * @return
126      */
127     @Action( ACTION_MOVE_UP )
128     public String doMoveUp( HttpServletRequest request )
129     {
130         int idType = NumberUtils.toInt( request.getParameter( PARAMETER_ID ), -1 );
131 
132         if ( idType != -1 )
133         {
134             EntryType entryTypeToMoveUp = EntryTypeHome.findByPrimaryKey( idType );
135             changeOrder( entryTypeToMoveUp, -1 );
136         }
137 
138         return redirectView( request, VIEW_MANAGE_ENTRY_TYPE );
139     }
140 
141     /**
142      * Decrease the priority of the EntryType
143      * 
144      * @param request
145      * @return
146      */
147     @Action( ACTION_MOVE_DOWN )
148     public String doMoveDown( HttpServletRequest request )
149     {
150         int idType = NumberUtils.toInt( request.getParameter( PARAMETER_ID ), -1 );
151 
152         if ( idType != -1 )
153         {
154             EntryType entryTypeToMoveDown = EntryTypeHome.findByPrimaryKey( idType );
155             changeOrder( entryTypeToMoveDown, 1 );
156         }
157 
158         return redirectView( request, VIEW_MANAGE_ENTRY_TYPE );
159     }
160 
161     private void changeOrder( EntryType entryToMove, int orderChange )
162     {
163         int oldOrder = entryToMove.getOrder( );
164         int newOrder = oldOrder + orderChange;
165 
166         for ( EntryType entryType : EntryTypeHome.getList( entryToMove.getPlugin( ) ) )
167         {
168             if ( entryType.getOrder( ) == newOrder )
169             {
170                 entryType.setOrder( oldOrder );
171                 EntryTypeHome.update( entryType );
172 
173                 entryToMove.setOrder( newOrder );
174                 EntryTypeHome.update( entryToMove );
175             }
176         }
177     }
178 
179     @View( value = VIEW_EDIT_ENTRY_TYPE )
180     public String getEditEntryType( HttpServletRequest request )
181     {
182         int idType = NumberUtils.toInt( request.getParameter( PARAMETER_ID ), -1 );
183 
184         if ( idType == -1 )
185         {
186             return redirectView( request, VIEW_MANAGE_ENTRY_TYPE );
187         }
188         Map<String, Object> model = getModel( );
189         model.put( MARK_ENTRY_TYPE, EntryTypeHome.findByPrimaryKey( idType ) );
190         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_EDIT_ENTRY_TYPE, getLocale( ), model );
191 
192         return getAdminPage( templateList.getHtml( ) );
193     }
194 
195     @Action( ACTION_DO_EDIT )
196     public String doEdit( HttpServletRequest request )
197     {
198         int idType = NumberUtils.toInt( request.getParameter( PARAMETER_ID ), -1 );
199 
200         if ( idType != -1 )
201         {
202             EntryType entryType = EntryTypeHome.findByPrimaryKey( idType );
203             entryType.setTitle( request.getParameter( PARAMETER_TITLE ) );
204             entryType.setIconName( request.getParameter( PARAMETER_ICON ) );
205             EntryTypeHome.update( entryType );
206         }
207 
208         return redirectView( request, VIEW_MANAGE_ENTRY_TYPE );
209     }
210 }