001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.entity.util;
019:
020: import java.io.IOException;
021: import java.net.URL;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import javax.xml.parsers.ParserConfigurationException;
026:
027: import org.ofbiz.base.util.Debug;
028: import org.ofbiz.entity.GenericDelegator;
029: import org.ofbiz.entity.GenericEntityException;
030: import org.ofbiz.entity.GenericPK;
031: import org.ofbiz.entity.GenericValue;
032: import org.ofbiz.entity.model.ModelEntity;
033: import org.xml.sax.SAXException;
034:
035: /**
036: * Some utility routines for loading seed data.
037: */
038: public class EntityDataAssert {
039:
040: public static final String module = EntityDataAssert.class
041: .getName();
042:
043: public static int assertData(URL dataUrl,
044: GenericDelegator delegator, List errorMessages)
045: throws GenericEntityException, SAXException,
046: ParserConfigurationException, IOException {
047: int rowsChecked = 0;
048:
049: if (dataUrl == null) {
050: String errMsg = "Cannot assert/check data, dataUrl was null";
051: errorMessages.add(errMsg);
052: Debug.logError(errMsg, module);
053: return 0;
054: }
055:
056: Debug.logVerbose("Loading XML Resource: "
057: + dataUrl.toExternalForm(), module);
058:
059: try {
060: List checkValueList = delegator.readXmlDocument(dataUrl);
061: Iterator checkValueIter = checkValueList.iterator();
062: while (checkValueIter.hasNext()) {
063: GenericValue checkValue = (GenericValue) checkValueIter
064: .next();
065:
066: // to check get the PK, find by that, compare all fields
067: GenericPK checkPK = checkValue.getPrimaryKey();
068: GenericValue currentValue = delegator
069: .findByPrimaryKey(checkPK);
070:
071: ModelEntity modelEntity = currentValue.getModelEntity();
072: List nonpkFieldNameList = modelEntity
073: .getNoPkFieldNames();
074: Iterator nonpkFieldNameIter = nonpkFieldNameList
075: .iterator();
076: while (nonpkFieldNameIter.hasNext()) {
077: String nonpkFieldName = (String) nonpkFieldNameIter
078: .next();
079: Object checkField = checkValue.get(nonpkFieldName);
080: Object currentField = currentValue
081: .get(nonpkFieldName);
082:
083: if (checkField != null
084: && !checkField.equals(currentField)) {
085: StringBuffer matchError = new StringBuffer();
086: matchError.append("Field ["
087: + modelEntity.getEntityName() + "."
088: + nonpkFieldName
089: + "] did not match; file value ["
090: + checkField + "], db value ["
091: + currentField + "] pk [" + checkPK
092: + "]");
093: errorMessages.add(matchError.toString());
094: }
095: }
096:
097: rowsChecked++;
098: }
099: } catch (GenericEntityException e) {
100: String xmlError = "Error checking/asserting XML Resource: "
101: + dataUrl.toExternalForm() + "; Error was: "
102: + e.getMessage();
103: Debug.logError(e, xmlError, module);
104: // instead of adding this as a message, throw the real exception; then caller has more control
105: //errorMessages.add(xmlError);
106: throw e;
107: }
108:
109: return rowsChecked;
110: }
111: }
|