KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > service > core > impl > ContentResourceManagerImpl


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 com.blandware.atleap.service.core.impl;
17
18 import com.blandware.atleap.common.util.PartialCollection;
19 import com.blandware.atleap.common.util.QueryInfo;
20 import com.blandware.atleap.model.core.*;
21 import com.blandware.atleap.persistence.core.ContentDocumentDAO;
22 import com.blandware.atleap.persistence.core.ContentImageDAO;
23 import com.blandware.atleap.persistence.core.ContentLocaleDAO;
24 import com.blandware.atleap.persistence.core.ContentResourceDAO;
25 import com.blandware.atleap.persistence.exception.DeleteException;
26 import com.blandware.atleap.service.core.ContentResourceManager;
27 import com.blandware.atleap.service.core.UtilManager;
28 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
29 import com.blandware.atleap.service.exception.BeanNotFoundException;
30 import com.blandware.atleap.service.exception.ContentLocaleNotFoundException;
31
32 /**
33  * <p>Implementation of ContentResourceManager</p>
34  * <p><a HREF="ContentResourceManagerImpl.java.htm"><i>View Source</i></a>
35  * </p>
36  *
37  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
38  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
39  * @version $Revision: 1.17 $ $Date: 2005/10/10 08:30:00 $
40  */

41 public class ContentResourceManagerImpl extends BaseManagerImpl implements ContentResourceManager {
42
43     /**
44      * Content resource DAO
45      */

46     protected ContentResourceDAO contentResourceDAO;
47     /**
48      * Content image DAO
49      */

50     protected ContentImageDAO contentImageDAO;
51     /**
52      * Content document DAO
53      */

54     protected ContentDocumentDAO contentDocumentDAO;
55     /**
56      * Content locale DAO
57      */

58     protected ContentLocaleDAO contentLocaleDAO;
59
60     /**
61      * Util manager to use
62      */

63     protected UtilManager utilManager;
64
65     /**
66      * Creates new instance of ContentResourceManagerImpl
67      */

68     public ContentResourceManagerImpl() {
69     }
70
71     /**
72      * Sets DAO for operating with content resources
73      *
74      * @param contentResourceDAO the DAO to set
75      */

76     public void setContentResourceDAO(ContentResourceDAO contentResourceDAO) {
77         this.contentResourceDAO = contentResourceDAO;
78     }
79
80     /**
81      * Sets DAO for operating with content images
82      *
83      * @param contentImageDAO the DAO to set
84      */

85     public void setContentImageDAO(ContentImageDAO contentImageDAO) {
86         this.contentImageDAO = contentImageDAO;
87     }
88
89     /**
90      * Sets DAO for operating with content documents
91      *
92      * @param contentDocumentDAO the DAO to set
93      */

94     public void setContentDocumentDAO(ContentDocumentDAO contentDocumentDAO) {
95         this.contentDocumentDAO = contentDocumentDAO;
96     }
97
98     /**
99      * Sets DAO for operating with content locales
100      *
101      * @param contentLocaleDAO the DAO to set
102      */

103     public void setContentLocaleDAO(ContentLocaleDAO contentLocaleDAO) {
104         this.contentLocaleDAO = contentLocaleDAO;
105     }
106
107     /**
108      * Sets util manager to use
109      *
110      * @param utilManager manager to set
111      */

112     public void setUtilManager(UtilManager utilManager) {
113         this.utilManager = utilManager;
114     }
115
116
117     // C O N T E N T R E S O U R C E S
118

119
120     //~ CRUD Methods ================================================================
121

122     /**
123      * @see com.blandware.atleap.service.core.ContentResourceManager#createContentResource(com.blandware.atleap.model.core.ContentResource, com.blandware.atleap.model.core.ResourceData)
124      */

125     public Long JavaDoc createContentResource(ContentResource contentResource, ResourceData resourceData) throws BeanAlreadyExistsException {
126
127         if ( log.isDebugEnabled() ) {
128             log.debug("Creating new content resource (uri=" + contentResource.getUri() + ")...");
129         }
130
131         Long JavaDoc contentResourceId = null;
132
133         if ( contentResourceDAO.hasDuplicates(contentResource) ) {
134             throw new BeanAlreadyExistsException("Content resource with the same URI already exists");
135         }
136
137         // resource does not exist, create it
138
contentResourceId = contentResourceDAO.createContentResource(contentResource, resourceData);
139
140
141         if ( log.isDebugEnabled() ) {
142             log.debug("New resource has been created succefully. It's ID is " + contentResourceId);
143         }
144
145         return contentResourceId;
146     }
147
148     /**
149      * @see com.blandware.atleap.service.core.ContentResourceManager#retrieveContentResource(java.lang.Long)
150      */

151     public ContentResource retrieveContentResource(Long JavaDoc contentResourceId) {
152         return contentResourceDAO.retrieveContentResource(contentResourceId);
153     }
154
155     /**
156      * @see com.blandware.atleap.service.core.ContentResourceManager#updateContentResource(com.blandware.atleap.model.core.ContentResource, com.blandware.atleap.model.core.ResourceData)
157      */

158     public void updateContentResource(ContentResource contentResource, ResourceData resourceData) throws BeanAlreadyExistsException {
159         // remove resource from cache in order to prevent Hibernate from assigning new version number
160
contentResourceDAO.removeFromCache(contentResource);
161
162         if ( log.isDebugEnabled() ) {
163             log.debug("Updating content resource (uri=" + contentResource.getUri() + ")...");
164         }
165
166         if ( contentResourceDAO.hasDuplicates(contentResource) ) {
167             throw new BeanAlreadyExistsException("Content resource with the same URI already exists");
168         }
169
170         // resource with this URI does not exist, so update it
171
ContentResource oldResource = contentResourceDAO.retrieveContentResource(contentResource.getId());
172         if (oldResource != null) {
173             String JavaDoc oldUri = oldResource.getUri();
174             String JavaDoc newUri = contentResource.getUri();
175             if (!newUri.equals(oldUri)) {
176                 utilManager.replaceLinkableObjectUriInLinkedObjects(oldUri, newUri, oldResource.getLinkedContentFieldValues(), oldResource.getLinkedMenuItems(), "", true);
177             }
178             contentResourceDAO.removeFromCache(oldResource);
179         }
180
181         contentResourceDAO.updateContentResource(contentResource, resourceData);
182
183
184         if ( log.isDebugEnabled() ) {
185             log.debug("Resource was updated succefully.");
186         }
187     }
188
189     /**
190      * @see com.blandware.atleap.service.core.ContentResourceManager#deleteContentResource(java.lang.Long)
191      */

192     public void deleteContentResource(Long JavaDoc contentResourceId) throws DeleteException, BeanNotFoundException {
193         ContentResource contentResource = contentResourceDAO.retrieveContentResource(contentResourceId);
194         if ( contentResource == null ) {
195             String JavaDoc errorMessage = "No content resource with ID=" + contentResource + "could be found";
196             throw new BeanNotFoundException(errorMessage);
197         }
198
199         contentResourceDAO.deleteContentResource(contentResource);
200     }
201
202     // ~ Additional methods ================================================================
203

204     /**
205      * @see com.blandware.atleap.service.core.ContentResourceManager#listContentResources(com.blandware.atleap.common.util.QueryInfo)
206      */

207     public PartialCollection listContentResources(QueryInfo queryInfo) {
208         return contentResourceDAO.listContentResources(queryInfo);
209     }
210
211     /**
212      * @see com.blandware.atleap.service.core.ContentResourceManager#increaseUsageCounter(java.lang.Long, java.lang.Integer)
213      */

214     public void increaseUsageCounter(Long JavaDoc resourceId, Integer JavaDoc value) throws BeanNotFoundException {
215         ContentResource resource = contentResourceDAO.retrieveContentResource(resourceId);
216
217         if ( resource == null ) {
218             // resource does not exist
219
String JavaDoc errorMessage = "Resource with ID=" + resourceId + " does not exist";
220             if ( log.isErrorEnabled() ) {
221                 log.error(errorMessage);
222             }
223             throw new BeanNotFoundException(errorMessage);
224         }
225
226         resource.setUsageCounter(new Integer JavaDoc(resource.getUsageCounter().intValue() + value.intValue()));
227         contentResourceDAO.updateContentResource(resource, resource.getResourceData());
228     }
229
230     // ~ Finders ================================================================
231

232     /**
233      * @see com.blandware.atleap.service.core.ContentResourceManager#listContentResources(com.blandware.atleap.common.util.QueryInfo)
234      */

235     public ContentResource findContentResourceByUri(String JavaDoc uri) {
236         return contentResourceDAO.findContentResourceByUri(uri);
237     }
238
239     // C O N T E N T I M A G E S
240

241
242     //~ CRUD Methods ================================================================
243

244     /**
245      * @see com.blandware.atleap.service.core.ContentResourceManager#createContentImage(com.blandware.atleap.model.core.ContentImage, com.blandware.atleap.model.core.ResourceData)
246      */

247     public Long JavaDoc createContentImage(ContentImage contentImage, ResourceData resourceData) throws BeanAlreadyExistsException {
248
249         if ( log.isDebugEnabled() ) {
250             log.debug("Creating new content image (uri=" + contentImage.getUri() + ")...");
251         }
252
253         Long JavaDoc contentImageId = null;
254         if ( contentResourceDAO.hasDuplicates(contentImage) ) {
255             throw new BeanAlreadyExistsException("Content image with the same URI already exists");
256         }
257
258
259         // image does not exist, create it
260
contentImageId = contentImageDAO.createContentImage(contentImage, resourceData);
261
262
263         if ( log.isDebugEnabled() ) {
264             log.debug("New image has been created succefully. It's ID is " + contentImageId);
265         }
266
267         return contentImageId;
268     }
269
270     /**
271      * @see com.blandware.atleap.service.core.ContentResourceManager#retrieveContentImage(java.lang.Long)
272      */

273     public ContentImage retrieveContentImage(Long JavaDoc contentImageId) {
274         ContentImage contentImage = null;
275         contentImage = contentImageDAO.retrieveContentImage(contentImageId);
276         return contentImage;
277     }
278
279     /**
280      * @see com.blandware.atleap.service.core.ContentResourceManager#updateContentImage(com.blandware.atleap.model.core.ContentImage, com.blandware.atleap.model.core.ResourceData)
281      */

282     public void updateContentImage(ContentImage contentImage, ResourceData resourceData) throws BeanAlreadyExistsException {
283         // remove content image from cache in order to prevent Hibernate from assigning new version number
284
contentImageDAO.removeFromCache(contentImage);
285         if ( log.isDebugEnabled() ) {
286             log.debug("Updating content image (uri=" + contentImage.getUri() + ")...");
287         }
288
289         if ( contentResourceDAO.hasDuplicates(contentImage) ) {
290             throw new BeanAlreadyExistsException("Content image with the same URI already exists");
291         }
292
293         // image with this URI does not exist, so update it
294
ContentImage oldImage = (ContentImage) contentResourceDAO.retrieveContentResource(contentImage.getId());
295         if (oldImage != null) {
296             String JavaDoc oldUri = oldImage.getUri();
297             int oldWidth = oldImage.getWidth().intValue();
298             int oldHeight = oldImage.getHeight().intValue();
299             String JavaDoc newUri = contentImage.getUri();
300             int newWidth = contentImage.getWidth().intValue();
301             int newHeight = contentImage.getHeight().intValue();
302             if (newWidth != oldWidth || newHeight != oldHeight) {
303                 utilManager.replaceImageSizesInLinkedObjects(oldUri, oldImage.getLinkedContentFieldValues(), oldWidth, oldHeight, newWidth, newHeight, "", true);
304             }
305             if (!newUri.equals(oldUri)) {
306                 utilManager.replaceLinkableObjectUriInLinkedObjects(oldUri, newUri, oldImage.getLinkedContentFieldValues(), oldImage.getLinkedMenuItems(), "", true);
307             }
308             contentResourceDAO.removeFromCache(oldImage);
309         }
310
311         contentImageDAO.updateContentImage(contentImage, resourceData);
312
313
314         if ( log.isDebugEnabled() ) {
315             log.debug("Image was updated succefully.");
316         }
317     }
318
319     /**
320      * @see com.blandware.atleap.service.core.ContentResourceManager#deleteContentImage(java.lang.Long)
321      */

322     public void deleteContentImage(Long JavaDoc contentImageId) throws DeleteException, BeanNotFoundException {
323
324         ContentImage contentImage = contentImageDAO.retrieveContentImage(contentImageId);
325         if ( contentImage == null ) {
326             String JavaDoc errorMessage = "No content image with ID=" + contentImage + "could be found";
327             throw new BeanNotFoundException(errorMessage);
328         }
329
330         contentImageDAO.deleteContentImage(contentImage);
331     }
332
333     // ~ Additional methods ================================================================
334

335     /**
336      * @see com.blandware.atleap.service.core.ContentResourceManager#listContentImages(com.blandware.atleap.common.util.QueryInfo)
337      */

338     public PartialCollection listContentImages(QueryInfo queryInfo) {
339         return contentImageDAO.listContentImages(queryInfo);
340     }
341
342     // ~ Finders ================================================================
343

344     /**
345      * @see com.blandware.atleap.service.core.ContentResourceManager#listContentImages(com.blandware.atleap.common.util.QueryInfo)
346      */

347     public ContentImage findContentImageByUri(String JavaDoc uri) {
348         return contentImageDAO.findContentImageByUri(uri);
349     }
350
351
352
353     // C O N T E N T D O C U M E N T S
354

355     //~ CRUD Methods ================================================================
356

357     /**
358      * @see com.blandware.atleap.service.core.ContentResourceManager#createContentDocument(com.blandware.atleap.model.core.ContentDocument, com.blandware.atleap.model.core.ResourceData, java.lang.String)
359      */

360     public Long JavaDoc createContentDocument(ContentDocument contentDocument, ResourceData resourceData, String JavaDoc localeIdentifier) throws BeanNotFoundException, BeanAlreadyExistsException {
361
362         if ( log.isDebugEnabled() ) {
363             log.debug("Creating new content document (uri=" + contentDocument.getUri() + ")...");
364         }
365
366         Long JavaDoc contentDocumentId = null;
367         if ( contentResourceDAO.hasDuplicates(contentDocument) ) {
368             throw new BeanAlreadyExistsException("Content document with the same URI already exists");
369         }
370
371         ContentLocale contentLocale = null;
372         contentLocale = contentLocaleDAO.retrieveContentLocale(localeIdentifier);
373
374         if ( contentLocale == null ) {
375             String JavaDoc errorMessage = "No content locale with identifier=" + localeIdentifier + "could be found";
376             if ( log.isErrorEnabled() ) {
377                 log.error(errorMessage);
378             }
379             throw new ContentLocaleNotFoundException(errorMessage);
380         }
381
382
383         // document does not exist, create it
384
contentDocumentId = contentDocumentDAO.createContentDocument(contentDocument, resourceData, contentLocale);
385
386
387         if ( log.isDebugEnabled() ) {
388             log.debug("New document has been created succefully. It's ID is " + contentDocumentId);
389         }
390
391         return contentDocumentId;
392     }
393
394     /**
395      * @see com.blandware.atleap.service.core.ContentResourceManager#retrieveContentDocument(java.lang.Long)
396      */

397     public ContentDocument retrieveContentDocument(Long JavaDoc contentDocumentId) {
398         ContentDocument contentDocument = null;
399         contentDocument = contentDocumentDAO.retrieveContentDocument(contentDocumentId);
400         return contentDocument;
401     }
402
403     /**
404      * @see com.blandware.atleap.service.core.ContentResourceManager#updateContentDocument(com.blandware.atleap.model.core.ContentDocument, com.blandware.atleap.model.core.ResourceData, java.lang.String)
405      */

406     public void updateContentDocument(ContentDocument contentDocument, ResourceData resourceData, String JavaDoc localeIdentifier) throws BeanNotFoundException, BeanAlreadyExistsException {
407         // remove content document from cache in order to prevent Hibernate from assigning new version number
408
contentDocumentDAO.removeFromCache(contentDocument);
409         if ( log.isDebugEnabled() ) {
410             log.debug("Updating content document (uri=" + contentDocument.getUri() + ")...");
411         }
412
413         if ( contentResourceDAO.hasDuplicates(contentDocument) ) {
414             throw new BeanAlreadyExistsException("Content document with the same URI already exists");
415         }
416
417         ContentLocale contentLocale = null;
418         contentLocale = contentLocaleDAO.retrieveContentLocale(localeIdentifier);
419         if ( contentLocale == null ) {
420             String JavaDoc errorMessage = "No content locale with identifier=" + localeIdentifier + "could be found";
421             if ( log.isErrorEnabled() ) {
422                 log.error(errorMessage);
423             }
424             throw new ContentLocaleNotFoundException(errorMessage);
425         }
426
427         // document with this URI does not exist, so update it
428
ContentResource oldResource = contentResourceDAO.retrieveContentResource(contentDocument.getId());
429         if (oldResource != null) {
430             String JavaDoc oldUri = oldResource.getUri();
431             String JavaDoc newUri = contentDocument.getUri();
432             if (!newUri.equals(oldUri)) {
433                 utilManager.replaceLinkableObjectUriInLinkedObjects(oldUri, newUri, oldResource.getLinkedContentFieldValues(), oldResource.getLinkedMenuItems(), "", true);
434             }
435             contentResourceDAO.removeFromCache(oldResource);
436         }
437
438         contentDocumentDAO.updateContentDocument(contentDocument, resourceData, contentLocale);
439
440
441         if ( log.isDebugEnabled() ) {
442             log.debug("Document was updated succefully.");
443         }
444     }
445
446     /**
447      * @see com.blandware.atleap.service.core.ContentResourceManager#deleteContentDocument(java.lang.Long)
448      */

449     public void deleteContentDocument(Long JavaDoc contentDocumentId) throws DeleteException, BeanNotFoundException {
450
451         ContentDocument contentDocument = contentDocumentDAO.retrieveContentDocument(contentDocumentId);
452         if ( contentDocument == null ) {
453             String JavaDoc errorMessage = "No content document with ID=" + contentDocument + "could be found";
454             throw new BeanNotFoundException(errorMessage);
455         }
456
457         contentDocumentDAO.deleteContentDocument(contentDocument);
458     }
459
460     // ~ Additional methods ================================================================
461

462     /**
463      * @see com.blandware.atleap.service.core.ContentResourceManager#listContentDocuments(com.blandware.atleap.common.util.QueryInfo)
464      */

465     public PartialCollection listContentDocuments(QueryInfo queryInfo) {
466         return contentDocumentDAO.listContentDocuments(queryInfo);
467     }
468
469     // ~ Finders ================================================================
470

471     /**
472      * @see com.blandware.atleap.service.core.ContentResourceManager#listContentDocuments(com.blandware.atleap.common.util.QueryInfo)
473      */

474     public ContentDocument findContentDocumentByUri(String JavaDoc uri) {
475         return contentDocumentDAO.findContentDocumentByUri(uri);
476     }
477
478 }
479
Popular Tags