1 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 ; 32 import java.util.List ; 33 import java.util.ArrayList ; 34 import java.util.ListIterator ; 35 36 46 public class ContentLocaleManagerImpl extends BaseManagerImpl implements ContentLocaleManager { 47 48 51 protected ContentLocaleDAO contentLocaleDAO; 52 53 56 protected ContentFieldDAO contentFieldDAO; 57 58 61 protected ContentFieldValueDAO contentFieldValueDAO; 62 63 66 protected List availableLocales; 67 68 71 protected List activeLocales; 72 73 76 protected ContentLocale defaultLocale; 77 78 81 public ContentLocaleManagerImpl() { 82 } 83 84 89 public void setContentLocaleDAO(ContentLocaleDAO contentLocaleDAO) { 90 this.contentLocaleDAO = contentLocaleDAO; 91 } 92 93 98 public void setContentFieldDAO(ContentFieldDAO contentFieldDAO) { 99 this.contentFieldDAO = contentFieldDAO; 100 } 101 102 107 public void setContentFieldValueDAO(ContentFieldValueDAO contentFieldValueDAO) { 108 this.contentFieldValueDAO = contentFieldValueDAO; 109 } 110 111 112 115 public void initialize() { 116 doCache(); 117 if (log.isInfoEnabled()) { 118 log.info("Content locales initialized"); 119 } 120 } 121 122 125 protected void doCache() { 126 availableLocales = listContentLocales(null).asList(); 128 129 activeLocales = new ArrayList (); 131 for ( ListIterator 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 } 143 144 146 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 String 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 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 176 contentLocaleDAO.createContentLocale(contentLocale); 178 179 List contentFields = contentFieldDAO.listContentFields(null).asList(); 181 for ( Iterator 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 203 public ContentLocale retrieveContentLocale(String localeIdentifier) { 204 ContentLocale contentLocale = null; 205 contentLocale = contentLocaleDAO.retrieveContentLocale(localeIdentifier); 206 return contentLocale; 207 } 208 209 212 public void updateContentLocale(ContentLocale contentLocale) { 213 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 232 public void deleteContentLocale(String 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 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 259 262 public PartialCollection listContentLocales(QueryInfo queryInfo) { 263 return contentLocaleDAO.listContentLocales(queryInfo); 264 } 265 266 269 public List getAvailableLocales() { 270 return availableLocales; 271 } 272 273 276 public List getActiveLocales() { 277 return activeLocales; 278 } 279 280 283 public ContentLocale getDefaultLocale() { 284 return defaultLocale; 285 } 286 287 289 291 } 292 | Popular Tags |