001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: *
017: */
018: package org.apache.lenya.cms.metadata.usecases;
019:
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.lenya.cms.metadata.Element;
028: import org.apache.lenya.cms.metadata.MetaData;
029: import org.apache.lenya.cms.metadata.MetaDataRegistry;
030: import org.apache.lenya.cms.publication.Document;
031: import org.apache.lenya.cms.publication.Publication;
032: import org.apache.lenya.cms.repository.Node;
033: import org.apache.lenya.cms.site.usecases.SiteUsecase;
034: import org.apache.lenya.cms.usecase.UsecaseException;
035: import org.apache.lenya.cms.workflow.WorkflowUtil;
036: import org.apache.lenya.cms.workflow.usecases.UsecaseWorkflowHelper;
037:
038: /**
039: * Usecase to edit metadata for a resource.
040: *
041: * @version $Id: Metadata.java 598808 2007-11-27 23:07:33Z andreas $
042: */
043: public class Metadata extends SiteUsecase {
044:
045: /**
046: * @see org.apache.lenya.cms.usecase.AbstractUsecase#getNodesToLock()
047: */
048: protected Node[] getNodesToLock() throws UsecaseException {
049: Node[] objects = new Node[0];
050: if (getSourceDocument() != null) {
051: objects = new Node[] { getSourceDocument()
052: .getRepositoryNode() };
053: }
054: return objects;
055: }
056:
057: public static class MetaDataWrapper {
058:
059: private String[] values;
060: private Element element;
061:
062: public MetaDataWrapper(Element element, String[] values) {
063: this .values = values;
064: this .element = element;
065: }
066:
067: public String[] getValues() {
068: return this .values;
069: }
070:
071: public Element getElement() {
072: return this .element;
073: }
074:
075: }
076:
077: /**
078: * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
079: */
080: protected void initParameters() {
081: super .initParameters();
082:
083: if (getSourceDocument() == null) {
084: return;
085: }
086:
087: MetaDataRegistry registry = null;
088: try {
089: registry = (MetaDataRegistry) this .manager
090: .lookup(MetaDataRegistry.ROLE);
091:
092: List numbers = new ArrayList();
093: Map num2namespace = new HashMap();
094: List keyList = new ArrayList();
095:
096: String[] namespaces = registry.getNamespaceUris();
097:
098: for (int nsIndex = 0; nsIndex < namespaces.length; nsIndex++) {
099: MetaData meta = getSourceDocument().getMetaData(
100: namespaces[nsIndex]);
101: String[] keys = meta.getPossibleKeys();
102: for (int keyIndex = 0; keyIndex < keys.length; keyIndex++) {
103: String key = "ns" + nsIndex + "." + keys[keyIndex];
104: String[] values = meta.getValues(keys[keyIndex]);
105: Element element = meta.getElementSet().getElement(
106: keys[keyIndex]);
107: setParameter(key, new MetaDataWrapper(element,
108: values));
109: keyList.add(key);
110: }
111: numbers.add("" + nsIndex);
112: num2namespace.put("" + nsIndex, namespaces[nsIndex]);
113: }
114:
115: setParameter("numbers", numbers);
116: setParameter("namespaces", num2namespace);
117:
118: Collections.sort(keyList);
119: setParameter("keys", keyList);
120:
121: } catch (Exception e) {
122: getLogger().error("Unable to load meta data.", e);
123: addErrorMessage("Unable to load meta data: "
124: + e.getMessage());
125: } finally {
126: if (registry != null) {
127: this .manager.release(registry);
128: }
129: }
130:
131: }
132:
133: protected void doCheckPreconditions() throws Exception {
134: super .doCheckPreconditions();
135: Document doc = getSourceDocument();
136: if (doc == null) {
137: return;
138: }
139:
140: if (!doc.getArea().equals(Publication.AUTHORING_AREA)) {
141: addErrorMessage("This usecase can only be invoked in the authoring area!");
142: }
143: UsecaseWorkflowHelper.checkWorkflow(this .manager, this ,
144: getEvent(), doc, getLogger());
145: }
146:
147: /**
148: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
149: */
150: protected void doExecute() throws Exception {
151: super .doExecute();
152:
153: // we need a reverse lookup to get the correct ns index:
154: Map num2namespace = (Map) getParameter("namespaces");
155: Map namespace2num = new HashMap();
156:
157: Iterator iter = num2namespace.keySet().iterator();
158: while (iter.hasNext()) {
159: String key = (String) iter.next();
160: namespace2num.put(num2namespace.get(key), key);
161: }
162:
163: Document document = getSourceDocument();
164: String[] namespaces = document.getMetaDataNamespaceUris();
165:
166: for (int nsIndex = 0; nsIndex < namespaces.length; nsIndex++) {
167: MetaData meta = document.getMetaData(namespaces[nsIndex]);
168: String orgNsIndex = (String) namespace2num
169: .get(namespaces[nsIndex]);
170:
171: String[] keys = meta.getPossibleKeys();
172: for (int keyIndex = 0; keyIndex < keys.length; keyIndex++) {
173: String key = keys[keyIndex];
174: Element element = meta.getElementSet().getElement(key);
175: if (element.isEditable()) {
176: Object value = getParameter("ns" + orgNsIndex + "."
177: + key);
178: if (value != null && value instanceof String) {
179: meta.setValue(key, (String) value);
180: }
181: }
182: }
183: }
184:
185: WorkflowUtil.invoke(this .manager, getSession(), getLogger(),
186: document, getEvent());
187: }
188:
189: protected String getEvent() {
190: return "edit";
191: }
192:
193: }
|