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.model;
019:
020: import java.util.List;
021:
022: import org.w3c.dom.Document;
023: import org.w3c.dom.Element;
024:
025: import org.ofbiz.base.util.UtilMisc;
026: import org.ofbiz.base.util.UtilXml;
027:
028: /**
029: * Generic Entity - KeyMap model class
030: *
031: */
032: public class ModelKeyMap implements java.io.Serializable {
033:
034: /** name of the field in this entity */
035: protected String fieldName = "";
036:
037: /** name of the field in related entity */
038: protected String relFieldName = "";
039:
040: /** Default Constructor */
041: public ModelKeyMap() {
042: }
043:
044: /** Data Constructor, if relFieldName is null defaults to fieldName */
045: public ModelKeyMap(String fieldName, String relFieldName) {
046: this .fieldName = fieldName;
047: this .relFieldName = UtilXml.checkEmpty(relFieldName,
048: this .fieldName);
049: }
050:
051: /** XML Constructor */
052: public ModelKeyMap(Element keyMapElement) {
053: this .fieldName = UtilXml.checkEmpty(keyMapElement
054: .getAttribute("field-name"));
055: // if no relFieldName is specified, use the fieldName; this is convenient for when they are named the same, which is often the case
056: this .relFieldName = UtilXml.checkEmpty(keyMapElement
057: .getAttribute("rel-field-name"), this .fieldName);
058: }
059:
060: /** name of the field in this entity */
061: public String getFieldName() {
062: return this .fieldName;
063: }
064:
065: public void setFieldName(String fieldName) {
066: this .fieldName = fieldName;
067: }
068:
069: /** name of the field in related entity */
070: public String getRelFieldName() {
071: return this .relFieldName;
072: }
073:
074: public void setRelFieldName(String relFieldName) {
075: this .relFieldName = relFieldName;
076: }
077:
078: // ======= Some Convenience Oriented Factory Methods =======
079: public static List makeKeyMapList(String fieldName1) {
080: return UtilMisc.toList(new ModelKeyMap(fieldName1, null));
081: }
082:
083: public static List makeKeyMapList(String fieldName1,
084: String relFieldName1) {
085: return UtilMisc.toList(new ModelKeyMap(fieldName1,
086: relFieldName1));
087: }
088:
089: public static List makeKeyMapList(String fieldName1,
090: String relFieldName1, String fieldName2,
091: String relFieldName2) {
092: return UtilMisc.toList(new ModelKeyMap(fieldName1,
093: relFieldName1), new ModelKeyMap(fieldName2,
094: relFieldName2));
095: }
096:
097: public static List makeKeyMapList(String fieldName1,
098: String relFieldName1, String fieldName2,
099: String relFieldName2, String fieldName3,
100: String relFieldName3) {
101: return UtilMisc.toList(new ModelKeyMap(fieldName1,
102: relFieldName1), new ModelKeyMap(fieldName2,
103: relFieldName2), new ModelKeyMap(fieldName3,
104: relFieldName3));
105: }
106:
107: public int hashCode() {
108: return this .fieldName.hashCode() + this .relFieldName.hashCode();
109: }
110:
111: public boolean equals(Object other) {
112: ModelKeyMap otherKeyMap = (ModelKeyMap) other;
113:
114: if (!otherKeyMap.fieldName.equals(this .fieldName))
115: return false;
116: if (!otherKeyMap.relFieldName.equals(this .relFieldName))
117: return false;
118:
119: return true;
120: }
121:
122: public Element toXmlElement(Document document) {
123: Element root = document.createElement("key-map");
124: root.setAttribute("field-name", this .getFieldName());
125: if (!this .getFieldName().equals(this .getRelFieldName())) {
126: root.setAttribute("rel-field-name", this.getRelFieldName());
127: }
128:
129: return root;
130: }
131: }
|