001 /*
002 *
003 * $Revision: 15270 $ $Date: 2009-05-25 17:27:57 +0200 (Mon, 25 May 2009) $
004 *
005 * This file is part of *** M y C o R e ***
006 * See http://www.mycore.de/ for details.
007 *
008 * This program is free software; you can use it, redistribute it
009 * and / or modify it under the terms of the GNU General Public License
010 * (GPL) as published by the Free Software Foundation; either version 2
011 * of the License or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program, in a file called gpl.txt or license.txt.
020 * If not, write to the Free Software Foundation Inc.,
021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA
022 */
023
024 package org.mycore.datamodel.metadata;
025
026 import static org.mycore.common.MCRConstants.DEFAULT_ENCODING;
027
028 import java.net.URI;
029
030 import org.apache.log4j.Logger;
031 import org.mycore.common.MCRConfiguration;
032 import org.mycore.common.MCRConfigurationException;
033 import org.mycore.common.MCRConstants;
034 import org.mycore.common.MCRException;
035 import org.mycore.common.MCRPersistenceException;
036 import org.mycore.datamodel.common.MCRActiveLinkException;
037
038 /**
039 * This class is a abstract basic class for objects in the MyCoRe Project. It is
040 * the frame to produce a full functionality object.
041 *
042 * @author Jens Kupferschmidt
043 * @version $Revision: 15270 $ $Date: 2009-05-25 17:27:57 +0200 (Mon, 25 May 2009) $
044 */
045 public abstract class MCRBase {
046 /**
047 * constant value for the object id length
048 */
049 public final static int MAX_LABEL_LENGTH = 256;
050
051 // from configuration
052 protected static final MCRConfiguration mcr_conf;
053
054 protected static final String mcr_encoding;
055
056 // the DOM document
057 protected org.jdom.Document jdom_document = null;
058
059 // the object content
060 protected MCRObjectID mcr_id = null;
061
062 protected String mcr_label = null;
063
064 protected String mcr_version = null;
065
066 protected String mcr_schema = null;
067
068 protected MCRObjectService mcr_service = null;
069
070 // other
071 protected static final String NL;
072
073 protected static final String SLASH;
074
075 protected boolean importMode = false;
076
077 // logger
078 static Logger LOGGER = Logger.getLogger(MCRBase.class.getPackage().getName());
079
080 /**
081 * Load static data for all MCRObjects
082 */
083 static {
084 NL = System.getProperty("line.separator");
085 SLASH = System.getProperty("file.separator");
086 // Load the configuration
087 mcr_conf = MCRConfiguration.instance();
088
089 // Default Encoding
090 mcr_encoding = mcr_conf.getString("MCR.Metadata.DefaultEncoding", DEFAULT_ENCODING);
091 LOGGER.debug("Encoding = " + mcr_encoding);
092 }
093
094 /**
095 * This is the constructor of the MCRBase class. It make an instance of the
096 * parser class and the metadata class. <br>
097 *
098 * @exception MCRException
099 * general Exception of MyCoRe
100 * @exception MCRConfigurationException
101 * a special exception for configuration data
102 */
103 public MCRBase() throws MCRException, MCRConfigurationException {
104 mcr_id = new MCRObjectID();
105 mcr_label = "";
106 mcr_version = MCRConstants.VERSION;
107 mcr_schema = "";
108
109 // Service class
110 mcr_service = new MCRObjectService();
111 }
112
113 /**
114 * This methode return the object id. If this is not set, null was returned.
115 *
116 * @return the id as MCRObjectID
117 */
118 public final MCRObjectID getId() {
119 return mcr_id;
120 }
121
122 /**
123 * This methode return the object label. If this is not set, null was
124 * returned.
125 *
126 * @return the lable as a string
127 */
128 public final String getLabel() {
129 return mcr_label;
130 }
131
132 /**
133 * This methode return the MyCoRe version of the data structure.
134 *
135 * @return the version as a string
136 */
137 public final String getVersion() {
138 return mcr_version;
139 }
140
141 /**
142 * This methode return the object schema. If this is not set, null was
143 * returned.
144 *
145 * @return the schema as a string
146 */
147 public final String getSchema() {
148 return mcr_schema;
149 }
150
151 /**
152 * This methode return the instance of the MCRObjectService class. If this
153 * was not found, null was returned.
154 *
155 * @return the instance of the MCRObjectService class
156 */
157 public final MCRObjectService getService() {
158 return mcr_service;
159 }
160
161 /**
162 * This methode read the XML input stream from an URI into a temporary DOM
163 * and check it with XSchema file.
164 *
165 * @param uri
166 * an URI
167 * @exception MCRException
168 * general Exception of MyCoRe
169 */
170 public abstract void setFromURI(URI uri) throws MCRException;
171
172 /**
173 * This methode read the XML input stream from a byte array into JDOM and
174 * check it with XSchema file.
175 *
176 * @param xml
177 * a XML string
178 * @exception MCRException
179 * general Exception of MyCoRe
180 */
181 public abstract void setFromXML(byte[] xml, boolean valid) throws MCRException;
182
183 /**
184 * This methode set the object ID.
185 *
186 * @param id
187 * the object ID
188 */
189 public final void setId(MCRObjectID id) {
190 if (id.isValid()) {
191 mcr_id = id;
192 }
193 }
194
195 /**
196 * This methode set the object label.
197 *
198 * @param label
199 * the object label
200 */
201 public final void setLabel(String label) {
202 mcr_label = label.trim();
203
204 if (mcr_label.length() > MAX_LABEL_LENGTH) {
205 mcr_label = mcr_label.substring(0, MAX_LABEL_LENGTH);
206 }
207 }
208
209 /**
210 * This methode set the MyCoRe version to the string 'Version 1.3'.
211 */
212 public final void setVersion() {
213 mcr_version = MCRConstants.VERSION;
214 }
215
216 /**
217 * This methode set the object schema.
218 *
219 * @param schema
220 * the object schema
221 */
222 public final void setSchema(String schema) {
223 if (schema == null) {
224 mcr_schema = "";
225
226 return;
227 }
228
229 mcr_schema = schema.trim();
230 }
231
232 /**
233 * This methode set the object MCRObjectService.
234 *
235 * @param service
236 * the object MCRObjectService part
237 */
238 public final void setService(MCRObjectService service) {
239 if (service != null) {
240 mcr_service = service;
241 }
242 }
243
244 /**
245 * This methode create a XML stream for all object data.
246 *
247 * @exception MCRException
248 * if the content of this class is not valid
249 * @return a JDOM Document with the XML data of the object as byte array
250 */
251 public abstract org.jdom.Document createXML() throws MCRException;
252
253 /**
254 * The methode create the object in the data store.
255 *
256 * @exception MCRPersistenceException
257 * if a persistence problem is occured
258 * @throws MCRActiveLinkException
259 */
260 public abstract void createInDatastore() throws MCRPersistenceException, MCRActiveLinkException;
261
262 /**
263 * The methode delete the object in the data store.
264 *
265 * @param id
266 * the object ID
267 * @exception MCRPersistenceException
268 * if a persistence problem is occured
269 * @throws MCRActiveLinkException
270 */
271 public abstract void deleteFromDatastore(String id) throws MCRPersistenceException, MCRActiveLinkException;
272
273 /**
274 * The methode receive the object for the given MCRObjectID and stored it in
275 * this MCRObject.
276 *
277 * @param id
278 * the object ID
279 * @exception MCRPersistenceException
280 * if a persistence problem is occured
281 */
282 public abstract void receiveFromDatastore(String id) throws MCRPersistenceException;
283
284 /**
285 * The methode update the object in the data store.
286 *
287 * @exception MCRPersistenceException
288 * if a persistence problem is occured
289 * @throws MCRActiveLinkException
290 */
291 public abstract void updateInDatastore() throws MCRPersistenceException, MCRActiveLinkException;
292
293 /**
294 * The method repair search index.
295 *
296 * @param id
297 * the MCRObjectID as String
298 */
299 public abstract void repairPersitenceDatastore(String id) throws MCRPersistenceException;
300
301 /**
302 * The method repair search index.
303 *
304 * @param id
305 * the MCRObjectID
306 */
307 public abstract void repairPersitenceDatastore(MCRObjectID id) throws MCRPersistenceException;
308
309 /**
310 * This method check the validation of the content of this class. The method
311 * returns <em>true</em> if
312 * <ul>
313 * <li>the mcr_id value is valid
314 * <li>the label value is not null or empty
315 * </ul>
316 * otherwise the method return <em>false</em>
317 *
318 * @return a boolean value
319 */
320 public boolean isValid() {
321 if (!mcr_id.isValid()) {
322 return false;
323 }
324
325 if ((mcr_label == null) || ((mcr_label = mcr_label.trim()).length() == 0)) {
326 return false;
327 }
328
329 if ((mcr_schema == null) || ((mcr_schema = mcr_schema.trim()).length() == 0)) {
330 return false;
331 }
332
333 return true;
334 }
335
336 public boolean isImportMode() {
337 return importMode;
338 }
339
340 public void setImportMode(boolean importMode) {
341 this.importMode = importMode;
342 }
343 }