KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ModelIndex.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.*;
27 import org.w3c.dom.*;
28
29 import org.ofbiz.base.util.*;
30
31 /**
32  * Generic Entity - Relation model class
33  *
34  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
35  * @version $Rev: 6327 $
36  * @since 2.0
37  */

38 public class ModelIndex extends ModelChild {
39
40     /** the index name, used for the database index name */
41     protected String JavaDoc name;
42
43     /** specifies whether or not this index should include the unique constraint */
44     protected boolean unique;
45
46     /** list of the field names included in this index */
47     protected List fieldNames = new ArrayList();
48
49     /** Default Constructor */
50     public ModelIndex() {
51         name = "";
52         unique = false;
53     }
54
55     /** Direct Create Constructor */
56     public ModelIndex(ModelEntity mainEntity, String JavaDoc name, boolean unique) {
57         super(mainEntity);
58         this.name = name;
59         this.unique = unique;
60     }
61
62     /** XML Constructor */
63     public ModelIndex(ModelEntity mainEntity, Element indexElement) {
64         super(mainEntity);
65
66         this.name = UtilXml.checkEmpty(indexElement.getAttribute("name"));
67         this.unique = "true".equals(UtilXml.checkEmpty(indexElement.getAttribute("unique")));
68
69         NodeList indexFieldList = indexElement.getElementsByTagName("index-field");
70         for (int i = 0; i < indexFieldList.getLength(); i++) {
71             Element indexFieldElement = (Element) indexFieldList.item(i);
72
73             if (indexFieldElement.getParentNode() == indexElement) {
74                 String JavaDoc fieldName = indexFieldElement.getAttribute("name");
75                 this.fieldNames.add(fieldName); }
76         }
77     }
78
79     /** the index name, used for the database index name */
80     public String JavaDoc getName() {
81         return this.name;
82     }
83
84     public void setName(String JavaDoc name) {
85         this.name = name;
86     }
87
88     /** specifies whether or not this index should include the unique constraint */
89     public boolean getUnique() {
90         return this.unique;
91     }
92
93     public void setUnique(boolean unique) {
94         this.unique = unique;
95     }
96
97     /** @deprecated
98       * the main entity of this relation */

99     public ModelEntity getMainEntity() {
100         return getModelEntity();
101     }
102
103     /** @deprecated */
104     public void setMainEntity(ModelEntity mainEntity) {
105         setModelEntity(mainEntity);
106     }
107
108     public Iterator getIndexFieldsIterator() {
109         return this.fieldNames.iterator();
110     }
111
112     public int getIndexFieldsSize() {
113         return this.fieldNames.size();
114     }
115
116     public String JavaDoc getIndexField(int index) {
117         return (String JavaDoc) this.fieldNames.get(index);
118     }
119
120     public void addIndexField(String JavaDoc fieldName) {
121         this.fieldNames.add(fieldName);
122     }
123
124     public String JavaDoc removeIndexField(int index) {
125         return (String JavaDoc) this.fieldNames.remove(index);
126     }
127
128     public Element toXmlElement(Document document) {
129         Element root = document.createElement("index");
130         root.setAttribute("name", this.getName());
131         if (this.getUnique()) {
132             root.setAttribute("unique", "true");
133         }
134
135         Iterator fnIter = this.fieldNames.iterator();
136         while (fnIter != null && fnIter.hasNext()) {
137             String JavaDoc fieldName = (String JavaDoc) fnIter.next();
138             Element fn = document.createElement("index-field");
139             fn.setAttribute("name", fieldName);
140             root.appendChild(fn);
141         }
142
143         return root;
144     }
145 }
146
Popular Tags