1 | package clarin.cmdi.componentregistry.frontend; |
---|
2 | |
---|
3 | import java.security.Principal; |
---|
4 | import java.util.List; |
---|
5 | |
---|
6 | import javax.swing.tree.DefaultMutableTreeNode; |
---|
7 | import javax.swing.tree.DefaultTreeModel; |
---|
8 | import javax.swing.tree.TreeModel; |
---|
9 | |
---|
10 | import org.apache.wicket.PageParameters; |
---|
11 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
12 | import org.apache.wicket.ajax.markup.html.form.AjaxFallbackButton; |
---|
13 | import org.apache.wicket.markup.html.basic.Label; |
---|
14 | import org.apache.wicket.markup.html.form.Button; |
---|
15 | import org.apache.wicket.markup.html.form.CheckBox; |
---|
16 | import org.apache.wicket.markup.html.form.Form; |
---|
17 | import org.apache.wicket.markup.html.form.TextArea; |
---|
18 | import org.apache.wicket.markup.html.link.Link; |
---|
19 | import org.apache.wicket.markup.html.panel.FeedbackPanel; |
---|
20 | import org.apache.wicket.markup.html.tree.BaseTree; |
---|
21 | import org.apache.wicket.markup.html.tree.ITreeState; |
---|
22 | import org.apache.wicket.markup.html.tree.LinkTree; |
---|
23 | import org.apache.wicket.model.CompoundPropertyModel; |
---|
24 | import org.apache.wicket.spring.injection.annot.SpringBean; |
---|
25 | import org.slf4j.Logger; |
---|
26 | import org.slf4j.LoggerFactory; |
---|
27 | |
---|
28 | import clarin.cmdi.componentregistry.ComponentRegistry; |
---|
29 | import clarin.cmdi.componentregistry.ComponentRegistryException; |
---|
30 | import clarin.cmdi.componentregistry.ComponentRegistryFactory; |
---|
31 | import clarin.cmdi.componentregistry.ComponentStatus; |
---|
32 | import clarin.cmdi.componentregistry.impl.database.AdminRegistry; |
---|
33 | import clarin.cmdi.componentregistry.impl.database.ComponentDescriptionDao; |
---|
34 | import clarin.cmdi.componentregistry.impl.database.ProfileDescriptionDao; |
---|
35 | import clarin.cmdi.componentregistry.model.AbstractDescription; |
---|
36 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
37 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
38 | |
---|
39 | @SuppressWarnings("serial") |
---|
40 | public class AdminHomePage extends SecureAdminWebPage { |
---|
41 | |
---|
42 | private static final long serialVersionUID = 1L; |
---|
43 | private final static Logger LOG = LoggerFactory.getLogger(AdminHomePage.class); |
---|
44 | private final CMDItemInfo info = new CMDItemInfo(); |
---|
45 | private final LinkTree tree; |
---|
46 | private transient AdminRegistry adminRegistry = new AdminRegistry(); |
---|
47 | @SpringBean(name = "componentRegistryFactory") |
---|
48 | private ComponentRegistryFactory componentRegistryFactory; |
---|
49 | @SpringBean |
---|
50 | private ProfileDescriptionDao profileDescriptionDao; |
---|
51 | @SpringBean |
---|
52 | private ComponentDescriptionDao componentDescriptionDao; |
---|
53 | |
---|
54 | public AdminHomePage(final PageParameters parameters) throws ComponentRegistryException { |
---|
55 | super(parameters); |
---|
56 | adminRegistry.setComponentRegistryFactory(componentRegistryFactory); |
---|
57 | adminRegistry.setProfileDescriptionDao(profileDescriptionDao); |
---|
58 | adminRegistry.setComponentDescriptionDao(componentDescriptionDao); |
---|
59 | addLinks(); |
---|
60 | final FeedbackPanel feedback = new FeedbackPanel("feedback"); |
---|
61 | feedback.setOutputMarkupId(true); |
---|
62 | add(feedback); |
---|
63 | Form form = new ItemEditForm("form"); |
---|
64 | add(form); |
---|
65 | |
---|
66 | Button deleteButton = new AjaxFallbackButton("delete", form) { |
---|
67 | |
---|
68 | @Override |
---|
69 | protected void onSubmit(AjaxRequestTarget target, Form<?> form) { |
---|
70 | CMDItemInfo info = (CMDItemInfo) form.getModelObject(); |
---|
71 | info("deleting:" + info.getName()); |
---|
72 | Principal userPrincipal = getWebRequest().getHttpServletRequest().getUserPrincipal(); |
---|
73 | try { |
---|
74 | adminRegistry.delete(info, userPrincipal); |
---|
75 | info("Item deleted."); |
---|
76 | reloadTreeModel(info); |
---|
77 | } catch (SubmitFailedException e) { |
---|
78 | LOG.error("Admin: ", e); |
---|
79 | error("Cannot delete: " + info.getName() + "\n error=" + e); |
---|
80 | } |
---|
81 | if (target != null) { |
---|
82 | target.addComponent(form); |
---|
83 | target.addComponent(tree); |
---|
84 | target.addComponent(feedback); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | @Override |
---|
89 | public boolean isEnabled() { |
---|
90 | return info.isDeletable(); |
---|
91 | |
---|
92 | } |
---|
93 | ; |
---|
94 | }; |
---|
95 | form.add(deleteButton); |
---|
96 | |
---|
97 | Button undeleteButton = new AjaxFallbackButton("undelete", form) { |
---|
98 | |
---|
99 | @Override |
---|
100 | protected void onSubmit(AjaxRequestTarget target, Form<?> form) { |
---|
101 | CMDItemInfo info = (CMDItemInfo) form.getModelObject(); |
---|
102 | info("undeleting:" + info.getName()); |
---|
103 | try { |
---|
104 | adminRegistry.undelete(info); |
---|
105 | info("Item put back."); |
---|
106 | reloadTreeModel(info); |
---|
107 | } catch (SubmitFailedException e) { |
---|
108 | LOG.error("Admin: ", e); |
---|
109 | error("Cannot undelete: " + info.getName() + "\n error=" + e); |
---|
110 | } |
---|
111 | if (target != null) { |
---|
112 | target.addComponent(form); |
---|
113 | target.addComponent(tree); |
---|
114 | target.addComponent(feedback); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | @Override |
---|
119 | public boolean isEnabled() { |
---|
120 | return info.isUndeletable(); |
---|
121 | } |
---|
122 | }; |
---|
123 | form.add(undeleteButton); |
---|
124 | |
---|
125 | CheckBox forceUpdateCheck = new CheckBox("forceUpdate"); |
---|
126 | form.add(forceUpdateCheck); |
---|
127 | |
---|
128 | Button submitButton = new AjaxFallbackButton("submit", form) { |
---|
129 | |
---|
130 | @Override |
---|
131 | protected void onSubmit(AjaxRequestTarget target, Form<?> form) { |
---|
132 | CMDItemInfo info = (CMDItemInfo) form.getModelObject(); |
---|
133 | Principal userPrincipal = getWebRequest().getHttpServletRequest().getUserPrincipal(); |
---|
134 | info("submitting:" + info.getName() + " id=(" + info.getDataNode().getDescription().getId() + ")"); |
---|
135 | try { |
---|
136 | adminRegistry.submitFile(info, userPrincipal); |
---|
137 | info("submitting done."); |
---|
138 | reloadTreeModel(info); |
---|
139 | } catch (SubmitFailedException e) { |
---|
140 | LOG.error("Admin: ", e); |
---|
141 | error("Cannot submit: " + info.getName() + "\n error=" + e); |
---|
142 | } |
---|
143 | if (target != null) { |
---|
144 | target.addComponent(form); |
---|
145 | target.addComponent(tree); |
---|
146 | target.addComponent(feedback); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | @Override |
---|
151 | public boolean isEnabled() { |
---|
152 | return info.isEditable(); |
---|
153 | } |
---|
154 | }; |
---|
155 | form.add(submitButton); |
---|
156 | |
---|
157 | tree = createTree("tree", form, createDBTreeModel()); |
---|
158 | add(tree); |
---|
159 | add(new Link("expandAll") { |
---|
160 | |
---|
161 | @Override |
---|
162 | public void onClick() { |
---|
163 | tree.getTreeState().expandAll(); |
---|
164 | } |
---|
165 | }); |
---|
166 | |
---|
167 | add(new Link("collapseAll") { |
---|
168 | |
---|
169 | @Override |
---|
170 | public void onClick() { |
---|
171 | tree.getTreeState().collapseAll(); |
---|
172 | } |
---|
173 | }); |
---|
174 | |
---|
175 | } |
---|
176 | |
---|
177 | private void reloadTreeModel(CMDItemInfo info) { |
---|
178 | try { |
---|
179 | tree.setModelObject(createDBTreeModel()); |
---|
180 | } catch (ComponentRegistryException e) { |
---|
181 | LOG.error("Admin: ", e); |
---|
182 | error("Cannot reload tree: " + info.getName() + "\n error=" + e); |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | private void addLinks() { |
---|
187 | add(new Label("linksMessage", "Browse the data below or choose on of the following options:")); |
---|
188 | add(new Link("massMigrate") { |
---|
189 | |
---|
190 | @Override |
---|
191 | public void onClick() { |
---|
192 | setResponsePage(MassMigratePage.class); |
---|
193 | } |
---|
194 | }); |
---|
195 | add(new Link("log") { |
---|
196 | |
---|
197 | @Override |
---|
198 | public void onClick() { |
---|
199 | setResponsePage(ViewLogPage.class); |
---|
200 | } |
---|
201 | }); |
---|
202 | add(new Link("statistics") { |
---|
203 | |
---|
204 | @Override |
---|
205 | public void onClick() { |
---|
206 | setResponsePage(StatisticsPage.class); |
---|
207 | } |
---|
208 | }); |
---|
209 | } |
---|
210 | |
---|
211 | private LinkTree createTree(String id, final Form form, TreeModel treeModel) { |
---|
212 | final LinkTree adminTree = new LinkTree(id, treeModel) { |
---|
213 | |
---|
214 | @Override |
---|
215 | protected void onNodeLinkClicked(Object node, BaseTree tree, AjaxRequestTarget target) { |
---|
216 | super.onNodeLinkClicked(node, tree, target); |
---|
217 | ITreeState treeState = tree.getTreeState(); |
---|
218 | if (treeState.isNodeExpanded(node)) { |
---|
219 | treeState.collapseNode(node); |
---|
220 | } else { |
---|
221 | treeState.expandNode(node); |
---|
222 | } |
---|
223 | DisplayDataNode dn = (DisplayDataNode) ((DefaultMutableTreeNode) node).getUserObject(); |
---|
224 | info.setDataNode(dn); |
---|
225 | AbstractDescription desc = dn.getDescription(); |
---|
226 | if (desc != null) { |
---|
227 | String content; |
---|
228 | if (desc.isProfile()) { |
---|
229 | content = profileDescriptionDao.getContent(dn.isDeleted(), desc.getId()); |
---|
230 | } else { |
---|
231 | content = componentDescriptionDao.getContent(dn.isDeleted(), desc.getId()); |
---|
232 | } |
---|
233 | info.setContent(content); |
---|
234 | } |
---|
235 | if (target != null) { |
---|
236 | target.addComponent(form); |
---|
237 | } |
---|
238 | } |
---|
239 | }; |
---|
240 | return adminTree; |
---|
241 | } |
---|
242 | |
---|
243 | private class ItemEditForm extends Form<CMDItemInfo> { |
---|
244 | |
---|
245 | public ItemEditForm(String name) { |
---|
246 | super(name); |
---|
247 | CompoundPropertyModel model = new CompoundPropertyModel(info); |
---|
248 | setModel(model); |
---|
249 | |
---|
250 | TextArea descriptionArea = new TextArea("description"); |
---|
251 | descriptionArea.setOutputMarkupId(true); |
---|
252 | add(descriptionArea); |
---|
253 | TextArea contentArea = new TextArea("content"); |
---|
254 | contentArea.setOutputMarkupId(true); |
---|
255 | add(contentArea); |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | private TreeModel createDBTreeModel() throws ComponentRegistryException { |
---|
260 | DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(new DisplayDataNode("ComponentRegistry", false)); |
---|
261 | DefaultMutableTreeNode publicNode = new DefaultMutableTreeNode(new DisplayDataNode("Public", false)); |
---|
262 | rootNode.add(publicNode); |
---|
263 | ComponentRegistry publicRegistry = componentRegistryFactory.getPublicRegistry(); |
---|
264 | add(publicNode, publicRegistry); |
---|
265 | List<ComponentRegistry> userRegistries = componentRegistryFactory.getAllUserRegistries(); |
---|
266 | for (ComponentRegistry registry : userRegistries) { |
---|
267 | DefaultMutableTreeNode userNode = new DefaultMutableTreeNode(new DisplayDataNode(registry.getName(), false)); |
---|
268 | rootNode.add(userNode); |
---|
269 | add(userNode, registry); |
---|
270 | } |
---|
271 | TreeModel model = new DefaultTreeModel(rootNode); |
---|
272 | return model; |
---|
273 | } |
---|
274 | |
---|
275 | private void add(DefaultMutableTreeNode parent, ComponentRegistry registry) throws ComponentRegistryException { |
---|
276 | DefaultMutableTreeNode componentsNode = new DefaultMutableTreeNode(new DisplayDataNode("Components", false)); |
---|
277 | parent.add(componentsNode); |
---|
278 | add(componentsNode, registry.getComponentDescriptions(), false, registry.getStatus()); |
---|
279 | |
---|
280 | DefaultMutableTreeNode profilesNode = new DefaultMutableTreeNode(new DisplayDataNode("Profiles", false)); |
---|
281 | parent.add(profilesNode); |
---|
282 | add(profilesNode, registry.getProfileDescriptions(), false, registry.getStatus()); |
---|
283 | |
---|
284 | DefaultMutableTreeNode deletedCompNode = new DefaultMutableTreeNode(new DisplayDataNode("Deleted Components", true)); |
---|
285 | parent.add(deletedCompNode); |
---|
286 | |
---|
287 | List<ComponentDescription> deletedComponentDescriptions = registry.getDeletedComponentDescriptions(); |
---|
288 | add(deletedCompNode, deletedComponentDescriptions, true, registry.getStatus()); |
---|
289 | |
---|
290 | DefaultMutableTreeNode deletedProfNode = new DefaultMutableTreeNode(new DisplayDataNode("Deleted Profiles", true)); |
---|
291 | parent.add(deletedProfNode); |
---|
292 | List<ProfileDescription> deletedProfileDescriptions = registry.getDeletedProfileDescriptions(); |
---|
293 | add(deletedProfNode, deletedProfileDescriptions, true, registry.getStatus()); |
---|
294 | } |
---|
295 | |
---|
296 | private void add(DefaultMutableTreeNode parent, List<? extends AbstractDescription> descs, boolean isDeleted, ComponentStatus status) { |
---|
297 | for (AbstractDescription desc : descs) { |
---|
298 | DefaultMutableTreeNode child = new DefaultMutableTreeNode(new DisplayDataNode(desc.getName(), isDeleted, desc, status)); |
---|
299 | parent.add(child); |
---|
300 | } |
---|
301 | } |
---|
302 | } |
---|