1 25 package org.ofbiz.entity.util; 26 27 import java.net.URL ; 28 import java.util.Iterator ; 29 import java.util.List ; 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 44 public class EntityDataAssert { 45 46 public static final String module = EntityDataAssert.class.getName(); 47 48 public static int assertData(URL dataUrl, GenericDelegator delegator, List errorMessages) { 49 int rowsChecked = 0; 50 51 if (dataUrl == null) { 52 String 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 checkValueList = delegator.readXmlDocument(dataUrl); 62 Iterator checkValueIter = checkValueList.iterator(); 63 while (checkValueIter.hasNext()) { 64 GenericValue checkValue = (GenericValue) checkValueIter.next(); 65 66 GenericPK checkPK = checkValue.getPrimaryKey(); 68 GenericValue currentValue = delegator.findByPrimaryKey(checkPK); 69 70 ModelEntity modelEntity = currentValue.getModelEntity(); 71 List nonpkFieldNameList = modelEntity.getNoPkFieldNames(); 72 Iterator nonpkFieldNameIter = nonpkFieldNameList.iterator(); 73 while (nonpkFieldNameIter.hasNext()) { 74 String nonpkFieldName = (String ) nonpkFieldNameIter.next(); 75 Object checkField = checkValue.get(nonpkFieldName); 76 Object 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 matchError = new StringBuffer (); 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 e) { 99 String 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 |