001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.frontend.admin;
017:
018: import org.apache.cocoon.components.flow.apples.StatelessAppleController;
019: import org.apache.cocoon.components.flow.apples.AppleRequest;
020: import org.apache.cocoon.components.flow.apples.AppleResponse;
021: import org.apache.cocoon.ResourceNotFoundException;
022: import org.apache.cocoon.environment.Request;
023: import org.apache.excalibur.xml.sax.XMLizable;
024: import org.outerj.daisy.frontend.util.XmlObjectXMLizable;
025: import org.outerj.daisy.frontend.util.AbstractDaisyApple;
026: import org.outerj.daisy.frontend.util.MultiXMLizable;
027: import org.outerj.daisy.frontend.RequestUtil;
028: import org.outerj.daisy.repository.Repository;
029: import org.outerj.daisy.workflow.WorkflowManager;
030: import org.outerj.daisy.workflow.WfListHelper;
031:
032: import java.util.Map;
033: import java.util.HashMap;
034: import java.util.Locale;
035:
036: public class AdminApple extends AbstractDaisyApple implements
037: StatelessAppleController {
038: private static Map<String, EntityHandler> ENTITY_HANDLERS;
039: static {
040: ENTITY_HANDLERS = new HashMap<String, EntityHandler>();
041: ENTITY_HANDLERS.put(DocumentTypeHandler.NAME,
042: new DocumentTypeHandler());
043: ENTITY_HANDLERS.put(FieldTypeHandler.NAME,
044: new FieldTypeHandler());
045: ENTITY_HANDLERS
046: .put(PartTypeHandler.NAME, new PartTypeHandler());
047: ENTITY_HANDLERS.put(UserHandler.NAME, new UserHandler());
048: ENTITY_HANDLERS.put(RoleHandler.NAME, new RoleHandler());
049: ENTITY_HANDLERS.put(BranchHandler.NAME, new BranchHandler());
050: ENTITY_HANDLERS
051: .put(LanguageHandler.NAME, new LanguageHandler());
052: ENTITY_HANDLERS.put(CollectionHandler.NAME,
053: new CollectionHandler());
054: ENTITY_HANDLERS.put(NamespacesHandler.NAME,
055: new NamespacesHandler());
056: ENTITY_HANDLERS.put(WfProcessDefinitionHandler.NAME,
057: new WfProcessDefinitionHandler());
058: ENTITY_HANDLERS.put(WfPoolHandler.NAME, new WfPoolHandler());
059: }
060:
061: protected void processRequest(AppleRequest appleRequest,
062: final AppleResponse appleResponse) throws Exception {
063: Repository repository = frontEndContext.getRepository();
064: Locale locale = frontEndContext.getLocale();
065:
066: String resource = appleRequest.getSitemapParameter("resource");
067: EntityHandler entityHandler = ENTITY_HANDLERS.get(resource);
068: if (entityHandler == null)
069: throw new ResourceNotFoundException(
070: "Unknown admin resource: " + resource);
071:
072: String method = request.getMethod();
073: if (method.equals("POST")) {
074: String action = RequestUtil.getStringParameter(request,
075: "action");
076:
077: if (action.equals("delete")) {
078: String idParam = RequestUtil.getStringParameter(
079: request, "id");
080: long id = Long.parseLong(idParam);
081: entityHandler.deleteEntity(id, repository);
082: } else {
083: boolean handled = entityHandler.handleAction(action,
084: request, repository);
085: if (!handled)
086: throw new Exception("Unsupported action: " + action);
087: }
088: }
089:
090: String sortKey = request.getParameter("sortKey");
091: if (sortKey == null)
092: sortKey = entityHandler.getDefaultSortKey();
093: String sortOrder = request.getParameter("sortOrder");
094: if (sortOrder == null
095: || !(sortOrder.equals("asc") || sortOrder
096: .equals("desc")))
097: sortOrder = "asc";
098:
099: // show list of the entities
100: Map<String, Object> viewData = new HashMap<String, Object>();
101: viewData.put("pageXml", entityHandler.getEntityList(repository,
102: locale));
103: viewData.put("pageContext", frontEndContext.getPageContext());
104: viewData.put("entityName", entityHandler.getEntityName()
105: .toLowerCase());
106: viewData.put("sortKey", sortKey);
107: viewData.put("sortOrder", sortOrder);
108: appleResponse.sendPage("EntitiesPipe", viewData);
109: }
110:
111: interface EntityHandler {
112: String getEntityName();
113:
114: String getDefaultSortKey();
115:
116: void deleteEntity(long id, Repository repository)
117: throws Exception;
118:
119: /**
120: * @return true if the action has been handled
121: */
122: boolean handleAction(String action, Request request,
123: Repository repository) throws Exception;
124:
125: XMLizable getEntityList(Repository repository, Locale locale)
126: throws Exception;
127: }
128:
129: public static class DocumentTypeHandler implements EntityHandler {
130: public static final String NAME = "documentType";
131:
132: public String getEntityName() {
133: return NAME;
134: }
135:
136: public String getDefaultSortKey() {
137: return "name";
138: }
139:
140: public void deleteEntity(long id, Repository repository)
141: throws Exception {
142: repository.getRepositorySchema().deleteDocumentType(id);
143: }
144:
145: public boolean handleAction(String action, Request request,
146: Repository repository) throws Exception {
147: return false;
148: }
149:
150: public XMLizable getEntityList(Repository repository,
151: Locale locale) throws Exception {
152: return new XmlObjectXMLizable(repository
153: .getRepositorySchema().getAllDocumentTypes(false)
154: .getXml());
155: }
156: }
157:
158: public static class PartTypeHandler implements EntityHandler {
159: public static final String NAME = "partType";
160:
161: public String getEntityName() {
162: return NAME;
163: }
164:
165: public String getDefaultSortKey() {
166: return "name";
167: }
168:
169: public void deleteEntity(long id, Repository repository)
170: throws Exception {
171: repository.getRepositorySchema().deletePartType(id);
172: }
173:
174: public boolean handleAction(String action, Request request,
175: Repository repository) throws Exception {
176: return false;
177: }
178:
179: public XMLizable getEntityList(Repository repository,
180: Locale locale) throws Exception {
181: return new XmlObjectXMLizable(repository
182: .getRepositorySchema().getAllPartTypes(false)
183: .getXml());
184: }
185: }
186:
187: public static class FieldTypeHandler implements EntityHandler {
188: public static final String NAME = "fieldType";
189:
190: public String getEntityName() {
191: return NAME;
192: }
193:
194: public String getDefaultSortKey() {
195: return "name";
196: }
197:
198: public void deleteEntity(long id, Repository repository)
199: throws Exception {
200: repository.getRepositorySchema().deleteFieldType(id);
201: }
202:
203: public boolean handleAction(String action, Request request,
204: Repository repository) throws Exception {
205: return false;
206: }
207:
208: public XMLizable getEntityList(Repository repository,
209: Locale locale) throws Exception {
210: return new XmlObjectXMLizable(repository
211: .getRepositorySchema().getAllFieldTypes(false)
212: .getXml());
213: }
214: }
215:
216: public static class CollectionHandler implements EntityHandler {
217: public static final String NAME = "collection";
218:
219: public String getEntityName() {
220: return NAME;
221: }
222:
223: public String getDefaultSortKey() {
224: return "name";
225: }
226:
227: public void deleteEntity(long id, Repository repository)
228: throws Exception {
229: repository.getCollectionManager().deleteCollection(id);
230: }
231:
232: public boolean handleAction(String action, Request request,
233: Repository repository) throws Exception {
234: return false;
235: }
236:
237: public XMLizable getEntityList(Repository repository,
238: Locale locale) throws Exception {
239: return new XmlObjectXMLizable(repository
240: .getCollectionManager().getCollections(false)
241: .getXml());
242: }
243: }
244:
245: public static class RoleHandler implements EntityHandler {
246: public static final String NAME = "role";
247:
248: public String getEntityName() {
249: return NAME;
250: }
251:
252: public String getDefaultSortKey() {
253: return "name";
254: }
255:
256: public void deleteEntity(long id, Repository repository)
257: throws Exception {
258: repository.getUserManager().deleteRole(id);
259: }
260:
261: public boolean handleAction(String action, Request request,
262: Repository repository) throws Exception {
263: return false;
264: }
265:
266: public XMLizable getEntityList(Repository repository,
267: Locale locale) throws Exception {
268: return new XmlObjectXMLizable(repository.getUserManager()
269: .getRoles().getXml());
270: }
271: }
272:
273: public static class UserHandler implements EntityHandler {
274: public static final String NAME = "user";
275:
276: public String getEntityName() {
277: return NAME;
278: }
279:
280: public String getDefaultSortKey() {
281: return "login";
282: }
283:
284: public void deleteEntity(long id, Repository repository)
285: throws Exception {
286: repository.getUserManager().deleteUser(id);
287: }
288:
289: public boolean handleAction(String action, Request request,
290: Repository repository) throws Exception {
291: return false;
292: }
293:
294: public XMLizable getEntityList(Repository repository,
295: Locale locale) throws Exception {
296: return new XmlObjectXMLizable(repository.getUserManager()
297: .getUsers().getXml());
298: }
299: }
300:
301: public static class BranchHandler implements EntityHandler {
302: public static final String NAME = "branch";
303:
304: public String getEntityName() {
305: return NAME;
306: }
307:
308: public String getDefaultSortKey() {
309: return "name";
310: }
311:
312: public void deleteEntity(long id, Repository repository)
313: throws Exception {
314: repository.getVariantManager().deleteBranch(id);
315: }
316:
317: public boolean handleAction(String action, Request request,
318: Repository repository) throws Exception {
319: return false;
320: }
321:
322: public XMLizable getEntityList(Repository repository,
323: Locale locale) throws Exception {
324: return new XmlObjectXMLizable(repository
325: .getVariantManager().getAllBranches(false).getXml());
326: }
327: }
328:
329: public static class LanguageHandler implements EntityHandler {
330: public static final String NAME = "language";
331:
332: public String getEntityName() {
333: return NAME;
334: }
335:
336: public String getDefaultSortKey() {
337: return "name";
338: }
339:
340: public boolean handleAction(String action, Request request,
341: Repository repository) throws Exception {
342: return false;
343: }
344:
345: public void deleteEntity(long id, Repository repository)
346: throws Exception {
347: repository.getVariantManager().deleteLanguage(id);
348: }
349:
350: public XMLizable getEntityList(Repository repository,
351: Locale locale) throws Exception {
352: return new XmlObjectXMLizable(repository
353: .getVariantManager().getAllLanguages(false)
354: .getXml());
355: }
356: }
357:
358: public static class NamespacesHandler implements EntityHandler {
359: public static final String NAME = "namespace";
360:
361: public String getEntityName() {
362: return NAME;
363: }
364:
365: public String getDefaultSortKey() {
366: return "name";
367: }
368:
369: public void deleteEntity(long id, Repository repository)
370: throws Exception {
371: repository.getNamespaceManager().unregisterNamespace(id);
372: }
373:
374: public boolean handleAction(String action, Request request,
375: Repository repository) throws Exception {
376: return false;
377: }
378:
379: public XMLizable getEntityList(Repository repository,
380: Locale locale) throws Exception {
381: return new XmlObjectXMLizable(repository
382: .getNamespaceManager().getAllNamespaces().getXml());
383: }
384: }
385:
386: public static class WfProcessDefinitionHandler implements
387: EntityHandler {
388: public static final String NAME = "wfProcessDefinition";
389:
390: public String getEntityName() {
391: return NAME;
392: }
393:
394: public String getDefaultSortKey() {
395: return "name";
396: }
397:
398: public void deleteEntity(long id, Repository repository)
399: throws Exception {
400: WorkflowManager workflowManager = (WorkflowManager) repository
401: .getExtension("WorkflowManager");
402: workflowManager.deleteProcessDefinition(String.valueOf(id));
403: }
404:
405: public boolean handleAction(String action, Request request,
406: Repository repository) throws Exception {
407: if ("loadSamples".equals(action)) {
408: WorkflowManager workflowManager = (WorkflowManager) repository
409: .getExtension("WorkflowManager");
410: workflowManager.loadSampleWorkflows();
411: return true;
412: }
413: return false;
414: }
415:
416: public XMLizable getEntityList(Repository repository,
417: Locale locale) throws Exception {
418: WorkflowManager workflowManager = (WorkflowManager) repository
419: .getExtension("WorkflowManager");
420:
421: Map<String, Integer> instanceCounts = workflowManager
422: .getProcessInstanceCounts();
423: XMLizable instanceCountsXml = new XmlObjectXMLizable(
424: WfListHelper
425: .getProcessInstanceCountsAsXml(instanceCounts));
426: XMLizable processDefinitionsXml = new XmlObjectXMLizable(
427: WfListHelper
428: .getProcessDefinitionsAsXml(workflowManager
429: .getAllProcessDefinitions(locale)));
430:
431: return new MultiXMLizable(instanceCountsXml,
432: processDefinitionsXml);
433: }
434: }
435:
436: public static class WfPoolHandler implements EntityHandler {
437: public static final String NAME = "wfPool";
438:
439: public String getEntityName() {
440: return NAME;
441: }
442:
443: public String getDefaultSortKey() {
444: return "name";
445: }
446:
447: public void deleteEntity(long id, Repository repository)
448: throws Exception {
449: WorkflowManager workflowManager = (WorkflowManager) repository
450: .getExtension("WorkflowManager");
451: workflowManager.getPoolManager().deletePool(id);
452: }
453:
454: public boolean handleAction(String action, Request request,
455: Repository repository) throws Exception {
456: return false;
457: }
458:
459: public XMLizable getEntityList(Repository repository,
460: Locale locale) throws Exception {
461: WorkflowManager workflowManager = (WorkflowManager) repository
462: .getExtension("WorkflowManager");
463: return new XmlObjectXMLizable(WfListHelper
464: .getPoolsAsXml(workflowManager.getPoolManager()
465: .getPools()));
466: }
467: }
468: }
|