KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > util > EntityDataAssert


1 /*
2  * $Id: EntityDataAssert.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.entity.util;
26
27 import java.net.URL JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.ofbiz.base.util.Debug;
32 import org.ofbiz.entity.GenericDelegator;
33 import org.ofbiz.entity.GenericPK;
34 import org.ofbiz.entity.GenericValue;
35 import org.ofbiz.entity.model.ModelEntity;
36
37 /**
38  * Some utility routines for loading seed data.
39  *
40  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
41  * @version $Rev: 5462 $
42  * @since 3.3
43  */

44 public class EntityDataAssert {
45
46     public static final String JavaDoc module = EntityDataAssert.class.getName();
47
48     public static int assertData(URL JavaDoc dataUrl, GenericDelegator delegator, List JavaDoc errorMessages) {
49         int rowsChecked = 0;
50         
51         if (dataUrl == null) {
52             String JavaDoc errMsg = "Cannot assert/check data, dataUrl was null";
53             errorMessages.add(errMsg);
54             Debug.logError(errMsg, module);
55             return 0;
56         }
57
58         Debug.logVerbose("[install.loadData] Loading XML Resource: \"" + dataUrl.toExternalForm() + "\"", module);
59
60         try {
61             List JavaDoc checkValueList = delegator.readXmlDocument(dataUrl);
62             Iterator JavaDoc checkValueIter = checkValueList.iterator();
63             while (checkValueIter.hasNext()) {
64                 GenericValue checkValue = (GenericValue) checkValueIter.next();
65                 
66                 // to check get the PK, find by that, compare all fields
67
GenericPK checkPK = checkValue.getPrimaryKey();
68                 GenericValue currentValue = delegator.findByPrimaryKey(checkPK);
69                 
70                 ModelEntity modelEntity = currentValue.getModelEntity();
71                 List JavaDoc nonpkFieldNameList = modelEntity.getNoPkFieldNames();
72                 Iterator JavaDoc nonpkFieldNameIter = nonpkFieldNameList.iterator();
73                 while (nonpkFieldNameIter.hasNext()) {
74                     String JavaDoc nonpkFieldName = (String JavaDoc) nonpkFieldNameIter.next();
75                     Object JavaDoc checkField = checkValue.get(nonpkFieldName);
76                     Object JavaDoc currentField = currentValue.get(nonpkFieldName);
77                     
78                     boolean matches = false;
79                     if (checkField == null) {
80                         if (currentField == null) {
81                             matches = true;
82                         }
83                     } else {
84                         if (checkField.equals(currentField)) {
85                             matches = true;
86                         }
87                     }
88                     
89                     if (!matches) {
90                         StringBuffer JavaDoc matchError = new StringBuffer JavaDoc();
91                         matchError.append("Field [" + modelEntity.getEntityName() + "." + nonpkFieldName + "] did not match; file value [" + checkField + "], db value [" + currentField + "] pk [" + checkPK + "]");
92                         errorMessages.add(matchError.toString());
93                     }
94                 }
95                 
96                 rowsChecked++;
97             }
98         } catch (Exception JavaDoc e) {
99             String JavaDoc xmlError = "Error checking/asserting XML Resource \"" + dataUrl.toExternalForm() + "\"; Error was: " + e.getMessage();
100             errorMessages.add(xmlError);
101             Debug.logError(e, xmlError, module);
102         }
103
104         return rowsChecked;
105     }
106 }
107
Popular Tags