1 | package eu.clarin.cmdi.virtualcollectionregistry.gui.pages; |
---|
2 | |
---|
3 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistry; |
---|
4 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryException; |
---|
5 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryUsageException; |
---|
6 | import eu.clarin.cmdi.virtualcollectionregistry.gui.VolatileEntityModel; |
---|
7 | import eu.clarin.cmdi.virtualcollectionregistry.gui.dialog.ConfirmationDialog; |
---|
8 | import eu.clarin.cmdi.virtualcollectionregistry.gui.menu.AjaxLinkMenuItem; |
---|
9 | import eu.clarin.cmdi.virtualcollectionregistry.gui.menu.AjaxPopupMenu; |
---|
10 | import eu.clarin.cmdi.virtualcollectionregistry.gui.table.CollectionsProvider; |
---|
11 | import eu.clarin.cmdi.virtualcollectionregistry.gui.table.VirtualCollectionTable; |
---|
12 | import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollection; |
---|
13 | import eu.clarin.cmdi.virtualcollectionregistry.service.VirtualCollectionValidator; |
---|
14 | import java.security.Principal; |
---|
15 | import java.util.Collections; |
---|
16 | import java.util.List; |
---|
17 | import org.apache.wicket.Component; |
---|
18 | import org.apache.wicket.PageParameters; |
---|
19 | import org.apache.wicket.Session; |
---|
20 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
21 | import org.apache.wicket.ajax.markup.html.AjaxLink; |
---|
22 | import org.apache.wicket.markup.html.panel.Panel; |
---|
23 | import org.apache.wicket.model.IModel; |
---|
24 | import org.apache.wicket.model.Model; |
---|
25 | import org.apache.wicket.model.StringResourceModel; |
---|
26 | import org.apache.wicket.spring.injection.annot.SpringBean; |
---|
27 | import org.slf4j.Logger; |
---|
28 | import org.slf4j.LoggerFactory; |
---|
29 | |
---|
30 | /** |
---|
31 | * Panel that shows a table with view, edit, publish, delete options for the |
---|
32 | * collections from an injected provided |
---|
33 | * |
---|
34 | * @author twagoo |
---|
35 | */ |
---|
36 | public class BrowseEditableCollectionsPanel extends Panel { |
---|
37 | |
---|
38 | private final static Logger logger = LoggerFactory.getLogger(BrowseEditableCollectionsPanel.class); |
---|
39 | |
---|
40 | @SpringBean |
---|
41 | private VirtualCollectionRegistry vcr; |
---|
42 | |
---|
43 | private class ActionsColumn extends Panel { |
---|
44 | |
---|
45 | public ActionsColumn(String id, IModel<VirtualCollection> model) { |
---|
46 | super(id, model); |
---|
47 | setRenderBodyOnly(true); |
---|
48 | |
---|
49 | final AjaxPopupMenu menu |
---|
50 | = new AjaxPopupMenu("menu", new Model<String>("[actions]")); |
---|
51 | |
---|
52 | final AjaxLinkMenuItem<VirtualCollection> publishItem |
---|
53 | = new AjaxLinkMenuItem<VirtualCollection>( |
---|
54 | new Model<String>("Publish"), model, "publish") { |
---|
55 | @Override |
---|
56 | protected void onClick(AjaxRequestTarget target, |
---|
57 | IModel<VirtualCollection> model) { |
---|
58 | doPublish(target, model); |
---|
59 | } |
---|
60 | }; |
---|
61 | menu.add(publishItem); |
---|
62 | |
---|
63 | final AjaxLinkMenuItem<VirtualCollection> editItem |
---|
64 | = new AjaxLinkMenuItem<VirtualCollection>( |
---|
65 | new Model<String>("Edit"), model, "edit") { |
---|
66 | @Override |
---|
67 | protected void onClick(AjaxRequestTarget target, |
---|
68 | IModel<VirtualCollection> model) { |
---|
69 | doEdit(target, model.getObject()); |
---|
70 | } |
---|
71 | }; |
---|
72 | menu.add(editItem); |
---|
73 | |
---|
74 | final AjaxLinkMenuItem<VirtualCollection> deleteItem |
---|
75 | = new AjaxLinkMenuItem<VirtualCollection>( |
---|
76 | new Model<String>("Delete"), model, "delete") { |
---|
77 | @Override |
---|
78 | protected void onClick(AjaxRequestTarget target, |
---|
79 | IModel<VirtualCollection> model) { |
---|
80 | doDelete(target, model.getObject()); |
---|
81 | } |
---|
82 | }; |
---|
83 | menu.add(deleteItem); |
---|
84 | |
---|
85 | final AjaxLinkMenuItem<VirtualCollection> detailsItem |
---|
86 | = new AjaxLinkMenuItem<VirtualCollection>( |
---|
87 | new Model<String>("Details"), model, "details") { |
---|
88 | @Override |
---|
89 | protected void onClick(AjaxRequestTarget target, |
---|
90 | IModel<VirtualCollection> model) { |
---|
91 | doDetails(target, model); |
---|
92 | } |
---|
93 | }; |
---|
94 | menu.add(detailsItem); |
---|
95 | add(menu); |
---|
96 | |
---|
97 | final VirtualCollection vc = model.getObject(); |
---|
98 | if (vc.isDeleted()) { |
---|
99 | detailsItem.setVisible(false).setEnabled(false); |
---|
100 | editItem.setVisible(false).setEnabled(false); |
---|
101 | } |
---|
102 | if (!vc.isPrivate()) { |
---|
103 | publishItem.setVisible(false).setEnabled(false); |
---|
104 | deleteItem.setVisible(false).setEnabled(false); |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | private class ActionsPanel extends Panel { |
---|
110 | |
---|
111 | public ActionsPanel(String id, IModel<VirtualCollection> model) { |
---|
112 | super(id, model); |
---|
113 | setRenderBodyOnly(true); |
---|
114 | |
---|
115 | final AjaxLink<VirtualCollection> publishLink |
---|
116 | = new AjaxLink<VirtualCollection>("publish", model) { |
---|
117 | @Override |
---|
118 | public void onClick(AjaxRequestTarget target) { |
---|
119 | doPublish(target, getModel()); |
---|
120 | } |
---|
121 | }; |
---|
122 | add(publishLink); |
---|
123 | |
---|
124 | final AjaxLink<VirtualCollection> editLink |
---|
125 | = new AjaxLink<VirtualCollection>("edit", model) { |
---|
126 | @Override |
---|
127 | public void onClick(AjaxRequestTarget target) { |
---|
128 | doEdit(target, getModelObject()); |
---|
129 | } |
---|
130 | }; |
---|
131 | add(editLink); |
---|
132 | |
---|
133 | final AjaxLink<VirtualCollection> deleteLink |
---|
134 | = new AjaxLink<VirtualCollection>("delete", model) { |
---|
135 | @Override |
---|
136 | public void onClick(AjaxRequestTarget target) { |
---|
137 | doDelete(target, getModelObject()); |
---|
138 | } |
---|
139 | }; |
---|
140 | add(deleteLink); |
---|
141 | |
---|
142 | final AjaxLink<VirtualCollection> detailsLink |
---|
143 | = new AjaxLink<VirtualCollection>("details", model) { |
---|
144 | @Override |
---|
145 | public void onClick(AjaxRequestTarget target) { |
---|
146 | doDetails(target, getModel()); |
---|
147 | } |
---|
148 | }; |
---|
149 | add(detailsLink); |
---|
150 | |
---|
151 | final VirtualCollection vc = model.getObject(); |
---|
152 | if (vc.isDeleted()) { |
---|
153 | detailsLink.setVisible(false).setEnabled(false); |
---|
154 | editLink.setVisible(false).setEnabled(false); |
---|
155 | } |
---|
156 | if (!vc.isPrivate()) { |
---|
157 | publishLink.setVisible(false).setEnabled(false); |
---|
158 | deleteLink.setVisible(false).setEnabled(false); |
---|
159 | } |
---|
160 | boolean isVisible = detailsLink.isVisible() |
---|
161 | || editLink.isVisible() || publishLink.isVisible() |
---|
162 | || deleteLink.isVisible(); |
---|
163 | setVisible(isVisible); |
---|
164 | } |
---|
165 | } // class BrowsePrivateCollectionsPage.ActionsPanel |
---|
166 | |
---|
167 | private final class PublishCollectionDialog extends ConfirmationDialog { |
---|
168 | |
---|
169 | private IModel<VirtualCollection> vcModel; |
---|
170 | |
---|
171 | public PublishCollectionDialog(String id, |
---|
172 | final Component updateComponenet) { |
---|
173 | super(id, updateComponenet); |
---|
174 | setInitialWidth(400); |
---|
175 | } |
---|
176 | |
---|
177 | @Override |
---|
178 | public void onConfirm(AjaxRequestTarget target) { |
---|
179 | try { |
---|
180 | try { |
---|
181 | prePublicationValidator.validate(vcModel.getObject()); |
---|
182 | doPublish(vcModel.getObject().getId()); |
---|
183 | } catch (VirtualCollectionRegistryUsageException ex) { |
---|
184 | confirmPublishCollectionDialog.showDialogue(target, vcModel, ex.getValidationErrors()); |
---|
185 | } |
---|
186 | } catch (VirtualCollectionRegistryException ex) { |
---|
187 | logger.error("Could not publish collection {}, id {}", vcModel.getObject().getName(), vcModel.getObject().getId(), ex); |
---|
188 | Session.get().error(ex.getMessage()); |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | public void showDialogue(AjaxRequestTarget target, IModel<VirtualCollection> vc) { |
---|
193 | this.vcModel = vc; |
---|
194 | super.show(target, |
---|
195 | new StringResourceModel("collections.publishconfirm", vc)); |
---|
196 | } |
---|
197 | } // class BrowsePrivateCollectionsPage.PublishCollectionDialog |
---|
198 | |
---|
199 | private void doPublish(long vcId) throws VirtualCollectionRegistryException { |
---|
200 | vcr.setVirtualCollectionState(getUser(), vcId, |
---|
201 | VirtualCollection.State.PUBLIC_PENDING); |
---|
202 | } |
---|
203 | |
---|
204 | private final class ConfirmPublishCollectionDialog extends ConfirmationDialog { |
---|
205 | |
---|
206 | private long vcId; |
---|
207 | |
---|
208 | public ConfirmPublishCollectionDialog(String id, |
---|
209 | final Component updateComponenet) { |
---|
210 | super(id, updateComponenet); |
---|
211 | setInitialWidth(400); |
---|
212 | } |
---|
213 | |
---|
214 | @Override |
---|
215 | public void onConfirm(AjaxRequestTarget target) { |
---|
216 | try { |
---|
217 | doPublish(vcId); |
---|
218 | } catch (VirtualCollectionRegistryException ex) { |
---|
219 | logger.error("Could not publish collection with id {}", vcId, ex); |
---|
220 | Session.get().error(ex.getMessage()); |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | public void showDialogue(AjaxRequestTarget target, IModel<VirtualCollection> vc, List<String> warnings) { |
---|
225 | this.vcId = vc.getObject().getId(); |
---|
226 | StringBuilder sb = new StringBuilder(); |
---|
227 | for (String warning : warnings) { |
---|
228 | sb.append(" -").append(warning).append("\n"); |
---|
229 | } |
---|
230 | super.show(target, |
---|
231 | new StringResourceModel("collections.publishwarningsconfirm", vc, new Object[]{sb})); |
---|
232 | } |
---|
233 | |
---|
234 | @Override |
---|
235 | protected Model<String> getCssClass() { |
---|
236 | return Model.of("longConfirmationDialog"); |
---|
237 | } |
---|
238 | |
---|
239 | } |
---|
240 | |
---|
241 | private final class DeleteCollectionDialog extends ConfirmationDialog { |
---|
242 | |
---|
243 | private long vcId; |
---|
244 | |
---|
245 | public DeleteCollectionDialog(String id, |
---|
246 | final Component updateComponenet) { |
---|
247 | super(id, updateComponenet); |
---|
248 | setInitialWidth(400); |
---|
249 | } |
---|
250 | |
---|
251 | @Override |
---|
252 | public void onConfirm(AjaxRequestTarget target) { |
---|
253 | try { |
---|
254 | vcr.deleteVirtualCollection(getUser(), vcId); |
---|
255 | } catch (VirtualCollectionRegistryException e) { |
---|
256 | e.printStackTrace(); |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | public void show(AjaxRequestTarget target, VirtualCollection vc) { |
---|
261 | this.vcId = vc.getId(); |
---|
262 | super.show(target, |
---|
263 | new StringResourceModel("collections.deleteconfirm", |
---|
264 | new VolatileEntityModel<VirtualCollection>(vc))); |
---|
265 | } |
---|
266 | } // class BrowsePrivateCollectionsPage.PublishCollectionDialog |
---|
267 | |
---|
268 | private final class EditPublishedCollectionDialog extends ConfirmationDialog { |
---|
269 | |
---|
270 | private long vcId; |
---|
271 | |
---|
272 | public EditPublishedCollectionDialog(String id, |
---|
273 | final Component updateComponenet) { |
---|
274 | super(id, updateComponenet); |
---|
275 | setInitialWidth(400); |
---|
276 | } |
---|
277 | |
---|
278 | @Override |
---|
279 | public void onConfirm(AjaxRequestTarget target) { |
---|
280 | setResponsePage(EditVirtualCollectionPage.class, new PageParameters(Collections.singletonMap("id", vcId))); |
---|
281 | } |
---|
282 | |
---|
283 | public void showDialogue(AjaxRequestTarget target, VirtualCollection vc) { |
---|
284 | this.vcId = vc.getId(); |
---|
285 | super.show(target, |
---|
286 | new StringResourceModel("collections.editpublishedconfirm", |
---|
287 | new VolatileEntityModel<>(vc))); |
---|
288 | } |
---|
289 | } |
---|
290 | |
---|
291 | private final PublishCollectionDialog publishDialog; |
---|
292 | private final DeleteCollectionDialog deleteDialog; |
---|
293 | private final EditPublishedCollectionDialog editPublishedDialog; |
---|
294 | private final ConfirmPublishCollectionDialog confirmPublishCollectionDialog; |
---|
295 | |
---|
296 | @SpringBean(name = "publication-soft") |
---|
297 | private VirtualCollectionValidator prePublicationValidator; |
---|
298 | |
---|
299 | /** |
---|
300 | * |
---|
301 | * @param id panel id |
---|
302 | * @param provider provider for collections that should be shown |
---|
303 | */ |
---|
304 | public BrowseEditableCollectionsPanel(String id, CollectionsProvider provider) { |
---|
305 | super(id); |
---|
306 | final VirtualCollectionTable table |
---|
307 | = new VirtualCollectionTable("collectionsTable", provider, true) { |
---|
308 | @Override |
---|
309 | protected Panel createActionColumn(String componentId, |
---|
310 | IModel<VirtualCollection> model) { |
---|
311 | return new ActionsColumn(componentId, model); |
---|
312 | } |
---|
313 | |
---|
314 | @Override |
---|
315 | protected Panel createActionPanel(String componentId, |
---|
316 | IModel<VirtualCollection> model) { |
---|
317 | return new ActionsPanel(componentId, model); |
---|
318 | } |
---|
319 | }; |
---|
320 | add(table); |
---|
321 | |
---|
322 | publishDialog = new PublishCollectionDialog("publishCollectionDialog", table); |
---|
323 | add(publishDialog); |
---|
324 | |
---|
325 | deleteDialog = new DeleteCollectionDialog("deleteCollectionDialog", table); |
---|
326 | add(deleteDialog); |
---|
327 | |
---|
328 | editPublishedDialog = new EditPublishedCollectionDialog("editPublishedCollectionDialog", table); |
---|
329 | add(editPublishedDialog); |
---|
330 | |
---|
331 | confirmPublishCollectionDialog = new ConfirmPublishCollectionDialog("confirmPublishCollectionDialog", table); |
---|
332 | add(confirmPublishCollectionDialog); |
---|
333 | } |
---|
334 | |
---|
335 | private void doEdit(AjaxRequestTarget target, VirtualCollection vc) { |
---|
336 | if (vc.isPublic()) { |
---|
337 | // ask for confirmation when trying to edit a published collection |
---|
338 | editPublishedDialog.showDialogue(target, vc); |
---|
339 | } else { |
---|
340 | setResponsePage(EditVirtualCollectionPage.class, new PageParameters(Collections.singletonMap("id", vc.getId()))); |
---|
341 | } |
---|
342 | } |
---|
343 | |
---|
344 | private void doPublish(AjaxRequestTarget target, |
---|
345 | IModel<VirtualCollection> vc) { |
---|
346 | publishDialog.showDialogue(target, vc); |
---|
347 | } |
---|
348 | |
---|
349 | private void doDelete(AjaxRequestTarget target, |
---|
350 | VirtualCollection vc) { |
---|
351 | deleteDialog.show(target, vc); |
---|
352 | } |
---|
353 | |
---|
354 | private void doDetails(AjaxRequestTarget target, IModel<VirtualCollection> vc) { |
---|
355 | setResponsePage(VirtualCollectionDetailsPage.class, VirtualCollectionDetailsPage.createPageParameters(vc.getObject(), getPage().getPageReference())); |
---|
356 | } |
---|
357 | |
---|
358 | private Principal getUser() { |
---|
359 | return ((BasePage) getPage()).getUser(); |
---|
360 | } |
---|
361 | } |
---|