KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > serverimpl > linkextraction > LinkExtractorHelper


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository.serverimpl.linkextraction;
17
18 import org.outerj.daisy.repository.*;
19 import org.outerj.daisy.repository.serverimpl.LocalRepositoryManager;
20 import org.outerj.daisy.repository.schema.PartType;
21 import org.outerj.daisy.repository.commonimpl.schema.CommonRepositorySchema;
22 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
23 import org.outerj.daisy.repository.commonimpl.CommonRepository;
24 import org.outerj.daisy.repository.commonimpl.variant.CommonVariantManager;
25 import org.outerj.daisy.linkextraction.LinkType;
26 import org.outerj.daisy.linkextraction.LinkExtractor;
27
28 import java.util.Collection JavaDoc;
29
30 public class LinkExtractorHelper {
31     private final Document document;
32     private final long documentId;
33     private final long liveVersion;
34     private final long lastVersion;
35     private final boolean documentContainsLastVersion;
36     private LinkCollectorImpl linkCollector;
37     private final CommonRepositorySchema repositorySchema;
38     private final CommonVariantManager variantManager;
39     private final AuthenticatedUser systemUser;
40     private final LocalRepositoryManager.Context context;
41
42     /**
43      * Constructor.
44      *
45      * <p>The boolean documentContainsLastVersion indicates whether the data currently in the document object
46      * is the latest version. This is needed because the linkextractor is used during the storage of the
47      * document, when the new version does not yet really exist (likewise, that's why we need documentId
48      * and liveVersion explicitely).
49      */

50     public LinkExtractorHelper(Document document, long documentId, long liveVersion, long lastVersion, boolean documentContainsLastVersion,
51                                AuthenticatedUser systemUser, LocalRepositoryManager.Context context) {
52         this.document = document;
53         this.documentId = documentId;
54         this.liveVersion = liveVersion;
55         this.lastVersion = lastVersion;
56         this.documentContainsLastVersion = documentContainsLastVersion;
57         CommonRepository repository = context.getCommonRepository();
58         this.repositorySchema = repository.getRepositorySchema();
59         this.variantManager = repository.getVariantManager();
60         this.systemUser = systemUser;
61         this.context = context;
62     }
63
64     public Collection JavaDoc extract() throws Exception JavaDoc {
65         this.linkCollector = new LinkCollectorImpl(documentId, document.getBranchId(), document.getLanguageId(),
66                 variantManager, systemUser);
67
68         Field[] fields;
69         Part[] parts;
70         Link[] links;
71         if (documentContainsLastVersion) {
72             fields = document.getFields().getArray();
73             parts = document.getParts().getArray();
74             links = document.getLinks().getArray();
75         } else {
76             Version version = document.getVersion(document.getLastVersionId());
77             fields = document.getFields().getArray();
78             parts = version.getParts().getArray();
79             links = version.getLinks().getArray();
80         }
81
82         if (lastVersion == liveVersion) {
83             extractLinks(fields, true, true);
84             extractLinks(parts, true, true);
85             extractLinks(links, true, true);
86         } else {
87             extractLinks(fields, true, false);
88             extractLinks(parts, true, false);
89             extractLinks(links, true, false);
90             if (liveVersion != -1) {
91                 Version version = document.getVersion(liveVersion);
92                 extractLinks(version.getFields().getArray(), false, true);
93                 extractLinks(version.getParts().getArray(), false, true);
94                 extractLinks(version.getLinks().getArray(), false, true);
95             }
96         }
97
98         return linkCollector.getLinks();
99     }
100
101     private void extractLinks(Part[] parts, boolean isLastVersion, boolean isLiveVersion) throws Exception JavaDoc {
102         for (int i = 0; i < parts.length; i++) {
103             this.linkCollector.changeTo(parts[i].getTypeId(), isLastVersion, isLiveVersion);
104             PartType partType = repositorySchema.getPartTypeById(parts[i].getTypeId(), false, systemUser);
105
106             if (partType.getLinkExtractor() != null) {
107                 LinkExtractor linkExtractor = context.getLinkExtractor(partType.getLinkExtractor());
108                 if (linkExtractor != null) {
109                     try {
110                         linkExtractor.extractLinks(parts[i], linkCollector, document.getBranchId(), document.getLanguageId());
111                     } catch (Throwable JavaDoc e) {
112                         context.getLogger().error("Error calling link extractor for " + document.getVariantKey() + " part " + partType.getName(), e);
113                     }
114                 }
115             }
116         }
117     }
118
119     private void extractLinks(Link[] links, boolean isLastVersion, boolean isLiveVersion) {
120         this.linkCollector.changeTo(-1, isLastVersion, isLiveVersion);
121         for (int i = 0; i < links.length; i++) {
122             linkCollector.addLink(LinkType.OUT_OF_LINE, links[i].getTarget());
123         }
124     }
125
126     private void extractLinks(Field[] fields, boolean isLastVersion, boolean isLiveVersion) {
127         this.linkCollector.changeTo(-1, isLastVersion, isLiveVersion);
128         for (int i = 0; i < fields.length; i++) {
129             if (fields[i].getValueType() == ValueType.LINK) {
130                 Object JavaDoc value = fields[i].getValue();
131                 Object JavaDoc[] values;
132                 if (!(value instanceof Object JavaDoc[]))
133                     values = new Object JavaDoc[] {value};
134                 else
135                     values = (Object JavaDoc[])value;
136                 for (int k = 0; k < values.length; k++) {
137                     VariantKey variantKey = (VariantKey)values[k];
138                     long targetBranchId = variantKey.getBranchId() != -1 ? variantKey.getBranchId() : document.getBranchId();
139                     long targetLanguageId = variantKey.getLanguageId() != -1 ? variantKey.getLanguageId() : document.getLanguageId();
140                     linkCollector.addLink(LinkType.FIELD, variantKey.getDocumentId(), targetBranchId, targetLanguageId);
141                 }
142             }
143         }
144     }
145 }
146
Popular Tags