01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.lenya.cms.site.usecases;
19:
20: import java.util.Arrays;
21:
22: import org.apache.lenya.cms.usecase.UsecaseResolver;
23: import org.apache.lenya.xml.DocumentHelper;
24: import org.apache.lenya.xml.NamespaceHelper;
25: import org.w3c.dom.Element;
26:
27: /**
28: * <p>
29: * Create a usecase document. The document content looks like this:
30: * </p>
31: *
32: * <pre>
33: * <usecase name="..."/>
34: * </pre>
35: */
36: public class CreateUsecaseDocument extends CreateDocument {
37:
38: protected static final String USECASE = "usecase";
39: protected static final String RESOURCE_TYPE_USECASE = "usecase";
40:
41: /**
42: * The namespace for usecase document content.
43: */
44: public static final String NAMESPACE = "http://apache.org/lenya/usecase/1.0";
45:
46: /**
47: * The local name of the usecase XML element.
48: */
49: public static final String ELEMENT_USECASE = "usecase";
50:
51: /**
52: * The local name of the name attribute.
53: */
54: public static final String ATTRIBUTE_NAME = "name";
55:
56: protected void initParameters() {
57: super .initParameters();
58: setParameter(DOCUMENT_TYPE, RESOURCE_TYPE_USECASE);
59: setParameter(RESOURCE_TYPES, Arrays.asList(new String[0]));
60: }
61:
62: protected void doCheckExecutionConditions() throws Exception {
63: super .doCheckExecutionConditions();
64:
65: String usecaseName = getParameterAsString(USECASE);
66: if (usecaseName.equals("")) {
67: addErrorMessage("Please enter a usecase name.");
68: } else {
69: UsecaseResolver resolver = null;
70: try {
71: resolver = (UsecaseResolver) this .manager
72: .lookup(UsecaseResolver.ROLE);
73: if (!resolver.isRegistered(getSourceURL(), usecaseName)) {
74: addErrorMessage("The usecase '" + usecaseName
75: + "' is not registered.");
76: }
77: } finally {
78: if (resolver != null) {
79: this .manager.release(resolver);
80: }
81: }
82: }
83: }
84:
85: protected void doExecute() throws Exception {
86: super .doExecute();
87:
88: NamespaceHelper helper = new NamespaceHelper(NAMESPACE, "",
89: ELEMENT_USECASE);
90: Element usecaseElement = helper.getDocument()
91: .getDocumentElement();
92: usecaseElement.setAttribute(ATTRIBUTE_NAME,
93: getParameterAsString(USECASE));
94: DocumentHelper.writeDocument(helper.getDocument(),
95: getNewDocument().getOutputStream());
96:
97: }
98:
99: }
|