KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > ValidationController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.controllers.kernel.impl.simple;
25
26 import java.util.regex.Pattern JavaDoc;
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 /**
39  * @author Stefan Sik, ss@frovi.com
40  *
41  * ValidationController
42  * Provides validation functionality for valueobjects
43  *
44  *
45  */

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 JavaDoc NOTUNIQUE_FIELD_ERROR_CODE = "302";
52
53     protected static void validateUniqueness(String JavaDoc value, String JavaDoc fieldName, Class JavaDoc objectClass, Integer JavaDoc excludeId, Object JavaDoc excludedObject) throws ConstraintException, SystemException
54     {
55         Pattern JavaDoc p = Pattern.compile("[.\\s]+");
56         String JavaDoc[] arrString = p.split(fieldName);
57         String JavaDoc 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 JavaDoc fieldName, String JavaDoc errorCode)
66     {
67         return new ConstraintException(fieldName, errorCode);
68     }
69
70
71     public static boolean fieldValueExists(Class JavaDoc objectClass, String JavaDoc fieldName, String JavaDoc checkValue, Integer JavaDoc excludeId, Object JavaDoc 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                 // Check for excluded object
92
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 JavaDoc 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     /**
121      * This is a method that never should be called.
122      */

123
124     public BaseEntityVO getNewVO()
125     {
126         return null;
127     }
128
129 }
130
Popular Tags