Skip to content

Commit

Permalink
Improvement or ApsEntity validation
Browse files Browse the repository at this point in the history
  • Loading branch information
eugeniosant committed Mar 18, 2012
1 parent 731aa53 commit f2bd968
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 141 deletions.
Expand Up @@ -33,6 +33,7 @@
import com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface;
import com.agiletec.aps.system.common.entity.parse.IApsEntityDOM;
import com.agiletec.aps.system.services.category.Category;
import com.agiletec.aps.system.services.group.IGroupManager;

/**
* This class represents an entity.
Expand Down Expand Up @@ -342,17 +343,25 @@ public void setEntityDOM(IApsEntityDOM entityDom) {
this._entityDom = entityDom;
}

public List<FieldError> validate() {
public List<FieldError> validate(IGroupManager groupManager) {
List<FieldError> errors = new ArrayList<FieldError>();
try {
/*
if (null == this.getDescr() || this.getDescr().trim().length() == 0) {
errors.add(new FieldError("description", FieldError.ErrorCode.MANDATORY));
if (null != this.getMainGroup() && null == groupManager.getGroup(this.getMainGroup())) {
FieldError error = new FieldError("mainGroup", FieldError.INVALID);
error.setMessage("Invalid main group - " + this.getMainGroup());
errors.add(error);
}
if (null == this.getMainGroup() || this.getMainGroup().trim().length() == 0) {
errors.add(new FieldError("mainGroup", FieldError.ErrorCode.MANDATORY));
if (null != this.getGroups()) {
Iterator<String> groupsIter = this.getGroups().iterator();
while (groupsIter.hasNext()) {
String groupName = groupsIter.next();
if (null == groupManager.getGroup(groupName)) {
FieldError error = new FieldError("extraGroup", FieldError.INVALID);
error.setMessage("Invalid extra group - " + groupName);
errors.add(error);
}
}
}
*/
if (null != this.getAttributeList()) {
List<AttributeInterface> attributes = this.getAttributeList();
for (int i = 0; i < attributes.size(); i++) {
Expand Down
Expand Up @@ -26,6 +26,23 @@ public String getFieldCode() {
return fieldCode;
}

public String getFullMessage() {
StringBuffer buffer = new StringBuffer(this.getAttributePositionMessage());
buffer.append(" : ");
if (null != this.getMessageKey()) {
buffer.append(this.getMessageKey());
} else if (null != this.getMessage()) {
buffer.append(this.getMessage());
} else {
buffer.append(this.getErrorCode());
}
return buffer.toString();
}

public String getAttributePositionMessage() {
return this.getTracer().getPositionMessage(this.getAttribute());
}

public AttributeTracer getTracer() {
return _tracer;
}
Expand Down
Expand Up @@ -21,8 +21,8 @@
import com.agiletec.aps.system.services.lang.Lang;

/**
* This class implements the 'tracer' for the jAPS Attributes. This class is
* used, with the singles attributes, to trace the position inside 'composite'
* This class implements the 'tracer' for the Entando Attributes. This class is
* used, with the singles attributes, to trace the position inside any 'complex'
* attributes. This class is involved during the update and validation process
* of the Attribute and, furthermore, it guarantees the correct construction of
* the form in the content edit interface.
Expand Down Expand Up @@ -99,6 +99,30 @@ public String getFormFieldName(AttributeInterface attribute) {
return formFieldName.toString();
}

public String getPositionMessage(AttributeInterface attribute) {
StringBuffer buffer = new StringBuffer("Attribute ");
if (this.isMonoListElement()) {
if (this.isCompositeElement()) {
buffer.append(this.getParentAttribute().getName())
.append(" - element ").append(String.valueOf(this.getListIndex() + 1))
.append(" - Included Attribute ").append(attribute.getName());
} else {
buffer.append(attribute.getName()).append(" - element ")
.append(String.valueOf(this.getListIndex() + 1));
}
} else if (this.isCompositeElement()) {
buffer.append(this.getParentAttribute().getName())
.append(" - Included Attribute ").append(attribute.getName());
} else if (this.isListElement()) {
buffer.append(attribute.getName())
.append(" - lang ").append(this.getListLang().getDescr())
.append(" - element ").append(String.valueOf(this.getListIndex() + 1));
} else {
buffer.append(attribute.getName());
}
return buffer.toString();
}

public Lang getLang() {
return _lang;
}
Expand Down
Expand Up @@ -24,6 +24,7 @@
import com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface;
import com.agiletec.aps.system.common.entity.parse.IApsEntityDOM;
import com.agiletec.aps.system.services.category.Category;
import com.agiletec.aps.system.services.group.IGroupManager;

/**
* This class represents an Entity.
Expand Down Expand Up @@ -195,6 +196,6 @@ public interface IApsEntity {
*/
public void disableAttributes(String disablingCode);

public List<FieldError> validate();
public List<FieldError> validate(IGroupManager groupManager);

}
Expand Up @@ -17,6 +17,7 @@
*/
package com.agiletec.aps.system.common.entity.model;

import com.agiletec.aps.system.ApsSystemUtils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -49,56 +50,68 @@ public class JAXBEntity implements Serializable {
public JAXBEntity() {}

public JAXBEntity(IApsEntity mainEntity, String langCode) {
this.setDescr(mainEntity.getDescr());
this.setId(mainEntity.getId());
this.setMainGroup(mainEntity.getMainGroup());
this.setTypeCode(mainEntity.getTypeCode());
this.setTypeDescr(mainEntity.getTypeDescr());
this.setGroups(mainEntity.getGroups());
this.setCategories(mainEntity.getCategories());
List<AttributeInterface> attributes = mainEntity.getAttributeList();
if (null == attributes || attributes.size() == 0) {
return;
}
for (int i = 0; i < attributes.size(); i++) {
AttributeInterface attribute = attributes.get(i);
DefaultJAXBAttribute jaxrAttribute = attribute.getJAXBAttribute(langCode);
if (null != jaxrAttribute) {
this.getAttributes().add(jaxrAttribute);
try {
this.setDescr(mainEntity.getDescr());
this.setId(mainEntity.getId());
this.setMainGroup(mainEntity.getMainGroup());
this.setTypeCode(mainEntity.getTypeCode());
this.setTypeDescr(mainEntity.getTypeDescr());
this.setGroups(mainEntity.getGroups());
this.setCategories(mainEntity.getCategories());
List<AttributeInterface> attributes = mainEntity.getAttributeList();
if (null == attributes || attributes.size() == 0) {
return;
}
for (int i = 0; i < attributes.size(); i++) {
AttributeInterface attribute = attributes.get(i);
DefaultJAXBAttribute jaxrAttribute = attribute.getJAXBAttribute(langCode);
if (null != jaxrAttribute) {
this.getAttributes().add(jaxrAttribute);
}
}
} catch (Throwable t) {
ApsSystemUtils.logThrowable(t, this, "JAXBEntity");
throw new RuntimeException("Error creating JAXBEntity", t);
}
}

public IApsEntity buildEntity(IApsEntity prototype, ICategoryManager categoryManager) {
prototype.setDescr(this.getDescr());
prototype.setId(this.getId());
prototype.setMainGroup(this.getMainGroup());
prototype.setTypeCode(this.getTypeCode());
prototype.setTypeDescr(this.getTypeDescr());
if (null != this.getGroups() && !this.getGroups().isEmpty()) {
Iterator<String> iter = this.getGroups().iterator();
while (iter.hasNext()) {
prototype.addGroup(iter.next());
try {
prototype.setDescr(this.getDescr());
prototype.setId(this.getId());
prototype.setMainGroup(this.getMainGroup());
prototype.setTypeCode(this.getTypeCode());
prototype.setTypeDescr(this.getTypeDescr());
if (null != this.getGroups() && !this.getGroups().isEmpty()) {
Iterator<String> iter = this.getGroups().iterator();
while (iter.hasNext()) {
prototype.addGroup(iter.next());
}
}
}
if (null != this.getCategories() && !this.getCategories().isEmpty()) {
Iterator<String> iter = this.getCategories().iterator();
while (iter.hasNext()) {
String categoryCode = iter.next();
Category category = categoryManager.getCategory(categoryCode);
if (null != category) prototype.addCategory(category);
if (null != this.getCategories() && !this.getCategories().isEmpty()) {
Iterator<String> iter = this.getCategories().iterator();
while (iter.hasNext()) {
String categoryCode = iter.next();
Category category = categoryManager.getCategory(categoryCode);
if (null != category) {
prototype.addCategory(category);
}
}
}
}
if (null == this.getAttributes()) {
return prototype;
}
for (int i = 0; i < this.getAttributes().size(); i++) {
DefaultJAXBAttribute jaxrAttribute = this.getAttributes().get(i);
AttributeInterface attribute = (AttributeInterface) prototype.getAttribute(jaxrAttribute.getName());
if (null != attribute && null != jaxrAttribute
&& attribute.getType().equals(jaxrAttribute.getType())) {
attribute.valueFrom(jaxrAttribute);
if (null == this.getAttributes()) {
return prototype;
}
for (int i = 0; i < this.getAttributes().size(); i++) {
DefaultJAXBAttribute jaxrAttribute = this.getAttributes().get(i);
AttributeInterface attribute = (AttributeInterface) prototype.getAttribute(jaxrAttribute.getName());
if (null != attribute && null != jaxrAttribute
&& attribute.getType().equals(jaxrAttribute.getType())) {
attribute.valueFrom(jaxrAttribute);
}
}
} catch (Throwable t) {
ApsSystemUtils.logThrowable(t, this, "buildEntity");
throw new RuntimeException("Error creating Entity", t);
}
return prototype;
}
Expand Down

0 comments on commit f2bd968

Please sign in to comment.