1 | package eu.clarin.cmdi.virtualcollectionregistry.gui.pages; |
---|
2 | |
---|
3 | import java.security.Principal; |
---|
4 | |
---|
5 | import org.apache.wicket.Component; |
---|
6 | import org.apache.wicket.WicketRuntimeException; |
---|
7 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
8 | import org.apache.wicket.ajax.markup.html.AjaxLink; |
---|
9 | import org.apache.wicket.authorization.strategies.role.Roles; |
---|
10 | import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation; |
---|
11 | import org.apache.wicket.markup.html.panel.Panel; |
---|
12 | import org.apache.wicket.model.IModel; |
---|
13 | import org.apache.wicket.model.Model; |
---|
14 | import org.apache.wicket.model.StringResourceModel; |
---|
15 | |
---|
16 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistry; |
---|
17 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryException; |
---|
18 | import eu.clarin.cmdi.virtualcollectionregistry.gui.ApplicationSession; |
---|
19 | import eu.clarin.cmdi.virtualcollectionregistry.gui.dialog.ConfirmationDialog; |
---|
20 | import eu.clarin.cmdi.virtualcollectionregistry.gui.table.VirtualCollectionTable; |
---|
21 | import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollection; |
---|
22 | import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollection.State; |
---|
23 | |
---|
24 | @AuthorizeInstantiation(Roles.USER) |
---|
25 | @SuppressWarnings("serial") |
---|
26 | public class BrowsePrivateCollectionsPage extends BasePage { |
---|
27 | private class ActionsPanel extends Panel { |
---|
28 | public ActionsPanel(String id, IModel<VirtualCollection> model) { |
---|
29 | super(id, model); |
---|
30 | setRenderBodyOnly(true); |
---|
31 | |
---|
32 | final AjaxLink<VirtualCollection> publishLink = |
---|
33 | new AjaxLink<VirtualCollection>("publish", model) { |
---|
34 | @Override |
---|
35 | public void onClick(AjaxRequestTarget target) { |
---|
36 | doPublish(target, getModelObject()); |
---|
37 | } |
---|
38 | }; |
---|
39 | add(publishLink); |
---|
40 | |
---|
41 | final AjaxLink<VirtualCollection> editLink = |
---|
42 | new AjaxLink<VirtualCollection>("edit", model) { |
---|
43 | @Override |
---|
44 | public void onClick(AjaxRequestTarget target) { |
---|
45 | doEdit(target, getModelObject()); |
---|
46 | } |
---|
47 | }; |
---|
48 | add(editLink); |
---|
49 | |
---|
50 | final AjaxLink<VirtualCollection> deleteLink = |
---|
51 | new AjaxLink<VirtualCollection>("delete", model) { |
---|
52 | @Override |
---|
53 | public void onClick(AjaxRequestTarget target) { |
---|
54 | doDelete(target, getModelObject()); |
---|
55 | } |
---|
56 | }; |
---|
57 | add(deleteLink); |
---|
58 | |
---|
59 | final AjaxLink<VirtualCollection> detailsLink = |
---|
60 | new AjaxLink<VirtualCollection>("details", model) { |
---|
61 | @Override |
---|
62 | public void onClick(AjaxRequestTarget target) { |
---|
63 | doDetails(target, getModelObject()); |
---|
64 | } |
---|
65 | }; |
---|
66 | add(detailsLink); |
---|
67 | |
---|
68 | VirtualCollection vc = model.getObject(); |
---|
69 | if (vc.isDeleted()) { |
---|
70 | detailsLink.setVisible(false).setEnabled(false); |
---|
71 | } |
---|
72 | if (!vc.isPrivate()) { |
---|
73 | editLink.setVisible(false).setEnabled(false); |
---|
74 | publishLink.setVisible(false).setEnabled(false); |
---|
75 | deleteLink.setVisible(false).setEnabled(false); |
---|
76 | } |
---|
77 | } |
---|
78 | } // class BrowsePrivateCollectionsPage.ActionsPanel |
---|
79 | |
---|
80 | private final class PublishCollectionDialog extends ConfirmationDialog { |
---|
81 | private long vcId; |
---|
82 | |
---|
83 | public PublishCollectionDialog(String id, |
---|
84 | final Component updateComponenet) { |
---|
85 | super(id, updateComponenet); |
---|
86 | setInitialWidth(400); |
---|
87 | } |
---|
88 | |
---|
89 | @Override |
---|
90 | public void onConfirm(AjaxRequestTarget target) { |
---|
91 | try { |
---|
92 | final VirtualCollectionRegistry vcr = |
---|
93 | VirtualCollectionRegistry.instance(); |
---|
94 | vcr.setVirtualCollectionState(getUser(), vcId, |
---|
95 | State.PUBLIC_PENDING); |
---|
96 | } catch (VirtualCollectionRegistryException e) { |
---|
97 | e.printStackTrace(); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | public void show(AjaxRequestTarget target, VirtualCollection vc) { |
---|
102 | this.vcId = vc.getId(); |
---|
103 | super.show(target, |
---|
104 | new StringResourceModel("collections.publishconfirm", |
---|
105 | new Model<VirtualCollection>(vc))); |
---|
106 | } |
---|
107 | } // class BrowsePrivateCollectionsPage.PublishCollectionDialog |
---|
108 | |
---|
109 | private final class DeleteCollectionDialog extends ConfirmationDialog { |
---|
110 | private long vcId; |
---|
111 | |
---|
112 | public DeleteCollectionDialog(String id, |
---|
113 | final Component updateComponenet) { |
---|
114 | super(id, updateComponenet); |
---|
115 | setInitialWidth(400); |
---|
116 | } |
---|
117 | |
---|
118 | @Override |
---|
119 | public void onConfirm(AjaxRequestTarget target) { |
---|
120 | try { |
---|
121 | final VirtualCollectionRegistry vcr = |
---|
122 | VirtualCollectionRegistry.instance(); |
---|
123 | vcr.deleteVirtualCollection(getUser(), vcId); |
---|
124 | } catch (VirtualCollectionRegistryException e) { |
---|
125 | e.printStackTrace(); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | public void show(AjaxRequestTarget target, VirtualCollection vc) { |
---|
130 | this.vcId = vc.getId(); |
---|
131 | super.show(target, |
---|
132 | new StringResourceModel("collections.deleteconfirm", |
---|
133 | new Model<VirtualCollection>(vc))); |
---|
134 | } |
---|
135 | } // class BrowsePrivateCollectionsPage.PublishCollectionDialog |
---|
136 | |
---|
137 | private final PublishCollectionDialog publishDialog; |
---|
138 | private final DeleteCollectionDialog deleteDialog; |
---|
139 | |
---|
140 | public BrowsePrivateCollectionsPage() { |
---|
141 | super(); |
---|
142 | final VirtualCollectionTable table = |
---|
143 | new VirtualCollectionTable("collectionsTable", true) { |
---|
144 | @Override |
---|
145 | protected Panel createActionPanel(String componentId, |
---|
146 | IModel<VirtualCollection> model) { |
---|
147 | return new ActionsPanel(componentId, model); |
---|
148 | } |
---|
149 | }; |
---|
150 | add(table); |
---|
151 | |
---|
152 | publishDialog = new PublishCollectionDialog("publishCollectionDialog", |
---|
153 | table.getTable()); |
---|
154 | add(publishDialog); |
---|
155 | deleteDialog = new DeleteCollectionDialog("deleteCollectionDialog", |
---|
156 | table.getTable()); |
---|
157 | add(deleteDialog); |
---|
158 | } |
---|
159 | |
---|
160 | private void doEdit(AjaxRequestTarget target, |
---|
161 | VirtualCollection vc) { |
---|
162 | setResponsePage(new CreateVirtualCollectionPage(vc, getPage())); |
---|
163 | }; |
---|
164 | |
---|
165 | private void doPublish(AjaxRequestTarget target, |
---|
166 | VirtualCollection vc) { |
---|
167 | publishDialog.show(target, vc); |
---|
168 | } |
---|
169 | |
---|
170 | private void doDelete(AjaxRequestTarget target, |
---|
171 | VirtualCollection vc) { |
---|
172 | deleteDialog.show(target, vc); |
---|
173 | } |
---|
174 | private void doDetails(AjaxRequestTarget target, VirtualCollection vc) { |
---|
175 | setResponsePage(new VirtualCollectionDetailsPage(vc, getPage())); |
---|
176 | } |
---|
177 | |
---|
178 | private Principal getUser() { |
---|
179 | ApplicationSession session = (ApplicationSession) getSession(); |
---|
180 | Principal principal = session.getPrincipal(); |
---|
181 | if (principal == null) { |
---|
182 | throw new WicketRuntimeException("principal == null"); |
---|
183 | } |
---|
184 | return principal; |
---|
185 | } |
---|
186 | |
---|
187 | } // class BrowsePrivateCollectionsPage |
---|