KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > model > ModelKeyMap


1 /*
2  * $Id: ModelKeyMap.java 6327 2005-12-14 18:56:54Z jaz $
3  *
4  * Copyright (c) 2001-2005 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 package org.ofbiz.entity.model;
25
26 import java.util.List JavaDoc;
27
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30
31 import org.ofbiz.base.util.UtilMisc;
32 import org.ofbiz.base.util.UtilXml;
33
34
35 /**
36  * Generic Entity - KeyMap model class
37  *
38  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
39  * @version $Rev: 6327 $
40  * @since 2.0
41  */

42 public class ModelKeyMap implements java.io.Serializable JavaDoc {
43
44     /** name of the field in this entity */
45     protected String JavaDoc fieldName = "";
46
47     /** name of the field in related entity */
48     protected String JavaDoc relFieldName = "";
49
50     /** Default Constructor */
51     public ModelKeyMap() {}
52
53     /** Data Constructor, if relFieldName is null defaults to fieldName */
54     public ModelKeyMap(String JavaDoc fieldName, String JavaDoc relFieldName) {
55         this.fieldName = fieldName;
56         this.relFieldName = UtilXml.checkEmpty(relFieldName, this.fieldName);
57     }
58
59     /** XML Constructor */
60     public ModelKeyMap(Element JavaDoc keyMapElement) {
61         this.fieldName = UtilXml.checkEmpty(keyMapElement.getAttribute("field-name"));
62         // if no relFieldName is specified, use the fieldName; this is convenient for when they are named the same, which is often the case
63
this.relFieldName = UtilXml.checkEmpty(keyMapElement.getAttribute("rel-field-name"), this.fieldName);
64     }
65
66     /** name of the field in this entity */
67     public String JavaDoc getFieldName() {
68         return this.fieldName;
69     }
70
71     public void setFieldName(String JavaDoc fieldName) {
72         this.fieldName = fieldName;
73     }
74
75     /** name of the field in related entity */
76     public String JavaDoc getRelFieldName() {
77         return this.relFieldName;
78     }
79
80     public void setRelFieldName(String JavaDoc relFieldName) {
81         this.relFieldName = relFieldName;
82     }
83
84     // ======= Some Convenience Oriented Factory Methods =======
85
public static List JavaDoc makeKeyMapList(String JavaDoc fieldName1) {
86         return UtilMisc.toList(new ModelKeyMap(fieldName1, null));
87     }
88     public static List JavaDoc makeKeyMapList(String JavaDoc fieldName1, String JavaDoc relFieldName1) {
89         return UtilMisc.toList(new ModelKeyMap(fieldName1, relFieldName1));
90     }
91     public static List JavaDoc makeKeyMapList(String JavaDoc fieldName1, String JavaDoc relFieldName1, String JavaDoc fieldName2, String JavaDoc relFieldName2) {
92         return UtilMisc.toList(new ModelKeyMap(fieldName1, relFieldName1), new ModelKeyMap(fieldName2, relFieldName2));
93     }
94     public static List JavaDoc makeKeyMapList(String JavaDoc fieldName1, String JavaDoc relFieldName1, String JavaDoc fieldName2, String JavaDoc relFieldName2, String JavaDoc fieldName3, String JavaDoc relFieldName3) {
95         return UtilMisc.toList(new ModelKeyMap(fieldName1, relFieldName1), new ModelKeyMap(fieldName2, relFieldName2), new ModelKeyMap(fieldName3, relFieldName3));
96     }
97
98     public int hashCode() {
99         return this.fieldName.hashCode() + this.relFieldName.hashCode();
100     }
101
102     public boolean equals(Object JavaDoc other) {
103         ModelKeyMap otherKeyMap = (ModelKeyMap) other;
104
105         if (!otherKeyMap.fieldName.equals(this.fieldName)) return false;
106         if (!otherKeyMap.relFieldName.equals(this.relFieldName)) return false;
107
108         return true;
109     }
110
111     public Element JavaDoc toXmlElement(Document JavaDoc document) {
112         Element JavaDoc root = document.createElement("key-map");
113         root.setAttribute("field-name", this.getFieldName());
114         if (!this.getFieldName().equals(this.getRelFieldName())) {
115             root.setAttribute("rel-field-name", this.getRelFieldName());
116         }
117
118         return root;
119     }
120 }
121
Popular Tags