1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.util.regex.Pattern ; 27 28 import org.apache.log4j.Logger; 29 import org.exolab.castor.jdo.Database; 30 import org.exolab.castor.jdo.OQLQuery; 31 import org.exolab.castor.jdo.QueryResults; 32 import org.infoglue.cms.entities.kernel.BaseEntityVO; 33 import org.infoglue.cms.entities.kernel.IBaseEntity; 34 import org.infoglue.cms.exception.ConstraintException; 35 import org.infoglue.cms.exception.SystemException; 36 37 38 46 public class ValidationController extends BaseController 47 { 48 49 private final static Logger logger = Logger.getLogger(ValidationController.class.getName()); 50 51 private static final String NOTUNIQUE_FIELD_ERROR_CODE = "302"; 52 53 protected static void validateUniqueness(String value, String fieldName, Class objectClass, Integer excludeId, Object excludedObject) throws ConstraintException, SystemException 54 { 55 Pattern p = Pattern.compile("[.\\s]+"); 56 String [] arrString = p.split(fieldName); 57 String cleanField = arrString[arrString.length-1]; 58 59 if(fieldValueExists(objectClass, cleanField, value, excludeId, excludedObject)) 60 { 61 throw createConstraintException(fieldName, NOTUNIQUE_FIELD_ERROR_CODE); 62 } 63 } 64 65 private static final ConstraintException createConstraintException(String fieldName, String errorCode) 66 { 67 return new ConstraintException(fieldName, errorCode); 68 } 69 70 71 public static boolean fieldValueExists(Class objectClass, String fieldName, String checkValue, Integer excludeId, Object excludeObject) throws SystemException 72 { 73 boolean valueExist = false; 74 Database db = CastorDatabaseService.getDatabase(); 75 OQLQuery oql; 76 77 try 78 { 79 beginTransaction(db); 80 81 oql = db.getOQLQuery( "SELECT u FROM " +objectClass.getName() + " u WHERE u." + fieldName + " = $1"); 82 oql.bind(checkValue); 83 84 QueryResults results = oql.execute(); 85 logger.info("Fetching entity in read/write mode"); 86 87 if (excludeId == null && excludeObject == null) 88 valueExist = results.hasMore(); 89 else 90 { 91 while (results.hasMore()) 93 { 94 IBaseEntity o = (IBaseEntity) results.next(); 95 logger.info("Validating...." + o.getIdAsObject() + ":" + excludeObject + ":" + o.getIdAsObject().equals(excludeObject)); 96 if(excludeObject != null) 97 { 98 if (!o.getIdAsObject().equals(excludeObject)) 99 valueExist = true; 100 } 101 else 102 { 103 if (o.getId().compareTo(excludeId) != 0) 104 valueExist = true; 105 } 106 } 107 } 108 109 commitTransaction(db); 110 } 111 catch (Exception e) 112 { 113 logger.error("An error occurred so we should not complete the transaction:" + e, e); 114 rollbackTransaction(db); 115 throw new SystemException(e.getMessage()); 116 } 117 return valueExist; 118 } 119 120 123 124 public BaseEntityVO getNewVO() 125 { 126 return null; 127 } 128 129 } 130 | Popular Tags |