logger.error(message);
throw new MolgenisModelException(message);
}
// construct
Entity entity = new Entity(element.getAttribute("name").trim(), element.getAttribute("label"),
model.getDatabase());
entity.setNamespace(model.getName());
// add optional properties
// EXTENDS exactly one
String _extends = element.getAttribute("extends");
if (_extends != null)
{
Vector<String> parents = new Vector<String>();
StringTokenizer tokenizer = new StringTokenizer(_extends, ",");
while (tokenizer.hasMoreTokens())
{
parents.add(tokenizer.nextToken().trim());
}
entity.setParents(parents);
}
// IMPLEMENTS
String _implements = element.getAttribute("implements");
if (_implements != null && !_implements.isEmpty())
{
entity.setImplements(new Vector<String>(Arrays.asList(_implements.split(","))));
}
// ABSTRACT
entity.setAbstract(Boolean.parseBoolean(element.getAttribute("abstract")));
// SYSTEM
entity.setSystem(Boolean.parseBoolean(element.getAttribute("system")));
// XREF_LABEL
String xref_label = element.getAttribute("xref_label");
if (xref_label != null && !xref_label.isEmpty())
{
List<String> xref_labels = new ArrayList<String>();
xref_labels.addAll(Arrays.asList(xref_label.split(",")));
entity.setXrefLabels(xref_labels);
}
else
{
entity.setXrefLabels(null);
}
// TRIGGER
if (element.hasAttribute("decorator"))
{
entity.setDecorator(element.getAttribute("decorator"));
}
// DESCRIPTION
NodeList elements = element.getElementsByTagName("description");
for (int j = 0; j < elements.getLength(); j++)
{
// parse the contents, including markup...
entity.setDescription(elementValueToString((Element) elements.item(j)));
}
// FIELD
elements = element.getElementsByTagName("field");
for (int j = 0; j < elements.getLength(); j++)
{
Element elem = (Element) elements.item(j);
parseField(entity, elem);
}
// UNIQUE
elements = element.getElementsByTagName("unique");
for (int j = 0; j < elements.getLength(); j++)
{
Element elem = (Element) elements.item(j);
Vector<String> keys = new Vector<String>();
// keys from keyfield="a,b" attribute
if (elem.hasAttribute("fields"))
{
for (String name : elem.getAttribute("fields").split(","))
{
// Field f = entity.getField(name);
// if (f == null)
// {
// // try to get superclass field (need to copy it then)
// f = entity.getAllField(name);
// if (f == null) throw new
// MolgenisModelException("Missing unique field '" + name
// + "' in entity '" + entity.getName() + "'");
//
// // copy the field so it will end up in the table (as
// // redundant copy) to enforce unique
// // f = new Field(f);
// // f.setEntity(entity);
// // f.setSystem(true);
// // entity.addField(f);
// }
keys.add(name);
}
}
// keys from <keyfield> elements
NodeList key_elements = elem.getElementsByTagName("keyfield");
for (int k = 0; k < key_elements.getLength(); k++)
{
elem = (Element) key_elements.item(k);
String name = elem.getAttribute("name");
// should include superclass methods now
// if (f == null)
// {
// throw new MolgenisModelException("Missing unique field: " +
// elem.getAttribute("name"));
// // return null;
// }
keys.add(name);
}
// description
String key_description = null;
if (elem.hasAttribute("description"))
{
key_description = elem.getAttribute("description");
}
// check if keys
if (keys.size() == 0)
{
throw new MolgenisModelException("missing fields on unique of '" + entity.getName()
+ "'. Expected <unique fields=\"field1[,field2,..]\" description=\"...\"/>");
}
try
{
entity.addKey(keys, elem.getAttribute("subclass").equals("true"), key_description);
// might be duplicate key
}
catch (Exception e)
{
throw new MolgenisModelException(e.getMessage());
}
}
elements = element.getElementsByTagName("indices");
if (elements.getLength() == 1)
{
Element elem = (Element) elements.item(0);
NodeList index_elements = elem.getElementsByTagName("index");
for (int k = 0; k < index_elements.getLength(); k++)
{
elem = (Element) index_elements.item(k);
Index index = new Index(elem.getAttribute("name"));
NodeList indexfield_elements = elem.getElementsByTagName("indexfield");
for (int l = 0; l < indexfield_elements.getLength(); l++)
{
elem = (Element) indexfield_elements.item(l);
Field f = entity.getField(elem.getAttribute("name"));
if (f == null)
{
// System.err.println(String.format(Error.
// MISSING_INDEX_FIELD.msg,
// elem.getAttribute("name")));
throw new MolgenisModelException("Missing index field: " + elem.getAttribute("name"));
// return null;
}
try
{
index.addField(elem.getAttribute("name"));
}
catch (Exception e)
{
throw new MolgenisModelException(e.getMessage());
}
}
try
{
entity.addIndex(index);
}
catch (Exception e)
{
}
}
}
else if (elements.getLength() > 1)
{
// System.err.println(Error.MULTIPLE_INDICES_ELEMENTS.msg);
// return null;
throw new MolgenisModelException("Multiple indices elements");
}
// done
// Todo: change if(molgenisOptions.jpa_use_sequence &&
// element.hasAttribute("allocationSize"))
if (element.hasAttribute("allocationSize"))
{
int allocationSize = Integer.parseInt(element.getAttribute("allocationSize"));
entity.setAllocationSize(allocationSize);
}
logger.debug("read: " + entity.getName());
return entity;
}