KickJava   Java API By Example, From Geeks To Geeks.

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


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.ContentField;
21 import com.blandware.atleap.model.core.ContentFieldValue;
22 import com.blandware.atleap.model.core.ContentLocale;
23 import com.blandware.atleap.persistence.core.ContentFieldDAO;
24 import com.blandware.atleap.persistence.core.ContentFieldValueDAO;
25 import com.blandware.atleap.persistence.core.ContentLocaleDAO;
26 import com.blandware.atleap.persistence.exception.DeleteException;
27 import com.blandware.atleap.service.core.ContentLocaleManager;
28 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
29 import com.blandware.atleap.service.exception.BeanNotFoundException;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.ListIterator JavaDoc;
35
36 /**
37  * <p>Implementation of ContentLocaleManager</p>
38  * <p><a HREF="ContentLocaleManagerImpl.java.htm"><i>View Source</i></a>
39  * </p>
40  * <p/>
41  *
42  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
43  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
44  * @version $Revision: 1.21 $ $Date: 2005/11/26 14:51:31 $
45  */

46 public class ContentLocaleManagerImpl extends BaseManagerImpl implements ContentLocaleManager {
47
48     /**
49      * Content locale DAO
50      */

51     protected ContentLocaleDAO contentLocaleDAO;
52
53     /**
54      * Content field DAO
55      */

56     protected ContentFieldDAO contentFieldDAO;
57
58     /**
59      * Content field value DAO
60      */

61     protected ContentFieldValueDAO contentFieldValueDAO;
62
63     /**
64      * List of all locales, available in the system
65      */

66     protected List JavaDoc availableLocales;
67
68     /**
69      * List of active locales
70      */

71     protected List JavaDoc activeLocales;
72
73     /**
74      * Default locale
75      */

76     protected ContentLocale defaultLocale;
77
78     /**
79      * Creates new instance of ContentLocaleManagerImpl
80      */

81     public ContentLocaleManagerImpl() {
82     }
83
84     /**
85      * Sets DAO for operating with content locales
86      *
87      * @param contentLocaleDAO the DAO to set
88      */

89     public void setContentLocaleDAO(ContentLocaleDAO contentLocaleDAO) {
90         this.contentLocaleDAO = contentLocaleDAO;
91     }
92
93     /**
94      * Sets DAO for operating with content fields
95      *
96      * @param contentFieldDAO the DAO to set
97      */

98     public void setContentFieldDAO(ContentFieldDAO contentFieldDAO) {
99         this.contentFieldDAO = contentFieldDAO;
100     }
101
102     /**
103      * Sets DAO for operating with content field values
104      *
105      * @param contentFieldValueDAO the DAO to set
106      */

107     public void setContentFieldValueDAO(ContentFieldValueDAO contentFieldValueDAO) {
108         this.contentFieldValueDAO = contentFieldValueDAO;
109     }
110
111
112     /**
113      * @see com.blandware.atleap.service.core.ContentLocaleManager#initialize()
114      */

115     public void initialize() {
116         doCache();
117         if (log.isInfoEnabled()) {
118             log.info("Content locales initialized");
119         }
120     }
121
122     /**
123      * Caches available locales, active locales and default one
124      */

125     protected void doCache() {
126         // get list of locales
127
availableLocales = listContentLocales(null).asList();
128
129         // get active locales and set default one
130
activeLocales = new ArrayList JavaDoc();
131         for ( ListIterator JavaDoc i = availableLocales.listIterator(); i.hasNext(); ) {
132             ContentLocale locale = ((ContentLocale) i.next()).shallowCopy();
133             if ( locale.isActive() ) {
134                 activeLocales.add(locale);
135                 if ( locale.getDefaultInstance().booleanValue() ) {
136                     defaultLocale = locale;
137                 }
138             }
139             i.set(locale);
140         }
141         // TODO: If no default locale present in system (the DB is corrupted for instance), default locale will contain something wrong. Fix it
142
}
143
144     //~ CRUD Methods ================================================================
145

146     /**
147      * @see com.blandware.atleap.service.core.ContentLocaleManager#createContentLocale(com.blandware.atleap.model.core.ContentLocale)
148      */

149     public void createContentLocale(ContentLocale contentLocale) throws BeanAlreadyExistsException {
150
151         if ( log.isDebugEnabled() ) {
152             log.debug("Creating new content locale (identifier=" + contentLocale.getIdentifier() + ")...");
153         }
154
155         ContentLocale existingContentLocale = contentLocaleDAO.retrieveContentLocale(contentLocale.getIdentifier());
156         if ( existingContentLocale != null ) {
157             // locale already exists
158
String JavaDoc errorMessage = "Content locale with identifier '" + contentLocale.getIdentifier() + "' already exists";
159             if ( log.isErrorEnabled() ) {
160                 log.error(errorMessage);
161             }
162             throw new BeanAlreadyExistsException(errorMessage);
163         }
164
165         Integer JavaDoc localesCount = contentLocaleDAO.listContentLocales(null).getTotal();
166         if ( localesCount.intValue() == 0 ) {
167             contentLocale.setDefaultInstance(Boolean.TRUE);
168             contentLocale.setActive(Boolean.TRUE);
169         } else {
170             contentLocale.setDefaultInstance(Boolean.FALSE);
171             contentLocale.setActive(Boolean.FALSE);
172         }
173
174         // TODO: solve problem with hardcoded items when there is no content locale in system (we're creating the first one)
175

176         // locale does not exist, so create it
177
contentLocaleDAO.createContentLocale(contentLocale);
178
179         // create empty CFV with current locale for all content fields
180
List JavaDoc contentFields = contentFieldDAO.listContentFields(null).asList();
181         for ( Iterator JavaDoc i = contentFields.iterator(); i.hasNext(); ) {
182             ContentField contentField = (ContentField) i.next();
183             ContentFieldValue contentFieldValue = new ContentFieldValue();
184             if ( contentField.getType() == ContentField.LINE_TYPE ) {
185                 contentFieldValue.setSimpleValue("");
186             } else {
187                 contentFieldValue.setValue(new byte[0]);
188             }
189             contentFieldValueDAO.createContentFieldValue(contentFieldValue, contentField, contentLocale);
190         }
191
192         doCache();
193
194         if ( log.isDebugEnabled() ) {
195             log.debug("New locale has been created successfully.");
196         }
197
198     }
199
200     /**
201      * @see com.blandware.atleap.service.core.ContentLocaleManager#retrieveContentLocale(java.lang.String)
202      */

203     public ContentLocale retrieveContentLocale(String JavaDoc localeIdentifier) {
204         ContentLocale contentLocale = null;
205         contentLocale = contentLocaleDAO.retrieveContentLocale(localeIdentifier);
206         return contentLocale;
207     }
208
209     /**
210      * @see com.blandware.atleap.service.core.ContentLocaleManager#updateContentLocale(com.blandware.atleap.model.core.ContentLocale)
211      */

212     public void updateContentLocale(ContentLocale contentLocale) {
213         // remove content locale from cache in order to prevent Hibernate from assigning new version number
214
contentLocaleDAO.removeFromCache(contentLocale);
215
216         if ( log.isDebugEnabled() ) {
217             log.debug("Updating content locale (identifier=" + contentLocale.getIdentifier() + ")...");
218         }
219
220         contentLocaleDAO.updateContentLocale(contentLocale);
221
222         doCache();
223
224         if ( log.isDebugEnabled() ) {
225             log.debug("Locale was updated succefully.");
226         }
227     }
228
229     /**
230      * @see com.blandware.atleap.service.core.ContentLocaleManager#deleteContentLocale(java.lang.String)
231      */

232     public void deleteContentLocale(String JavaDoc localeIdentifier) throws DeleteException, BeanNotFoundException {
233
234         if ( log.isDebugEnabled() ) {
235             log.debug("Deleting locale with identifier '" + localeIdentifier + "'");
236         }
237
238         ContentLocale contentLocale = contentLocaleDAO.retrieveContentLocale(localeIdentifier);
239         if ( contentLocale == null ) {
240             String JavaDoc errorMessage = "No locale with identifier=" + localeIdentifier + "could be found";
241             throw new BeanNotFoundException(errorMessage);
242         }
243
244         if ( contentLocale.getDefaultInstance().booleanValue() ) {
245             throw new DeleteException("Content locale is default and cannot be deleted.");
246         }
247
248         contentLocaleDAO.deleteContentLocale(contentLocale);
249
250         doCache();
251
252         if ( log.isDebugEnabled() ) {
253             log.debug("Locale has been deleted successfully");
254         }
255     }
256
257     // ~ Additional methods ================================================================
258

259     /**
260      * @see com.blandware.atleap.service.core.ContentLocaleManager#listContentLocales(com.blandware.atleap.common.util.QueryInfo)
261      */

262     public PartialCollection listContentLocales(QueryInfo queryInfo) {
263         return contentLocaleDAO.listContentLocales(queryInfo);
264     }
265
266     /**
267      * @see com.blandware.atleap.service.core.ContentLocaleManager#getAvailableLocales()
268      */

269     public List JavaDoc getAvailableLocales() {
270         return availableLocales;
271     }
272
273     /**
274      * @see com.blandware.atleap.service.core.ContentLocaleManager#getActiveLocales()
275      */

276     public List JavaDoc getActiveLocales() {
277         return activeLocales;
278     }
279
280     /**
281      * @see com.blandware.atleap.service.core.ContentLocaleManager#getDefaultLocale()
282      */

283     public ContentLocale getDefaultLocale() {
284         return defaultLocale;
285     }
286
287     // ~ Finders ================================================================
288

289     // ~ Service methods ================================================================
290

291 }
292
Popular Tags