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.migration;
019:
020: import java.util.Arrays;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import org.apache.avalon.framework.service.ServiceSelector;
026: import org.apache.excalibur.source.SourceResolver;
027: import org.apache.lenya.ac.impl.AbstractAccessControlTest;
028: import org.apache.lenya.cms.cocoon.source.SourceUtil;
029: import org.apache.lenya.cms.publication.Area;
030: import org.apache.lenya.cms.publication.Document;
031: import org.apache.lenya.cms.publication.DocumentException;
032: import org.apache.lenya.cms.publication.DocumentFactory;
033: import org.apache.lenya.cms.publication.DocumentManager;
034: import org.apache.lenya.cms.publication.DocumentUtil;
035: import org.apache.lenya.cms.publication.Publication;
036: import org.apache.lenya.cms.repository.Node;
037: import org.apache.lenya.cms.repository.RepositoryUtil;
038: import org.apache.lenya.cms.repository.Session;
039: import org.apache.lenya.cms.site.SiteException;
040: import org.apache.lenya.cms.site.SiteManager;
041: import org.apache.lenya.cms.site.SiteNode;
042: import org.apache.lenya.cms.site.SiteStructure;
043:
044: /**
045: * Migrate from path-based content to UUIDs.
046: */
047: public class MigrateUuidsTest extends AbstractAccessControlTest {
048:
049: /**
050: * Do the migration.
051: * @throws Exception
052: */
053: public void testMigrateUuids() throws Exception {
054:
055: Session session = login("lenya");
056: DocumentFactory factory = DocumentUtil.createDocumentFactory(
057: getManager(), session);
058: Publication[] pubs = factory.getPublications();
059: for (int i = 0; i < pubs.length; i++) {
060: this .migratedDocs.clear();
061: migratePublication(pubs[i]);
062: }
063: session.commit();
064: }
065:
066: private void migratePublication(Publication pub) throws Exception {
067: getLogger().info("Migrating publication [" + pub.getId() + "]");
068:
069: String[] areaNames = pub.getAreaNames();
070: if (areaNames != null) {
071: for (int i = 0; i < areaNames.length; i++) {
072: Area area = pub.getArea(areaNames[i]);
073: migrateArea(area);
074: }
075: }
076: }
077:
078: private void migrateArea(Area area) throws Exception {
079: getLogger().info("Migrating area [" + area + "]");
080:
081: SiteNode[] nodes = area.getSite().getNodes();
082:
083: Map path2langs = new HashMap();
084:
085: for (int n = 0; n < nodes.length; n++) {
086:
087: String uuid = nodes[n].getUuid();
088: if (uuid != null && uuid.startsWith("/")) {
089: String[] languages = nodes[n].getLanguages();
090: for (int l = 0; l < languages.length; l++) {
091: path2langs.put(nodes[n].getPath(), languages);
092: Document doc = area.getDocument(nodes[n].getPath(),
093: languages[l]);
094: migrateDocument(doc);
095: }
096: }
097: }
098: verifyMigration(area, path2langs);
099: }
100:
101: protected void verifyMigration(Area area, Map path2langs)
102: throws SiteException, DocumentException {
103: SiteStructure site = area.getSite();
104: for (Iterator i = path2langs.keySet().iterator(); i.hasNext();) {
105: String path = (String) i.next();
106: String[] langs = (String[]) path2langs.get(path);
107: SiteNode node = site.getNode(path);
108: Document migratedDoc = node.getLink(node.getLanguages()[0])
109: .getDocument();
110: String[] migratedLangs = migratedDoc.getLanguages();
111: assertEquals(Arrays.asList(langs), Arrays
112: .asList(migratedLangs));
113: }
114: }
115:
116: private Map migratedDocs = new HashMap();
117:
118: private void migrateDocument(Document doc) throws Exception {
119:
120: getLogger().info("Migrating document [" + doc + "]");
121:
122: DocumentManager docManager = null;
123: SiteManager siteManager = null;
124: ServiceSelector selector = null;
125: SourceResolver resolver = null;
126: try {
127: docManager = (DocumentManager) getManager().lookup(
128: DocumentManager.ROLE);
129: selector = (ServiceSelector) getManager().lookup(
130: SiteManager.ROLE + "Selector");
131: siteManager = (SiteManager) selector.select(doc
132: .getPublication().getSiteManagerHint());
133: resolver = (SourceResolver) getManager().lookup(
134: SourceResolver.ROLE);
135:
136: String path = doc.getPath();
137:
138: Node node = siteManager.getSiteStructure(doc.getFactory(),
139: doc.getPublication(), doc.getArea())
140: .getRepositoryNode();
141:
142: if (!node.isLocked()) {
143: node.lock();
144: }
145:
146: Document newDoc;
147: /*
148: String docId = doc.getUUID();
149: if (this.migratedDocs.containsKey(docId)) {
150: Document migratedDoc = (Document) this.migratedDocs.get(docId);
151: newDoc = docManager.addVersion(migratedDoc, doc.getArea(), doc.getLanguage(), false);
152: SourceUtil.copy(resolver, doc.getSourceURI(), newDoc.getSourceURI());
153: } else {
154: newDoc = docManager.add(doc.getFactory(),
155: doc.getResourceType(),
156: doc.getSourceURI(),
157: doc.getPublication(),
158: doc.getArea(),
159: doc.getLanguage(),
160: doc.getExtension());
161:
162: migratedDocs.put(docId, newDoc);
163: siteManager.set(path, newDoc);
164: }
165:
166: String[] uris = doc.getMetaDataNamespaceUris();
167: for (int i = 0; i < uris.length; i++) {
168: newDoc.getMetaData(uris[i]).replaceBy(doc.getMetaData(uris[i]));
169: }
170: */
171: doc.delete();
172:
173: } finally {
174: if (docManager != null) {
175: getManager().release(docManager);
176: }
177: if (selector != null) {
178: if (siteManager != null) {
179: selector.release(siteManager);
180: }
181: getManager().release(selector);
182: }
183: if (resolver != null) {
184: getManager().release(resolver);
185: }
186: }
187: }
188:
189: }
|