KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > action > executer > ContentMetadataExtracter


1 /*
2  * Copyright (C) 2005 Jesper Steen Møller
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.action.executer;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.alfresco.model.ContentModel;
25 import org.alfresco.repo.content.metadata.MetadataExtracter;
26 import org.alfresco.repo.content.metadata.MetadataExtracterRegistry;
27 import org.alfresco.service.cmr.action.Action;
28 import org.alfresco.service.cmr.action.ParameterDefinition;
29 import org.alfresco.service.cmr.dictionary.ClassDefinition;
30 import org.alfresco.service.cmr.dictionary.DictionaryService;
31 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
32 import org.alfresco.service.cmr.repository.ContentReader;
33 import org.alfresco.service.cmr.repository.ContentService;
34 import org.alfresco.service.cmr.repository.NodeRef;
35 import org.alfresco.service.cmr.repository.NodeService;
36 import org.alfresco.service.namespace.QName;
37
38 /**
39  * Extract metadata from any added content.
40  * <p>
41  * The metadata is extracted from the content and compared to the current
42  * property values. Missing or zero-length properties are replaced,
43  * otherwise they are left as is.<br/>
44  * <i>This may change if the action gets parameterized in future</i>.
45  *
46  * @author Jesper Steen Møller
47  */

48 public class ContentMetadataExtracter extends ActionExecuterAbstractBase
49 {
50     /**
51      * Action constants
52      */

53     public static final String JavaDoc NAME = "extract-metadata";
54
55     /*
56      * TODO: Action parameters.
57      *
58      * Currently none exist, but it may be nice to add a 'policy' parameter for
59      * overwriting the extracted properties, with the following possible values:
60      * 1) Never: Never overwrite node properties that
61      * exist (i.e. preserve values, nulls, and blanks)
62      * 2) Pragmatic: Write
63      * extracted properties if they didn't exist before, are null, or evaluate
64      * to an empty string.
65      * 3) Always: Always store the extracted properes.
66      *
67      * Policies 1 and 2 will preserve previously set properties in case nodes
68      * are moved/copied, making this action run on the same content several
69      * times. However, if a property is deliberately cleared (e.g. by putting
70      * the empty string into the "decription" field), the pragmatic policy would
71      * indeed overwrite it. The current implementation matches the 'pragmatic'
72      * policy.
73      */

74
75     /**
76      * The node service
77      */

78     private NodeService nodeService;
79
80     /**
81      * Set the node service
82      *
83      * @param nodeService the node service
84      */

85     public void setNodeService(NodeService nodeService)
86     {
87         this.nodeService = nodeService;
88     }
89
90     /**
91      * Our content service
92      */

93     private ContentService contentService;
94
95     /**
96      * @param contentService The contentService to set.
97      */

98     public void setContentService(ContentService contentService)
99     {
100         this.contentService = contentService;
101     }
102     
103     /**
104      * The dictionary service
105      */

106     private DictionaryService dictionaryService;
107     
108     /**
109      * @param dictService The DictionaryService to set.
110      */

111     public void setDictionaryService(DictionaryService dictService)
112     {
113         this.dictionaryService = dictService;
114     }
115
116     /**
117      * Our Extracter
118      */

119     private MetadataExtracterRegistry metadataExtracterRegistry;
120
121     /**
122      * @param metadataExtracterRegistry The metadataExtracterRegistry to set.
123      */

124     public void setMetadataExtracterRegistry(MetadataExtracterRegistry metadataExtracterRegistry)
125     {
126         this.metadataExtracterRegistry = metadataExtracterRegistry;
127     }
128
129     /**
130      * @see org.alfresco.repo.action.executer.ActionExecuter#execute(org.alfresco.service.cmr.repository.NodeRef,
131      * NodeRef)
132      */

133     public void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef)
134     {
135         if (this.nodeService.exists(actionedUponNodeRef) == true)
136         {
137             ContentReader cr = contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);
138
139             // 'cr' may be null, e.g. for folders and the like
140
if (cr != null && cr.getMimetype() != null)
141             {
142                 MetadataExtracter me = metadataExtracterRegistry.getExtracter(cr.getMimetype());
143                 if (me != null)
144                 {
145                     Map JavaDoc<QName, Serializable JavaDoc> newProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(7, 0.5f);
146                     me.extract(cr, newProps);
147
148                     Map JavaDoc<QName, Serializable JavaDoc> allProps = nodeService.getProperties(actionedUponNodeRef);
149
150                     /*
151                      * The code below implements a modestly conservative
152                      * 'preserve' policy which shouldn't override values
153                      * accidentally.
154                      */

155
156                     boolean changed = false;
157                     for (QName key : newProps.keySet())
158                     {
159                         // check if we need to add an aspect for the prop
160
ClassDefinition propClass = dictionaryService.getProperty(key).getContainerClass();
161                         if (propClass.isAspect() &&
162                             nodeService.hasAspect(actionedUponNodeRef, propClass.getName()) == false)
163                         {
164                             Map JavaDoc<QName, Serializable JavaDoc> aspectProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(3, 1.0f);
165                             for (QName defKey : propClass.getProperties().keySet())
166                             {
167                                 if (dictionaryService.getProperty(defKey).isMandatory())
168                                 {
169                                     aspectProps.put(defKey, allProps.get(defKey));
170                                     allProps.remove(defKey);
171                                 }
172                             }
173                             nodeService.addAspect(actionedUponNodeRef, propClass.getName(), aspectProps);
174                         }
175                         
176                         Serializable JavaDoc value = newProps.get(key);
177                         if (value == null)
178                         {
179                             continue; // Content extracters shouldn't do this
180
}
181                         
182                         // Look up the old value, and check for nulls
183
Serializable JavaDoc oldValue = allProps.get(key);
184                         if (oldValue == null || oldValue.toString().length() == 0)
185                         {
186                             allProps.put(key, value);
187                             changed = true;
188                         }
189                     }
190                     
191                     if (changed)
192                     {
193                         nodeService.setProperties(actionedUponNodeRef, allProps);
194                     }
195                 }
196             }
197         }
198     }
199
200     @Override JavaDoc
201     protected void addParameterDefintions(List JavaDoc<ParameterDefinition> arg0)
202     {
203         // None!
204
}
205 }
Popular Tags