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.*;
021: import org.w3c.dom.*;
022:
023: import org.ofbiz.base.util.*;
024:
025: /**
026: * Generic Entity - Relation model class
027: *
028: */
029: public class ModelIndex extends ModelChild {
030:
031: /** the index name, used for the database index name */
032: protected String name;
033:
034: /** specifies whether or not this index should include the unique constraint */
035: protected boolean unique;
036:
037: /** list of the field names included in this index */
038: protected List fieldNames = new ArrayList();
039:
040: /** Default Constructor */
041: public ModelIndex() {
042: name = "";
043: unique = false;
044: }
045:
046: /** Direct Create Constructor */
047: public ModelIndex(ModelEntity mainEntity, String name,
048: boolean unique) {
049: super (mainEntity);
050: this .name = name;
051: this .unique = unique;
052: }
053:
054: /** XML Constructor */
055: public ModelIndex(ModelEntity mainEntity, Element indexElement) {
056: super (mainEntity);
057:
058: this .name = UtilXml.checkEmpty(indexElement
059: .getAttribute("name"));
060: this .unique = "true".equals(UtilXml.checkEmpty(indexElement
061: .getAttribute("unique")));
062:
063: NodeList indexFieldList = indexElement
064: .getElementsByTagName("index-field");
065: for (int i = 0; i < indexFieldList.getLength(); i++) {
066: Element indexFieldElement = (Element) indexFieldList
067: .item(i);
068:
069: if (indexFieldElement.getParentNode() == indexElement) {
070: String fieldName = indexFieldElement
071: .getAttribute("name");
072: this .fieldNames.add(fieldName);
073: }
074: }
075: }
076:
077: /** the index name, used for the database index name */
078: public String getName() {
079: return this .name;
080: }
081:
082: public void setName(String name) {
083: this .name = name;
084: }
085:
086: /** specifies whether or not this index should include the unique constraint */
087: public boolean getUnique() {
088: return this .unique;
089: }
090:
091: public void setUnique(boolean unique) {
092: this .unique = unique;
093: }
094:
095: /** @deprecated
096: * the main entity of this relation */
097: public ModelEntity getMainEntity() {
098: return getModelEntity();
099: }
100:
101: /** @deprecated */
102: public void setMainEntity(ModelEntity mainEntity) {
103: setModelEntity(mainEntity);
104: }
105:
106: public Iterator getIndexFieldsIterator() {
107: return this .fieldNames.iterator();
108: }
109:
110: public int getIndexFieldsSize() {
111: return this .fieldNames.size();
112: }
113:
114: public String getIndexField(int index) {
115: return (String) this .fieldNames.get(index);
116: }
117:
118: public void addIndexField(String fieldName) {
119: this .fieldNames.add(fieldName);
120: }
121:
122: public String removeIndexField(int index) {
123: return (String) this .fieldNames.remove(index);
124: }
125:
126: public Element toXmlElement(Document document) {
127: Element root = document.createElement("index");
128: root.setAttribute("name", this .getName());
129: if (this .getUnique()) {
130: root.setAttribute("unique", "true");
131: }
132:
133: Iterator fnIter = this .fieldNames.iterator();
134: while (fnIter != null && fnIter.hasNext()) {
135: String fieldName = (String) fnIter.next();
136: Element fn = document.createElement("index-field");
137: fn.setAttribute("name", fieldName);
138: root.appendChild(fn);
139: }
140:
141: return root;
142: }
143: }
|