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,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.site.usecases;
019:
020: import java.util.ArrayList;
021: import java.util.Arrays;
022: import java.util.List;
023:
024: import org.apache.avalon.framework.service.ServiceException;
025: import org.apache.avalon.framework.service.ServiceSelector;
026: import org.apache.lenya.cms.publication.Document;
027: import org.apache.lenya.cms.publication.DocumentFactory;
028: import org.apache.lenya.cms.publication.DocumentManager;
029: import org.apache.lenya.cms.publication.Publication;
030: import org.apache.lenya.cms.publication.PublicationException;
031: import org.apache.lenya.cms.publication.PublicationUtil;
032: import org.apache.lenya.cms.publication.util.DocumentSet;
033: import org.apache.lenya.cms.site.SiteException;
034: import org.apache.lenya.cms.site.SiteManager;
035: import org.apache.lenya.cms.site.SiteStructure;
036: import org.apache.lenya.cms.usecase.AbstractUsecase;
037: import org.apache.lenya.cms.usecase.UsecaseException;
038:
039: /**
040: * Empty the trash.
041: *
042: * @version $Id: EmptyTrash.java 473861 2006-11-12 03:51:14Z gregor $
043: */
044: public class EmptyTrash extends AbstractUsecase {
045:
046: protected static final String DOCUMENTS = "documents";
047:
048: /**
049: * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
050: */
051: protected void initParameters() {
052: super .initParameters();
053: try {
054: Document[] documents = getTrashDocuments();
055: setParameter(DOCUMENTS, Arrays.asList(documents));
056: } catch (SiteException e) {
057: throw new RuntimeException(e);
058: } catch (PublicationException e) {
059: throw new RuntimeException(e);
060: }
061: }
062:
063: /**
064: * Lock the following objects:
065: * <ul>
066: * <li>all involved documents in the trash area</li>
067: * <li>the document trash site structure</li>
068: * </ul>
069: * @see org.apache.lenya.cms.usecase.AbstractUsecase#getNodesToLock()
070: */
071: protected org.apache.lenya.cms.repository.Node[] getNodesToLock()
072: throws UsecaseException {
073: List nodes = new ArrayList();
074: ServiceSelector selector = null;
075: SiteManager siteManager = null;
076:
077: try {
078: Publication publication = PublicationUtil
079: .getPublicationFromUrl(this .manager,
080: getDocumentFactory(), getSourceURL());
081: DocumentFactory identityMap = getDocumentFactory();
082: Document[] docs = getTrashDocuments();
083: for (int i = 0; i < docs.length; i++) {
084: nodes.add(docs[i].getRepositoryNode());
085: }
086:
087: selector = (ServiceSelector) this .manager
088: .lookup(SiteManager.ROLE + "Selector");
089: siteManager = (SiteManager) selector.select(publication
090: .getSiteManagerHint());
091: SiteStructure structure = siteManager.getSiteStructure(
092: identityMap, publication, Publication.TRASH_AREA);
093: nodes.add(structure.getRepositoryNode());
094: } catch (Exception e) {
095: throw new UsecaseException(e);
096: } finally {
097: if (selector != null) {
098: if (siteManager != null) {
099: selector.release(siteManager);
100: }
101: this .manager.release(selector);
102: }
103: }
104: return (org.apache.lenya.cms.repository.Node[]) nodes
105: .toArray(new org.apache.lenya.cms.repository.Node[nodes
106: .size()]);
107: }
108:
109: /**
110: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
111: */
112: protected void doExecute() throws Exception {
113: super .doExecute();
114:
115: DocumentManager documentManager = null;
116: try {
117: documentManager = (DocumentManager) this .manager
118: .lookup(DocumentManager.ROLE);
119: Document[] documents = getTrashDocuments();
120: DocumentSet set = new DocumentSet(documents);
121: documentManager.delete(set);
122: } finally {
123: if (documentManager != null) {
124: this .manager.release(documentManager);
125: }
126: }
127: }
128:
129: /**
130: * @return The documents in the trash area.
131: * @throws PublicationException if an error occurs.
132: * @throws SiteException if an error occurs.
133: */
134: protected Document[] getTrashDocuments()
135: throws PublicationException, SiteException {
136: Publication publication = PublicationUtil
137: .getPublicationFromUrl(this .manager,
138: getDocumentFactory(), getSourceURL());
139: DocumentFactory identityMap = getDocumentFactory();
140: Document[] documents;
141:
142: ServiceSelector selector = null;
143: SiteManager siteManager = null;
144: try {
145: selector = (ServiceSelector) this .manager
146: .lookup(SiteManager.ROLE + "Selector");
147: siteManager = (SiteManager) selector.select(publication
148: .getSiteManagerHint());
149: documents = siteManager.getDocuments(identityMap,
150: publication, Publication.TRASH_AREA);
151: } catch (ServiceException e) {
152: throw new RuntimeException(e);
153: } finally {
154: if (selector != null) {
155: if (siteManager != null) {
156: selector.release(siteManager);
157: }
158: this.manager.release(selector);
159: }
160: }
161:
162: return documents;
163: }
164: }
|