Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package jpaoletti.jpm.core;
  
  import java.util.*;
An Entity is the visual representation and operation over a class of a data model. One entity is configured through an xml file.

Simple entity configuration file

 <?xml version='1.0' ?>
 <!DOCTYPE entity SYSTEM "entity.dtd">
 <entity id="entity_id" clazz="the.entity.Class" cached="true/false">
     <auditable>false</auditable>
     ...
     <operations>
     ...
     </operations>
     <field>
     ...
     </field>
     <field>
     ...
     </field>
 </entity>
 
 

Author(s):
jpaoletti
 
 public class Entity extends PMCoreObject {
 
     // Represents the entity id. This must me unique.
     private String id;
     //The full name of the class represented by the entity.
     private String clazz;
     //If defined, represents the order of the fields. <br/>{@code <order>field_id2 field_id3 field_id1</order>}
     private String order;
     /*
      * If defined, indicates the id of another entity to inherits the fields,
      * not the operations for now. <br/>{@code
      * <extendz>other_entity_id</extendz>}
      */
     private String extendz;
     //The parent entity if extendz is defined
     private Entity extendzEntity;
     /*
      * Indicates if the entity is auditable so every time an instance is
      * modified, the PM will create an auditory entry of the change
      * <br/><pre>{@code <auditable>true</auditable>}</pre>
      */
     private Boolean auditable;
    
Defines an owner to the entity. It makes this entity "weak".
 <owner>
     <entityId>owner_entity_id</entityId>
     <entityProperty>owner_property</entityProperty>
     <localProperty>local_pointer_to_owner</localProperty>
     <entityCollectionClass></entityCollectionClass>
  </owner>
  
 

See also:
EntityOwner
 
     private EntityOwner owner;
     //List of fields
     private ArrayList<Fieldfields;
     //Internal map to optimize getFieldById() method
     private Map<StringFieldfieldsbyid;
    
Operations of the entity. Standard operations are "add", "edit", "delete", "show", "list" but the programmer can define whatever he wants.
<operations>...</operations>

 
     private Operations operations;
     private Highlights highlights;
     private DataAccess dataAccess;
     //Avoid counting items
     private Boolean noCount;
     private List<Entityweaks;
     private boolean cached;
     //Field id that represent the id of the entity.Optional
     private String idField;
     private PresentationManager pm;
 
     public Entity(PresentationManager pm) {
         super();
         this. = pm;
          = null;
          = null;
          = false;
    }

    
Return the list of fields including inherited ones.

Returns:
The list
    public ArrayList<FieldgetAllFields() {
        final ArrayList<Fieldr = new ArrayList<Field>();
        final List<Stringids = new ArrayList<String>();
        if (getFields() != null) {
            for (Field field : getFields()) {
                r.add(field);
                ids.add(field.getId());
            }
        }
        if (getExtendzEntity() != null) {
            for (Field field : getExtendzEntity().getAllFields()) {
                if (!ids.contains(field.getId())) {
                    r.add(field);
                }
            }
        }
        return r;
    }

    
Returns a list of this entity instances with null from and count and with the given filter

Parameters:
ctx The context
filter The filter
Returns:
The list
Throws:
PMException
    public List<?> getList(PMContext ctxEntityFilter filterthrows PMException {
        return getList(ctxfilternullnullnull);
    }

    
Return a list of this entity instances with null from and count and the filter took from the entity container of the context

Parameters:
ctx The context
Returns:
The list
Throws:
PMException
    public List<?> getList(PMContext ctxthrows PMException {
        final EntityFilter filter = (ctx != null && this.equals(ctx.getEntity())) ? ctx.getEntityContainer().getFilter() : null;
        return getList(ctxfilternullnullnull);
    }

    
Returns a list taken from data access with the given parameters.

Parameters:
ctx The context
filter A filter
from The index of the first element
count The maximun number of items retrieved
Returns:
The list
Throws:
PMException
    public List<?> getList(PMContext ctxEntityFilter filterListSort sortInteger fromInteger countthrows PMException {
        return getDataAccess().list(ctxfilternullsortfromcount);
    }

    
Returns a list taken from data access with the given parameters.

Parameters:
ctx The context
filter A list filter
from The index of the first element
count The maximun number of items retrieved
Returns:
The list
Throws:
PMException
    public List<?> getList(PMContext ctxListFilter filterListSort sortInteger fromInteger countthrows PMException {
        return getDataAccess().list(ctxnullfiltersortfromcount);
    }

    
Getter for a field by its id

Parameters:
id The Field id
Returns:
The Field with the given id
    public Field getFieldById(String id) {
        return getFieldsbyid().get(id);
    }

    
Getter for fieldsbyid. If its null, this methods fill it

Returns:
The mapped field list
    private Map<StringFieldgetFieldsbyid() {
        if ( == null) {
             = new HashMap<StringField>();
            for (Field f : getAllFields()) {
                .put(f.getId(), f);
            }
        }
        return ;
    }

    
This method sorts the fields and returns them

Returns:
fields ordered
    public ArrayList<FieldgetOrderedFields() {
        try {
            if (isOrdered()) {
                ArrayList<Fieldr = new ArrayList<Field>(getAllFields());
                Collections.sort(rnew FieldComparator(getOrder()));
                return r;
            }
        } catch (Exception e) {
            getPm().error(e);
        }
        return getAllFields();
    }

    
Determine if the entity have the order property

Returns:
true if order != null
    public boolean isOrdered() {
        return getOrder() != null;
    }

    
String representation of an entity

Returns:
The string
    @Override
    public String toString() {
        return "Entity (" +  + ") " + ;
    }

    
This method fills the extendsFields variable with the parent Fields. If some field is redefined, parent field is ignored

Parameters:
entity The parent entity given by PM engine
    @Deprecated
    public void fillFields(Entity entity) {
        for (Field field : entity.getAllFields()) {
            if (!containsField(field.getId())) {
                getFields().add(field);
            }
        }
    }

    
Check if there is a Field with an id

Parameters:
id The field id
Returns:
true if the entity contains a Field with the given id
    private boolean containsField(String id) {
        return getFieldById(id) != null;
    }

    
Getter for id

Returns:
the id
    public String getId() {
        return ;
    }

    

Parameters:
id the id to set
    public void setId(String id) {
        this. = id;
    }

    
Getter for clazz

Returns:
the clazz
    public String getClazz() {
        return ;
    }

    

Parameters:
clazz the clazz to set
    public void setClazz(String clazz) {
        this. = clazz;
    }

    
Getter for order

Returns:
the order
    public String getOrder() {
        return ;
    }

    

Parameters:
order the order to set
    public void setOrder(String order) {
        this. = order;
    }

    
The name of the parent entity

Returns:
the extendz
    public String getExtendz() {
        return ;
    }

    

Parameters:
extendz the extendz to set
    public void setExtendz(String extendz) {
        this. = extendz;
    }

    
Indicates if the entity is auditable or not

Returns:
the auditable
    public boolean isAuditable() {
        if ( == null) {
            return false;
        }
        return ;
    }

    

Parameters:
auditable the auditable to set
    public void setAuditable(boolean auditable) {
        this. = auditable;
    }

    
Getter for owner

Returns:
the owner
    public EntityOwner getOwner() {
        return ;
    }

    

Parameters:
owner the owner to set
    public void setOwner(EntityOwner owner) {
        this. = owner;
    }

    
Getter for entity fields

Returns:
the fields
    public ArrayList<FieldgetFields() {
        return ;
    }

    

Parameters:
fields the fields to set
    public void setFields(ArrayList<Fieldfields) {
        this. = fields;
    }

    
Getter for entity operations

Returns:
the operations
    public Operations getOperations() {
        return ;
    }

    

Parameters:
operations the operations to set
    public void setOperations(Operations operations) {
        this. = operations;
    }

    

Parameters:
extendzEntity the extendzEntity to set
    public void setExtendzEntity(Entity extendzEntity) {
        this. = extendzEntity;
    }

    

Returns:
the extendzEntity
    public Entity getExtendzEntity() {
        if ( == null &&  != null) {
             = .getEntity(getExtendz());
            if ( == null) {
                .error("Entity '" + getId() + "' inherites from '" + getExtendz() + "' but this entity was not loaded yet.");
            }
        }
        return ;
    }

    

Parameters:
highlights the highlights to set
    public void setHighlights(Highlights highlights) {
        this. = highlights;
    }

    

Returns:
the highlights
    public Highlights getHighlights() {
        return ;
    }

    

Returns:
the dataAccess
    public DataAccess getDataAccess() {
        if ( == null) {
            try {
                 = (DataAccess.newInstance(.getDefaultDataAccess());
            } catch (Exception e) {
                getPm().error(e);
            }
        }
        if ( != null) {
            .setEntity(this);
        }
        return ;
    }

    

Parameters:
weaks the weaks to set
    public void setWeaks(List<Entityweaks) {
        this. = weaks;
    }

    

Returns:
the weaks
    public List<EntitygetWeaks() {
        return ;
    }

    
Looks for the weak entity corresponding to the given field in this string entity

Parameters:
field
Returns:
the weak entity
    public Entity getWeak(Field field) {
        for (Entity entity : getWeaks()) {
            if (entity.getOwner().getEntityProperty().equals(field.getProperty())) {
                return entity;
            }
        }
        return null;
    }

    

Returns:
the hashcode
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (( == null) ? 0 : .hashCode());
        return result;
    }

    
Compares two entities by id to check if they are equals

Parameters:
obj
Returns:
true if both are the same entity
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof Entity)) {
            return false;
        }
        Entity other = (Entityobj;
        if ( == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!.equals(other.id)) {
            return false;
        }
        return true;
    }

    

Parameters:
noCount the noCount to set
    public void setNoCount(Boolean noCount) {
        this. = noCount;
    }

    

Returns:
the noCount
    public Boolean getNoCount() {
        if ( == null) {
            return false;
        }
        return ;
    }

    
Looks for an apropiate highlight for this field+instance

Parameters:
field
instance
Returns:
the highlight
    public Highlight getHighlight(Field fieldObject instance) {
        if (getHighlights() == null) {
            return null;
        }
        return getHighlights().getHighlight(thisfieldinstance);
    }

    

Returns:
true if the entity is weak
    public boolean isWeak() {
        return getOwner() != null;
    }
    public boolean isCached() {
        return ;
    }
    public void setCached(boolean cached) {
        this. = cached;
    }

    
Returns the internationalized entity title
    public String getTitle() {
        final String key = String.format("pm.entity.%s"getId());
        final String message = .message(key);
        if (key.equals(message)) {
            if (getExtendzEntity() != null) {
                return getExtendzEntity().getTitle();
            }
        }
        return message;
    }
    public String getIdField() {
        if ( != null) {
            return ;
        } else if (getExtendzEntity() != null) {
            return getExtendzEntity().getIdField();
        } else {
            return null;
        }
    }
    public void setIdField(String idField) {
        this. = idField;
    }

    

Returns:
true if the entity has idField
    public boolean isIdentified() {
        return getIdField() != null;
    }

    

Returns:
true if the entity has operations with selected scope
    public boolean hasSelectedScopeOperations() {
        if (getOperations() != null) {
            return getOperations().getOperationsForScope(.).count() > 0;
        } else {
            return false;
        }
    }
New to GrepCode? Check out our FAQ X