Package org.molgenis.model.elements

Examples of org.molgenis.model.elements.Entity


      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;
  }
View Full Code Here


      logger.error(message);
      throw new MolgenisModelException(message);
    }

    // construct
    Entity entity = model.getEntity(element.getAttribute("type"));

    try
    {
      method.setReturnType(entity);
    }
View Full Code Here

    // ENTITY
    elements = element.getElementsByTagName("entity");
    for (int j = 0; j < elements.getLength(); j++)
    {
      Element elem = (Element) elements.item(j);
      Entity e = parseEntity(model, elem);
      e.setNamespace(module.getName());
      module.getEntities().add(e);
      e.setModule(module);
    }
  }
View Full Code Here

          form.setReadOnly(Boolean.parseBoolean(readonly));
        }

        // ENTITY
        // TODO: whould have expected this in the constructor!
        Entity entity = (Entity) model.getDatabase().getChild(element.getAttribute("entity"));
        if (entity == null)
        {
          throw new MolgenisModelException("Could not find the specified entity '"
              + element.getAttribute("entity") + "' for form '" + form.getName() + "'");
        }
        form.setRecord((Record) entity);// form.setEntity(entity);

        // HIDDEN FIELDS
        form.setHideFields(new ArrayList<String>());
        String hide_fields = element.getAttribute("hide_fields");
        if (hide_fields != null && !hide_fields.isEmpty())
        {
          String[] hiddenFieldArray = hide_fields.split(",");
          for (String field : hiddenFieldArray)
          {
            Field f = entity.getAllField(field.trim());
            if (f == null)
            {
              throw new MolgenisModelException("Could not find field '" + field
                  + "' defined in hide_fields='" + element.getAttribute("hide_fields")
                  + "' in form '" + form.getName() + "'");
            }
            // use name from 'f' to correct for case problems
            form.getHideFields().add(f.getName());
          }
        }

        // COMPACT_FIELDS
        if (!element.getAttribute("compact_view").isEmpty())
        {
          String[] fields = element.getAttribute("compact_view").split(",");
          // check if the fields are there
          List<String> compact_fields = new ArrayList<String>();
          for (String field : fields)
          {
            Field f = entity.getAllField(field);
            if (f == null)
            {
              throw new MolgenisModelException("Could not find field '" + field
                  + "' defined in compact_view='" + element.getAttribute("compact_view")
                  + "' in form '" + form.getName() + "'");
View Full Code Here

    for (org.molgenis.model.elements.Form form : model.getUserinterface().getAllForms())
    {
      List<String> hideFields = form.getHideFields();
      for (String fieldName : hideFields)
      {
        Entity entity = form.getEntity();
        Field field = entity.getAllField(fieldName);
        if (field == null)
        {
          throw new MolgenisModelException("error in hide_fields for form name=" + form.getName()
              + ": cannot find field '" + fieldName + "' in form entity='" + entity.getName() + "'");
        }
        else
        {
          if (!form.getReadOnly() && field.isNillable() == false && !field.isAuto()
              && field.getDefaultValue().equals(""))
View Full Code Here

      {
        if (f.getType() instanceof XrefField || f.getType() instanceof MrefField)
        {
          if (f.getXrefLabelNames().size() > 0 && f.getXrefLabelNames().get(0).equals(f.getXrefFieldName()))
          {
            Entity xref_entity = f.getXrefEntity();
            if (xref_entity.getXrefLabels() != null)
            {
              logger.debug("copying xref_label " + xref_entity.getXrefLabels() + " from "
                  + f.getXrefEntityName() + " to field " + f.getEntity().getName() + "."
                  + f.getName());
              f.setXrefLabelNames(xref_entity.getXrefLabels());
            }
          }
        }
      }
    }
View Full Code Here

      for (Field xref_field_from : xref_entity_from.getImplementedFieldsOf(new MrefField()))
      {
        try
        {
          // retrieve the references to the entity+field
          Entity xref_entity_to = xref_field_from.getXrefEntity();
          Field xref_field_to = xref_field_from.getXrefField();

          // TODO: check whether this link is already present

          // create the new entity for the link, if explicitly named
          String mref_name = xref_field_from.getMrefName(); // explicit

          // if mref_name longer than 30 throw error
          if (options.db_driver.toLowerCase().contains("oracle") && mref_name.length() > 30)
          {
            throw new MolgenisModelException("mref_name cannot be longer then 30 characters, found: "
                + mref_name);
          }

          // check if the mref already exists
          Entity mrefEntity = null;
          try
          {
            mrefEntity = model.getEntity(mref_name);
          }
          catch (Exception e)
          {
          }

          // if mref entity doesn't exist: create
          if (mrefEntity == null)
          {
            mrefEntity = new Entity(mref_name, mref_name, model.getDatabase());
            mrefEntity.setNamespace(xref_entity_from.getNamespace());
            mrefEntity.setAssociation(true);
            mrefEntity.setDescription("Link table for many-to-many relationship '"
                + xref_entity_from.getName() + "." + xref_field_from.getName() + "'.");
            mrefEntity.setSystem(true);

            // create id field to ensure ordering
            Field idField = new Field(mrefEntity, new IntField(), "autoid", "autoid", true, false, false,
                null);
            idField.setHidden(true);
            idField.setDescription("automatic id field to ensure ordering of mrefs");
            mrefEntity.addField(idField);
            mrefEntity.addKey(idField.getName(), "unique auto key to ensure ordering of mrefs");

            // create the fields for the linktable
            Field field;
            Vector<String> unique = new Vector<String>();

            field = new Field(mrefEntity, new XrefField(), xref_field_from.getMrefRemoteid(), null, false,
                false, false, null);
            field.setXRefVariables(xref_entity_to.getName(), xref_field_to.getName(),
                xref_field_from.getXrefLabelNames());
            if (xref_field_from.isXrefCascade()) field.setXrefCascade(true);
            mrefEntity.addField(field);

            unique.add(field.getName());

            // add all the key-fields of xref_entity_from
            for (Field key : xref_entity_from.getKeyFields(Entity.PRIMARY_KEY))
            {
              field = new Field(mrefEntity, new XrefField(), xref_field_from.getMrefLocalid(), null,
                  false, false, false, null);

              // null xreflabel
              field.setXRefVariables(xref_entity_from.getName(), key.getName(), null);

              mrefEntity.addField(field);
              unique.add(field.getName());
            }

            // create the unique combination
            mrefEntity.addKey(unique, false, null);

          }
          // if mrefEntity does not exist, check xref_labels
          else
          {
            // field is xref_field, does it have label(s)?
            Field xrefField = mrefEntity.getAllField(xref_field_to.getName());

            // verify xref_label
            if (xrefField != null)
            {
              // logger.debug("adding xref_label "+xref_field_to.getXrefLabelNames()+"'back' for "+xrefField.getName());
              xrefField.setXrefLabelNames(xref_field_from.getXrefLabelNames());

            }
          }

          // set the linktable reference in the xref-field
          xref_field_from.setMrefName(mrefEntity.getName());
        }
        catch (Exception e)
        {
          e.printStackTrace();
          System.exit(-1);
View Full Code Here

      Vector<Pair<Entity, Entity>> references = new Vector<Pair<Entity, Entity>>();

      // retrieve all the entities
      for (String viewentity : view.getEntities())
      {
        Entity entity = model.getEntity(viewentity);
        if (entity == null) throw new MolgenisModelException("Entity '" + viewentity + "' in view '"
            + view.getName() + "' does not exist");

        entities.add(entity);
      }

      // validate that there are xref's pointing to the respective
      // entities
      for (Entity entity : entities)
      {
        for (Field field : entity.getFields())
        {
          if (!(field.getType() instanceof XrefField)) continue;

          // get the entity, which is referenced by the field
          Entity referenced = null;
          try
          {
            referenced = field.getXrefEntity();
          }
          catch (Exception e)
          {
            ;
          }

          // check whether we're referencing one of the other entities
          // in the view
          for (Entity other : entities)
          {
            // exclude ourselves
            if (other.getName().equals(entity.getName())) continue;

            // check whether this is an entity we're referencing
            if (other.getName().equals(referenced.getName())) references.add(new Pair<Entity, Entity>(
                entity, other));
          }
        }
      }
View Full Code Here

          if (xref_label_names.size() == 0)
          {
            xref_label_names.add(field.getXrefFieldName());
          }

          Entity xref_entity = model.getEntity(xref_entity_name);
          if (xref_entity == null) throw new MolgenisModelException("xref entity '" + xref_entity_name
              + "' does not exist for field " + entityname + "." + fieldname);

          if (xref_field_name == null || xref_field_name.equals(""))
          {
            xref_field_name = xref_entity.getPrimaryKey().getName();
            field.setXrefField(xref_field_name);

            logger.debug("automatically set " + entityname + "." + fieldname + " xref_field="
                + xref_field_name);
          }

          if (!xref_entity.getName().equals(field.getXrefEntityName())) throw new MolgenisModelException(
              "xref entity '" + xref_entity_name + "' does not exist for field " + entityname + "."
                  + fieldname + " (note: entity names are case-sensitive)");

          if (xref_entity.isAbstract())
          {
            throw new MolgenisModelException("cannot refer to abstract xref entity '" + xref_entity_name
                + "' from field " + entityname + "." + fieldname);
          }

          // if (entity.isAbstract()
          // && field.getType() instanceof MrefField) throw new
          // MolgenisModelException(
          // "interfaces cannot have mref therefore remove '"
          // + entityname + "." + fieldname + "'");

          Field xref_field = xref_entity.getField(xref_field_name, false, true, true);

          if (xref_field == null) throw new MolgenisModelException("xref field '" + xref_field_name
              + "' does not exist for field " + entityname + "." + fieldname);

          // if (xref_field == null) xref_field =
          // xref_entity.getPrimaryKey();
          // throw new MolgenisModelException("xref field '" +
          // xref_field_name
          // + "' does not exist for field " + entityname + "." +
          // fieldname);

          for (String xref_label_name : xref_label_names)
          {
            Field xref_label = null;
            // test if label is defined as {entity}.{field}
            if (xref_label_name.contains("."))
            {
              xref_label = model.findField(xref_label_name);
            }
            // else assume {entity} == xref_entity
            else
            {
              xref_label = xref_entity.getAllField(xref_label_name);
            }
            // if null, check if a path to another xref_label:
            // 'fieldname_xreflabel'
            if (xref_label == null)
            {
              StringBuilder validFieldsBuilder = new StringBuilder();
              Map<String, List<Field>> candidates = field.allPossibleXrefLabels();

              if (candidates.size() == 0)
              {
                throw new MolgenisModelException(
                    "xref label '"
                        + xref_label_name
                        + "' does not exist for field "
                        + entityname
                        + "."
                        + fieldname
                        + ". \nCouldn't find suitable secondary keys to use as xref_label. \nDid you set a unique=\"true\" or <unique fields=\" ...>?");
              }

              for (Entry<String, List<Field>> entry : candidates.entrySet())
              {
                String key = entry.getKey();
                if (xref_label_name.equals(key))
                {
                  List<Field> value = entry.getValue();
                  xref_label = value.get(value.size() - 1);
                }
                validFieldsBuilder.append(',').append(key);
              }

              // still null, must be error
              if (xref_label == null)
              {
                throw new MolgenisModelException("xref label '" + xref_label_name
                    + "' does not exist for field " + entityname + "." + fieldname
                    + ". Valid labels include " + validFieldsBuilder.toString());
              }

            }
            else
            {
              // validate the label

              if (!xref_label_name.equals(xref_field_name)
                  && !field.allPossibleXrefLabels().keySet().contains(xref_label_name))
              {
                String validLabels = StringUtils.join(field.allPossibleXrefLabels().keySet(), ',');
                throw new MolgenisModelException("xref label '" + xref_label_name + "' for "
                    + entityname + "." + fieldname
                    + " is not part a secondary key. Valid labels are " + validLabels
                    + "\nDid you set a unique=\"true\" or <unique fields=\" ...>?");
              }

            }

          }

          if (xref_field.getType() instanceof TextField) throw new MolgenisModelException("xref field '"
              + xref_field_name + "' is of illegal type 'TEXT' for field " + entityname + "." + fieldname);

          boolean isunique = false;
          for (Unique unique : xref_entity.getAllKeys())
          {
            for (Field keyfield : unique.getFields())
            {
              if (keyfield.getName().equals(xref_field_name)) isunique = true;
            }
          }
          if (!isunique) throw new MolgenisModelException("xref pointer '" + xref_entity_name + "."
              + xref_field_name + "' is a non-unique field for field " + entityname + "." + fieldname
              + "\n" + xref_entity.toString());
        }
      }
    }
  }
View Full Code Here

      }

      Vector<String> parents = entity.getParents();
      if (parents.size() != 0)
      {
        Entity parent = model.getEntity(parents.get(0));
        if (parent == null) throw new MolgenisModelException("superclass '" + parents.get(0) + "' for '"
            + entity.getName() + "' is missing");
        if (parent.isAbstract()) throw new MolgenisModelException(entity.getName() + " cannot extend "
            + parents.get(0) + " because superclas " + parents.get(0) + " is abstract (use implements)");
        if (entity.isAbstract()) throw new MolgenisModelException(entity.getName() + " cannot extend "
            + parents.get(0) + " because " + entity.getName() + " itself is abstract");

        if (parent.getKeys().size() == 0)
        {
          // log.out("panix");
          continue;

        }
View Full Code Here

TOP

Related Classes of org.molgenis.model.elements.Entity

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.