1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.io.StringReader ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import org.apache.log4j.Logger; 33 import org.apache.xerces.parsers.DOMParser; 34 import org.exolab.castor.jdo.Database; 35 import org.exolab.castor.jdo.OQLQuery; 36 import org.exolab.castor.jdo.QueryResults; 37 import org.infoglue.cms.applications.common.VisualFormatter; 38 import org.infoglue.cms.entities.content.DigitalAsset; 39 import org.infoglue.cms.entities.kernel.BaseEntityVO; 40 import org.infoglue.cms.entities.management.ContentTypeDefinition; 41 import org.infoglue.cms.entities.management.Language; 42 import org.infoglue.cms.entities.management.UserContentTypeDefinition; 43 import org.infoglue.cms.entities.management.UserProperties; 44 import org.infoglue.cms.entities.management.UserPropertiesVO; 45 import org.infoglue.cms.entities.management.impl.simple.LanguageImpl; 46 import org.infoglue.cms.entities.management.impl.simple.UserContentTypeDefinitionImpl; 47 import org.infoglue.cms.entities.management.impl.simple.UserPropertiesImpl; 48 import org.infoglue.cms.exception.Bug; 49 import org.infoglue.cms.exception.ConstraintException; 50 import org.infoglue.cms.exception.SystemException; 51 import org.infoglue.cms.security.InfoGluePrincipal; 52 import org.infoglue.cms.util.ConstraintExceptionBuffer; 53 import org.w3c.dom.CDATASection ; 54 import org.w3c.dom.Document ; 55 import org.w3c.dom.Node ; 56 import org.w3c.dom.NodeList ; 57 import org.xml.sax.InputSource ; 58 59 62 63 public class UserPropertiesController extends BaseController 64 { 65 private final static Logger logger = Logger.getLogger(UserPropertiesController.class.getName()); 66 67 70 71 public static UserPropertiesController getController() 72 { 73 return new UserPropertiesController(); 74 } 75 76 77 public UserProperties getUserPropertiesWithId(Integer userPropertiesId, Database db) throws SystemException, Bug 78 { 79 return (UserProperties) getObjectWithId(UserPropertiesImpl.class, userPropertiesId, db); 80 } 81 82 public UserPropertiesVO getUserPropertiesVOWithId(Integer userPropertiesId) throws SystemException, Bug 83 { 84 return (UserPropertiesVO) getVOWithId(UserPropertiesImpl.class, userPropertiesId); 85 } 86 87 public List getUserPropertiesVOList() throws SystemException, Bug 88 { 89 return getAllVOObjects(UserPropertiesImpl.class, "userPropertiesId"); 90 } 91 92 95 96 public UserPropertiesVO create(Integer languageId, Integer contentTypeDefinitionId, UserPropertiesVO userPropertiesVO) throws ConstraintException, SystemException 97 { 98 Database db = CastorDatabaseService.getDatabase(); 99 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 100 101 UserProperties userProperties = null; 102 103 beginTransaction(db); 104 try 105 { 106 userProperties = create(languageId, contentTypeDefinitionId, userPropertiesVO, db); 107 commitTransaction(db); 108 } 109 catch(Exception e) 110 { 111 logger.error("An error occurred so we should not completes the transaction:" + e, e); 112 rollbackTransaction(db); 113 throw new SystemException(e.getMessage()); 114 } 115 116 return userProperties.getValueObject(); 117 } 118 119 123 124 public UserProperties create(Integer languageId, Integer contentTypeDefinitionId, UserPropertiesVO userPropertiesVO, Database db) throws ConstraintException, SystemException, Exception 125 { 126 Language language = LanguageController.getController().getLanguageWithId(languageId, db); 127 ContentTypeDefinition contentTypeDefinition = ContentTypeDefinitionController.getController().getContentTypeDefinitionWithId(contentTypeDefinitionId, db); 128 129 UserProperties userProperties = new UserPropertiesImpl(); 130 userProperties.setLanguage((LanguageImpl)language); 131 userProperties.setContentTypeDefinition((ContentTypeDefinition)contentTypeDefinition); 132 133 userProperties.setValueObject(userPropertiesVO); 134 135 db.create(userProperties); 136 137 return userProperties; 138 } 139 140 143 144 public UserPropertiesVO update(Integer languageId, Integer contentTypeDefinitionId, UserPropertiesVO userPropertiesVO) throws ConstraintException, SystemException, Exception 145 { 146 UserPropertiesVO realUserPropertiesVO = userPropertiesVO; 147 148 if(userPropertiesVO.getId() == null) 149 { 150 InfoGluePrincipal infoGluePrincipal = UserControllerProxy.getController().getUser(userPropertiesVO.getUserName()); 151 List userPropertiesVOList = UserPropertiesController.getController().getUserPropertiesVOList(infoGluePrincipal.getName(), languageId); 152 if(userPropertiesVOList != null && userPropertiesVOList.size() > 0) 153 { 154 realUserPropertiesVO = (UserPropertiesVO)userPropertiesVOList.get(0); 155 realUserPropertiesVO.setValue(userPropertiesVO.getValue()); 156 } 157 else 158 { 159 logger.info("Creating the entity because there was no version at all for: " + contentTypeDefinitionId + " " + languageId); 160 realUserPropertiesVO = create(languageId, contentTypeDefinitionId, userPropertiesVO); 161 } 162 } 163 164 return (UserPropertiesVO) updateEntity(UserPropertiesImpl.class, (BaseEntityVO) realUserPropertiesVO); 165 } 166 167 public UserPropertiesVO update(UserPropertiesVO userPropertiesVO, String [] users) throws ConstraintException, SystemException 168 { 169 Database db = CastorDatabaseService.getDatabase(); 170 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 171 172 UserProperties userProperties = null; 173 174 beginTransaction(db); 175 176 try 177 { 178 userProperties = getUserPropertiesWithId(userPropertiesVO.getUserPropertiesId(), db); 180 userProperties.setValueObject(userPropertiesVO); 181 182 ceb.throwIfNotEmpty(); 184 185 commitTransaction(db); 186 } 187 catch(ConstraintException ce) 188 { 189 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 190 rollbackTransaction(db); 191 throw ce; 192 } 193 catch(Exception e) 194 { 195 logger.error("An error occurred so we should not complete the transaction:" + e, e); 196 rollbackTransaction(db); 197 throw new SystemException(e.getMessage()); 198 } 199 200 return userProperties.getValueObject(); 201 } 202 203 204 205 public void delete(UserPropertiesVO userPropertiesVO) throws ConstraintException, SystemException 206 { 207 deleteEntity(UserPropertiesImpl.class, userPropertiesVO.getUserPropertiesId()); 208 } 209 210 213 public void deleteDigitalAssetRelation(Integer userPropertiesId, DigitalAsset digitalAsset, Database db) throws SystemException, Bug 214 { 215 UserProperties userProperties = getUserPropertiesWithId(userPropertiesId, db); 216 217 userProperties.getDigitalAssets().remove(digitalAsset); 218 digitalAsset.getUserProperties().remove(userProperties); 219 } 220 221 225 226 public List getUserPropertiesVOList(String userName, Integer languageId) throws ConstraintException, SystemException 227 { 228 Database db = CastorDatabaseService.getDatabase(); 229 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 230 231 List userPropertiesVOList = new ArrayList (); 232 233 beginTransaction(db); 234 235 try 236 { 237 List userProperties = getUserPropertiesList(userName, languageId, db, true); 238 userPropertiesVOList = toVOList(userProperties); 239 240 ceb.throwIfNotEmpty(); 242 243 commitTransaction(db); 244 } 245 catch(ConstraintException ce) 246 { 247 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 248 rollbackTransaction(db); 249 throw ce; 250 } 251 catch(Exception e) 252 { 253 logger.error("An error occurred so we should not complete the transaction:" + e, e); 254 rollbackTransaction(db); 255 throw new SystemException(e.getMessage()); 256 } 257 258 return userPropertiesVOList; 259 } 260 261 265 266 public List getUserPropertiesList(String userName, Integer languageId, Database db, boolean readOnly) throws ConstraintException, SystemException, Exception 267 { 268 List userPropertiesList = new ArrayList (); 269 270 OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.UserPropertiesImpl f WHERE f.userName = $1 AND f.language = $2"); 271 oql.bind(userName); 272 oql.bind(languageId); 273 274 QueryResults results = null; 275 if(readOnly) 276 { 277 results = oql.execute(Database.ReadOnly); 278 } 279 else 280 { 281 logger.info("Fetching entity in read/write mode:" + userName); 282 results = oql.execute(); 283 } 284 285 while (results.hasMore()) 286 { 287 UserProperties userProperties = (UserProperties)results.next(); 288 userPropertiesList.add(userProperties); 289 } 290 291 results.close(); 292 oql.close(); 293 294 return userPropertiesList; 295 } 296 297 298 301 302 public List getContentTypeDefinitionVOList(String userName) throws ConstraintException, SystemException 303 { 304 Database db = CastorDatabaseService.getDatabase(); 305 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 306 307 List contentTypeDefinitionVOList = new ArrayList (); 308 309 beginTransaction(db); 310 311 try 312 { 313 List userContentTypeDefinitionList = getUserContentTypeDefinitionList(userName, db); 314 Iterator contentTypeDefinitionsIterator = userContentTypeDefinitionList.iterator(); 315 while(contentTypeDefinitionsIterator.hasNext()) 316 { 317 UserContentTypeDefinition userContentTypeDefinition = (UserContentTypeDefinition)contentTypeDefinitionsIterator.next(); 318 contentTypeDefinitionVOList.add(userContentTypeDefinition.getContentTypeDefinition().getValueObject()); 319 } 320 321 ceb.throwIfNotEmpty(); 322 323 commitTransaction(db); 324 } 325 catch(ConstraintException ce) 326 { 327 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 328 rollbackTransaction(db); 329 throw ce; 330 } 331 catch(Exception e) 332 { 333 logger.error("An error occurred so we should not complete the transaction:" + e, e); 334 rollbackTransaction(db); 335 throw new SystemException(e.getMessage()); 336 } 337 338 return contentTypeDefinitionVOList; 339 } 340 341 344 345 public List getUserContentTypeDefinitionList(String userName, Database db) throws ConstraintException, SystemException, Exception 346 { 347 List userContentTypeDefinitionList = new ArrayList (); 348 349 OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.UserContentTypeDefinitionImpl f WHERE f.userName = $1"); 350 oql.bind(userName); 351 352 QueryResults results = oql.execute(); 353 logger.info("Fetching entity in read/write mode:" + userName); 354 355 while (results.hasMore()) 356 { 357 UserContentTypeDefinition userContentTypeDefinition = (UserContentTypeDefinition)results.next(); 358 userContentTypeDefinitionList.add(userContentTypeDefinition); 359 } 360 361 results.close(); 362 oql.close(); 363 364 return userContentTypeDefinitionList; 365 } 366 367 370 371 public void updateContentTypeDefinitions(String userName, String [] contentTypeDefinitionIds) throws ConstraintException, SystemException 372 { 373 Database db = CastorDatabaseService.getDatabase(); 374 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 375 376 List contentTypeDefinitionVOList = new ArrayList (); 377 378 beginTransaction(db); 379 380 try 381 { 382 List userContentTypeDefinitionList = this.getUserContentTypeDefinitionList(userName, db); 383 Iterator contentTypeDefinitionsIterator = userContentTypeDefinitionList.iterator(); 384 while(contentTypeDefinitionsIterator.hasNext()) 385 { 386 UserContentTypeDefinition userContentTypeDefinition = (UserContentTypeDefinition)contentTypeDefinitionsIterator.next(); 387 db.remove(userContentTypeDefinition); 388 } 389 390 for(int i=0; i<contentTypeDefinitionIds.length; i++) 391 { 392 Integer contentTypeDefinitionId = new Integer (contentTypeDefinitionIds[i]); 393 ContentTypeDefinition contentTypeDefinition = ContentTypeDefinitionController.getController().getContentTypeDefinitionWithId(contentTypeDefinitionId, db); 394 UserContentTypeDefinitionImpl userContentTypeDefinitionImpl = new UserContentTypeDefinitionImpl(); 395 userContentTypeDefinitionImpl.setUserName(userName); 396 userContentTypeDefinitionImpl.setContentTypeDefinition(contentTypeDefinition); 397 db.create(userContentTypeDefinitionImpl); 398 } 399 400 ceb.throwIfNotEmpty(); 401 402 commitTransaction(db); 403 } 404 catch(ConstraintException ce) 405 { 406 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 407 rollbackTransaction(db); 408 throw ce; 409 } 410 catch(Exception e) 411 { 412 logger.error("An error occurred so we should not complete the transaction:" + e, e); 413 rollbackTransaction(db); 414 throw new SystemException(e.getMessage()); 415 } 416 } 417 418 422 423 public void updateAttributeValue(Integer userPropertiesId, String attributeName, String attributeValue) throws SystemException, Bug 424 { 425 UserPropertiesVO userPropertiesVO = getUserPropertiesVOWithId(userPropertiesId); 426 427 if(userPropertiesVO != null) 428 { 429 try 430 { 431 logger.info("attributeName:" + attributeName); 432 logger.info("versionValue:" + userPropertiesVO.getValue()); 433 logger.info("attributeValue:" + attributeValue); 434 InputSource inputSource = new InputSource (new StringReader (userPropertiesVO.getValue())); 435 436 DOMParser parser = new DOMParser(); 437 parser.parse(inputSource); 438 Document document = parser.getDocument(); 439 440 NodeList nl = document.getDocumentElement().getChildNodes(); 441 Node attributesNode = nl.item(0); 442 443 boolean existed = false; 444 nl = attributesNode.getChildNodes(); 445 for(int i=0; i<nl.getLength(); i++) 446 { 447 Node n = nl.item(i); 448 if(n.getNodeName().equalsIgnoreCase(attributeName)) 449 { 450 if(n.getFirstChild() != null && n.getFirstChild().getNodeValue() != null) 451 { 452 n.getFirstChild().setNodeValue(attributeValue); 453 existed = true; 454 break; 455 } 456 else 457 { 458 CDATASection cdata = document.createCDATASection(attributeValue); 459 n.appendChild(cdata); 460 existed = true; 461 break; 462 } 463 } 464 } 465 466 if(existed == false) 467 { 468 org.w3c.dom.Element attributeElement = document.createElement(attributeName); 469 attributesNode.appendChild(attributeElement); 470 CDATASection cdata = document.createCDATASection(attributeValue); 471 attributeElement.appendChild(cdata); 472 } 473 474 StringBuffer sb = new StringBuffer (); 475 org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb); 476 logger.info("sb:" + sb); 477 userPropertiesVO.setValue(sb.toString()); 478 update(userPropertiesVO.getLanguageId(), userPropertiesVO.getContentTypeDefinitionId(), userPropertiesVO); 479 } 480 catch(Exception e) 481 { 482 e.printStackTrace(); 483 } 484 } 485 } 486 487 488 491 492 public String getAttributeValue(Integer userPropertiesId, String attributeName, boolean escapeHTML) throws SystemException, Bug 493 { 494 String value = ""; 495 496 UserPropertiesVO userPropertiesVO = getUserPropertiesVOWithId(userPropertiesId); 497 498 if(userPropertiesVO != null) 499 { 500 try 501 { 502 logger.info("attributeName:" + attributeName); 503 logger.info("VersionValue:" + userPropertiesVO.getValue()); 504 InputSource inputSource = new InputSource (new StringReader (userPropertiesVO.getValue())); 505 506 DOMParser parser = new DOMParser(); 507 parser.parse(inputSource); 508 Document document = parser.getDocument(); 509 510 NodeList nl = document.getDocumentElement().getChildNodes(); 511 Node n = nl.item(0); 512 513 nl = n.getChildNodes(); 514 for(int i=0; i<nl.getLength(); i++) 515 { 516 n = nl.item(i); 517 if(n.getNodeName().equalsIgnoreCase(attributeName)) 518 { 519 if(n.getFirstChild() != null && n.getFirstChild().getNodeValue() != null) 520 { 521 value = n.getFirstChild().getNodeValue(); 522 logger.info("Getting value: " + value); 523 if(value != null && escapeHTML) 524 value = new VisualFormatter().escapeHTML(value); 525 break; 526 } 527 } 528 } 529 } 530 catch(Exception e) 531 { 532 e.printStackTrace(); 533 } 534 } 535 return value; 537 } 538 539 542 543 public List getDigitalAssetVOList(Integer userPropertiesId) throws SystemException, Bug 544 { 545 Database db = CastorDatabaseService.getDatabase(); 546 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 547 548 List digitalAssetVOList = new ArrayList (); 549 550 beginTransaction(db); 551 552 try 553 { 554 UserProperties userProperties = UserPropertiesController.getController().getUserPropertiesWithId(userPropertiesId, db); 555 if(userProperties != null) 556 { 557 Collection digitalAssets = userProperties.getDigitalAssets(); 558 digitalAssetVOList = toVOList(digitalAssets); 559 } 560 561 commitTransaction(db); 562 } 563 catch(Exception e) 564 { 565 logger.info("An error occurred when we tried to fetch the list of digitalAssets belonging to this userProperties:" + e); 566 e.printStackTrace(); 567 rollbackTransaction(db); 568 throw new SystemException(e.getMessage()); 569 } 570 571 return digitalAssetVOList; 572 } 573 574 575 579 580 public BaseEntityVO getNewVO() 581 { 582 return new UserPropertiesVO(); 583 } 584 585 586 587 } 588 | Popular Tags |