1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
62 private static final String VIEW_MANAGE_ENTRY_TYPE = "manageEntryType";
63 private static final String VIEW_EDIT_ENTRY_TYPE = "editEntryType";
64
65
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
70 private static final String MARK_ENTRY_TYPE_LIST = "entryTypeList";
71 private static final String MARK_ENTRY_TYPE = "entryType";
72
73
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
81
82
83
84
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
99
100
101
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
123
124
125
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
143
144
145
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 }