001: /*
002: * $Id: ModelIndex.java,v 1.2 2003/12/11 01:58:19 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.entity.model;
025:
026: import java.util.*;
027: import org.w3c.dom.*;
028:
029: import org.ofbiz.base.util.*;
030:
031: /**
032: * Generic Entity - Relation model class
033: *
034: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
035: * @version $Revision: 1.2 $
036: * @since 2.0
037: */
038: public class ModelIndex {
039:
040: /** reference to the entity this index refers to */
041: protected ModelEntity mainEntity;
042:
043: /** the index name, used for the database index name */
044: protected String name;
045:
046: /** specifies whether or not this index should include the unique constraint */
047: protected boolean unique;
048:
049: /** list of the field names included in this index */
050: protected List fieldNames = new ArrayList();
051:
052: /** Default Constructor */
053: public ModelIndex() {
054: name = "";
055: unique = false;
056: }
057:
058: /** Direct Create Constructor */
059: public ModelIndex(ModelEntity mainEntity, String name,
060: boolean unique) {
061: this .mainEntity = mainEntity;
062: this .name = name;
063: this .unique = unique;
064: }
065:
066: /** XML Constructor */
067: public ModelIndex(ModelEntity mainEntity, Element indexElement) {
068: this .mainEntity = mainEntity;
069:
070: this .name = UtilXml.checkEmpty(indexElement
071: .getAttribute("name"));
072: this .unique = "true".equals(UtilXml.checkEmpty(indexElement
073: .getAttribute("unique")));
074:
075: NodeList indexFieldList = indexElement
076: .getElementsByTagName("index-field");
077: for (int i = 0; i < indexFieldList.getLength(); i++) {
078: Element indexFieldElement = (Element) indexFieldList
079: .item(i);
080:
081: if (indexFieldElement.getParentNode() == indexElement) {
082: String fieldName = indexFieldElement
083: .getAttribute("name");
084: this .fieldNames.add(fieldName);
085: }
086: }
087: }
088:
089: /** the index name, used for the database index name */
090: public String getName() {
091: return this .name;
092: }
093:
094: public void setName(String name) {
095: this .name = name;
096: }
097:
098: /** specifies whether or not this index should include the unique constraint */
099: public boolean getUnique() {
100: return this .unique;
101: }
102:
103: public void setUnique(boolean unique) {
104: this .unique = unique;
105: }
106:
107: /** the main entity of this relation */
108: public ModelEntity getMainEntity() {
109: return this .mainEntity;
110: }
111:
112: public void setMainEntity(ModelEntity mainEntity) {
113: this .mainEntity = mainEntity;
114: }
115:
116: public Iterator getIndexFieldsIterator() {
117: return this .fieldNames.iterator();
118: }
119:
120: public int getIndexFieldsSize() {
121: return this .fieldNames.size();
122: }
123:
124: public String getIndexField(int index) {
125: return (String) this .fieldNames.get(index);
126: }
127:
128: public void addIndexField(String fieldName) {
129: this .fieldNames.add(fieldName);
130: }
131:
132: public String removeIndexField(int index) {
133: return (String) this.fieldNames.remove(index);
134: }
135: }
|