jpaCascade = element.getAttribute("jpaCascade");
}
}
// construct
Field field = new Field(entity, MolgenisFieldTypes.getType(type), name, label, Boolean.parseBoolean(auto),
Boolean.parseBoolean(nillable), Boolean.parseBoolean(readonly), default_value, jpaCascade);
logger.debug("read: " + field.toString());
// add optional properties
if (!description.isEmpty())
{
field.setDescription(description.trim());
}
if (hidden.equals("true"))
{
field.setHidden(true);
}
if (!default_code.isEmpty())
{
field.setDefaultCode(default_code);
}
if (filter.equals("true"))
{
logger.warn("filter set for field '" + name + "' of entity '" + entity.getName() + "'");
logger.warn(filterfield + " " + filtertype + " " + filtervalue);
logger.warn(System.currentTimeMillis() + " - filter bool: '" + Boolean.parseBoolean(filter) + "'");
if ((filtertype != null && filtertype.isEmpty()) || (filterfield != null && filterfield.isEmpty()))
{
throw new MolgenisModelException("field '" + name + "' of entity '" + entity.getName()
+ "': when the filter is set to true, the filtertype, filterfield and filtervalue must be set");
}
if (filtervalue != null && filtervalue.isEmpty())
{
logger.warn("no value specified for filter in field '" + name + "' of entity '" + entity.getName()
+ "'");
}
field.setFilter(Boolean.parseBoolean(filter));
field.setFiltertype(filtertype);
field.setFilterfield(filterfield);
field.setFiltervalue(filtervalue);
}
// add type dependent properties
if (type.equals("string"))
{
if (!length.isEmpty())
{
field.setVarCharLength(Integer.parseInt(length));
}
else
{
field.setVarCharLength(255);
}
}
else if (type.equals("enum"))
{
Vector<String> options = new Vector<String>();
StringTokenizer tokenizer = new StringTokenizer(enum_options, ",");
while (tokenizer.hasMoreElements())
{
options.add(tokenizer.nextToken().trim());
}
if (options.size() < 1)
{
throw new MolgenisModelException("enum_options must be ',' delimited for field '" + field.getName()
+ "' of entity '" + entity.getName() + "'");
}
field.setEnumOptions(options);
}
else if (type.equals("xref") || type.equals("mref"))
{
// xref must be defined unless mref_name is set
// caveat, can be both ends!!!
if (mref_name.isEmpty() && xref_entity.isEmpty())
{
throw new MolgenisModelException("xref_entity must be set for xref field '" + field.getName()
+ "' of entity '" + entity.getName() + "'");
}
List<String> xref_labels = null;
if (xref_label != null)
{
xref_labels = Arrays.asList(xref_label.split(","));
}
field.setXRefVariables(xref_entity, xref_field, xref_labels);
// optional custom naming instead of default
// necessary when using existing database
if (type.equals("mref"))
{
if (!mref_name.isEmpty())
{
field.setMrefName(mref_name);
}
if (!mref_localid.isEmpty())
{
field.setMrefLocalid(mref_localid);
}
if (!mref_remoteid.isEmpty())
{
field.setMrefRemoteid(mref_remoteid);
}
}
if (!element.getAttribute("xref_cascade").isEmpty())
{
if (element.getAttribute("xref_cascade").equalsIgnoreCase("true"))
{
field.setXrefCascade(true);
}
else
{
throw new MolgenisModelException("Unknown option on xref_cascade: '"
+ element.getAttribute("xref_cascade") + "'");
}
}
}
// add the field to entity
try
{
entity.addField(field);
}
catch (Exception e)
{
throw new MolgenisModelException("duplicate field '" + field.getName() + "' in entity '" + entity.getName()
+ "'");
}
// check whether this field has a short-hand for index
if (index.equals("true"))
{
Index i = new Index(name);
try
{
i.addField(name);
}
catch (Exception e)
{
throw new MolgenisModelException("duplicate field '" + field.getName() + "' in entity '"
+ entity.getName() + "'");
}
entity.addIndex(i);
}
// check whether this field has a short-hand for unique
if (unique.equals("true"))
{
entity.addKey(field.getName(), null);
}
// parse annotations (used by JPA)
if (element.getChildNodes().getLength() >= 1)
{
String annotations = org.apache.commons.lang.StringUtils.deleteWhitespace(
element.getChildNodes().item(1).getTextContent()).trim();
field.setAnnotations(annotations);
}
}