1 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 38 public class ModelIndex extends ModelChild { 39 40 41 protected String name; 42 43 44 protected boolean unique; 45 46 47 protected List fieldNames = new ArrayList(); 48 49 50 public ModelIndex() { 51 name = ""; 52 unique = false; 53 } 54 55 56 public ModelIndex(ModelEntity mainEntity, String name, boolean unique) { 57 super(mainEntity); 58 this.name = name; 59 this.unique = unique; 60 } 61 62 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 fieldName = indexFieldElement.getAttribute("name"); 75 this.fieldNames.add(fieldName); } 76 } 77 } 78 79 80 public String getName() { 81 return this.name; 82 } 83 84 public void setName(String name) { 85 this.name = name; 86 } 87 88 89 public boolean getUnique() { 90 return this.unique; 91 } 92 93 public void setUnique(boolean unique) { 94 this.unique = unique; 95 } 96 97 99 public ModelEntity getMainEntity() { 100 return getModelEntity(); 101 } 102 103 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 getIndexField(int index) { 117 return (String ) this.fieldNames.get(index); 118 } 119 120 public void addIndexField(String fieldName) { 121 this.fieldNames.add(fieldName); 122 } 123 124 public String removeIndexField(int index) { 125 return (String ) 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 fieldName = (String ) 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 |