Permalink
Browse files

- Object validation %90 percent completed. Some of them too much deta…

…iled and unnecessary.

- Validation have to be made before objects send to repository. So, in IcalService save methods catches exceptions before send to repositories.
  • Loading branch information...
dgkncelik committed May 20, 2018
1 parent d25dfa6 commit 490406bece8582965469a2842bd38dd468f0b25e
Showing with 469 additions and 343 deletions.
  1. +77 −0 src/main/java/edu/itu/cavabunga/core/entity/Component.java
  2. +1 −28 src/main/java/edu/itu/cavabunga/core/entity/Property.java
  3. +16 −11 src/main/java/edu/itu/cavabunga/core/entity/component/Alarm.java
  4. +16 −35 src/main/java/edu/itu/cavabunga/core/entity/component/Calendar.java
  5. +12 −11 src/main/java/edu/itu/cavabunga/core/entity/component/Daylight.java
  6. +31 −36 src/main/java/edu/itu/cavabunga/core/entity/component/Event.java
  7. +17 −11 src/main/java/edu/itu/cavabunga/core/entity/component/Freebusy.java
  8. +23 −27 src/main/java/edu/itu/cavabunga/core/entity/component/Journal.java
  9. +11 −11 src/main/java/edu/itu/cavabunga/core/entity/component/Standard.java
  10. +14 −19 src/main/java/edu/itu/cavabunga/core/entity/component/Timezone.java
  11. +32 −37 src/main/java/edu/itu/cavabunga/core/entity/component/Todo.java
  12. +2 −17 src/main/java/edu/itu/cavabunga/core/entity/property/Action.java
  13. +2 −18 src/main/java/edu/itu/cavabunga/core/entity/property/Attendee.java
  14. +2 −13 src/main/java/edu/itu/cavabunga/core/entity/property/Catagories.java
  15. +2 −17 src/main/java/edu/itu/cavabunga/core/entity/property/Completed.java
  16. +2 −13 src/main/java/edu/itu/cavabunga/core/entity/property/Contact.java
  17. +2 −13 src/main/java/edu/itu/cavabunga/core/entity/property/Created.java
  18. +5 −12 src/main/java/edu/itu/cavabunga/core/entity/property/Dtend.java
  19. +2 −13 src/main/java/edu/itu/cavabunga/core/entity/property/Dtstamp.java
  20. +10 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Dtstart.java
  21. +10 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Due.java
  22. +5 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Duration.java
  23. +10 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Exdate.java
  24. +5 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Freebusy.java
  25. +6 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Lastmod.java
  26. +5 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Organizer.java
  27. +10 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Rdate.java
  28. +10 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Recurid.java
  29. +2 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Related.java
  30. +6 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Repeat.java
  31. +2 −1 src/main/java/edu/itu/cavabunga/core/entity/property/Resources.java
  32. +6 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Rrule.java
  33. +6 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Seq.java
  34. +18 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Status.java
  35. +5 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Summary.java
  36. +11 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Transp.java
  37. +10 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Trigger.java
  38. +5 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Tzid.java
  39. +5 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Tzname.java
  40. +5 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Tzoffsetfrom.java
  41. +5 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Tzoffsetto.java
  42. +5 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Tzurl.java
  43. +5 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Uid.java
  44. +5 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Url.java
  45. +6 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Version.java
  46. +22 −0 src/main/java/edu/itu/cavabunga/core/service/IcalServiceImpl.java
@@ -2,7 +2,10 @@
import com.fasterxml.jackson.annotation.*;
import edu.itu.cavabunga.core.entity.component.*;
import edu.itu.cavabunga.core.entity.property.PropertyType;
import edu.itu.cavabunga.exception.Validation;
import lombok.Data;
import org.apache.commons.lang.Validate;
import org.hibernate.annotations.DiscriminatorOptions;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@@ -70,6 +73,80 @@ public void addProperty(Property property){
}
public void validate(){
if(!components.isEmpty()){
for (Component c : components){
try {
c.validate();
}catch (Exception e){
throw new Validation(this.getClass().getName() + " component's sub component validation failed: " + e.getMessage());
}
}
}
if(!properties.isEmpty()){
for(Property p : properties){
try {
p.validate();
}catch (Exception e){
throw new Validation(this.getClass().getName() + " component property validateion failed " + e.getMessage());
}
}
}
}
public void validateOptionalOneProperties(List<PropertyType> propertyTypeList){
Integer propertyCount = 0;
for(PropertyType pt : propertyTypeList){
for(Property p : properties){
if(p.getClass().getName().equals(pt.create().getClass().getName())){
propertyCount++;
}
if(propertyCount >= 2){
throw new Validation("Component validation failed in optional-one properties check: " + p.getClass().getName());
}
}
propertyCount = 0;
}
}
public void validateOptionalManyProperties(){
//
}
public void validateRequiredOneProperties(List<PropertyType> propertyTypeList){
Integer propertyCount = 0;
for(PropertyType pt : propertyTypeList){
for(Property p : properties){
if(p.getClass().getName().equals(pt.create().getClass().getName())){
propertyCount++;
}
if(propertyCount >= 2 ){
throw new Validation("Component validation failed in required-one properties check. Count >= 2: " + p.getClass().getName());
}
}
if(propertyCount == 0){
throw new Validation("Component validation failed in required-one properties check. Count 0: " + pt.create().getClass().getName());
}
}
}
public void validateReqiredManyProperties(List<PropertyType> propertyTypeList){
Integer propertyCount = 0;
for(PropertyType pt : propertyTypeList){
for(Property p : properties){
if(p.getClass().getName().equals(pt.create().getClass().getName())){
propertyCount++;
}
}
if(propertyCount == 0){
throw new Validation("Component validation failed in required-many properties check: " + pt.create().getClass().getName());
}
}
}
}
@@ -102,34 +102,6 @@ public void validate(){
}
}
}
/*
if(mustHaveParameterList != null && !mustHaveParameterList.isEmpty()){
try{
validateMustHaveParameters(mustHaveParameterList);
}catch (Exception e){
throw new Validation(this.getClass().getName() + " property class validation failed: " + e.getMessage());
}
}
if(cannotHaveParameterList != null && !cannotHaveParameterList.isEmpty()){
try {
validateCannotHaveParameters(cannotHaveParameterList);
}catch (Exception e){
throw new Validation(this.getClass().getName() + " property class validation failed: " + e.getMessage());
}
}
if(propertyValueType != null){
try{
validateValueType(propertyValueType);
}catch (Exception e){
throw new Validation(this.getClass().getName() + " property class validation failed: " + e.getMessage());
}
}
*/
}
public void validateMustHaveParameters(List<Parameter> parameterList){
@@ -208,6 +180,7 @@ public void validateValueType(PropertyValueType propertyValueType){
try {
Float f = Float.parseFloat(value);
} catch (NumberFormatException e) {
//TODO: GEO property has a value type in 'float;float' format !!
throw new Validation(this.getClass().getName() + " value is not valid FLOAT type " + value);
}
}
@@ -2,26 +2,31 @@
import edu.itu.cavabunga.core.entity.Component;
import edu.itu.cavabunga.core.entity.Property;
import edu.itu.cavabunga.core.entity.property.PropertyType;
import edu.itu.cavabunga.exception.Validation;
import javax.persistence.Entity;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Alarm extends Component{
@Override
public void validate(){
if(!this.getProperties().isEmpty()){
for(Property p : this.getProperties()){
try{
p.validate();
}catch (Exception e){
throw new Validation("Alarm property validation failed: " + p.getValue());
}
}
if(!(this.getParent() instanceof Event) && !(this.getParent() instanceof Todo)){
throw new Validation("Alarm component can only be child of Event and Todo, not: " + this.getParent().getClass().getName());
}
if(!this.getComponents().isEmpty()){
throw new Validation("Alarm component cannot have sub-component");
}
super.validate();
List<PropertyType> requireOneList = new ArrayList<PropertyType>();
requireOneList.add(PropertyType.Action);
requireOneList.add(PropertyType.Trigger);
super.validateRequiredOneProperties(requireOneList);
List<PropertyType> optionalOneList = new ArrayList<PropertyType>();
optionalOneList.add(PropertyType.Due);
optionalOneList.add(PropertyType.Repeat);
super.validateOptionalOneProperties(optionalOneList);
}
}
@@ -4,52 +4,33 @@
import edu.itu.cavabunga.core.entity.Parameter;
import edu.itu.cavabunga.core.entity.Property;
import edu.itu.cavabunga.core.entity.property.Prodid;
import edu.itu.cavabunga.core.entity.property.PropertyType;
import edu.itu.cavabunga.core.entity.property.Version;
import edu.itu.cavabunga.exception.Validation;
import org.apache.commons.lang.Validate;
import javax.persistence.Entity;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Calendar extends Component {
@Override
public void validate(){
//TODO: this is a linear search could cause performence problems, take a look 'JAVA 8 Streams'
if(!this.getProperties().isEmpty()) {
boolean isProdidExists = false;
boolean isVersionExists = false;
for (Property p : this.getProperties()) {
if (p instanceof Prodid) {
isProdidExists = true;
}
if (p instanceof Version) {
isVersionExists = true;
}
try {
p.validate();
} catch (Exception e){
throw new Validation("Property validation failed: " + p.getValue());
}
}
if(this.getParent() != null){
throw new Validation("Calendar component cannot have parent component");
}
super.validate();
if (!(isProdidExists && isVersionExists)) {
throw new Validation("CALENDAR object must have PRODID and VERSION properties");
}
}
List<PropertyType> optionalOneList = new ArrayList<PropertyType>();
optionalOneList.add(PropertyType.Calscale);
optionalOneList.add(PropertyType.Method);
super.validateOptionalOneProperties(optionalOneList);
if(!this.getComponents().isEmpty()) {
try {
for (Component c : this.getComponents()){
c.validate();
}
} catch(Exception e) {
throw new Validation("Calendar object's sub components caused validation exception");
}
}
//TODO: according to rfc 5545 calendar component should at least one sub-component, but how to create participants first empty calendar??
List<PropertyType> requiredOneList = new ArrayList<PropertyType>();
requiredOneList.add(PropertyType.Prodid);
requiredOneList.add(PropertyType.Version);
super.validateRequiredOneProperties(requiredOneList);
}
}
@@ -2,26 +2,27 @@
import edu.itu.cavabunga.core.entity.Component;
import edu.itu.cavabunga.core.entity.Property;
import edu.itu.cavabunga.core.entity.property.PropertyType;
import edu.itu.cavabunga.exception.Validation;
import javax.persistence.Entity;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Daylight extends Component{
@Override
public void validate(){
if(!this.getProperties().isEmpty()){
for(Property p : this.getProperties()){
try{
p.validate();
}catch (Exception e){
throw new Validation("Daylight property validation failed: " + p.getValue());
}
}
if(!(this.getParent() instanceof Timezone)){
throw new Validation("Daylight component cannot be child of :" + this.getParent().getClass().getName());
}
if(!this.getComponents().isEmpty()){
throw new Validation("Daylight component cannot have sub-component");
}
super.validate();
List<PropertyType> requireOneList = new ArrayList<PropertyType>();
requireOneList.add(PropertyType.Dtstart);
requireOneList.add(PropertyType.Tzname);
requireOneList.add(PropertyType.Tzoffsetfrom);
super.validateRequiredOneProperties(requireOneList);
}
}
@@ -3,52 +3,47 @@
import edu.itu.cavabunga.core.entity.Component;
import edu.itu.cavabunga.core.entity.Property;
import edu.itu.cavabunga.core.entity.property.Dtstamp;
import edu.itu.cavabunga.core.entity.property.PropertyType;
import edu.itu.cavabunga.core.entity.property.Uid;
import edu.itu.cavabunga.exception.Validation;
import javax.persistence.Entity;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Event extends Component {
@Override
public void validate(){
//TODO: linear search
boolean isDtstampExists = false;
boolean isUidExists = false;
if(!this.getProperties().isEmpty()){
for(Property p : this.getProperties()){
if(p instanceof Dtstamp){
isDtstampExists = true;
}
if(p instanceof Uid){
isUidExists = true;
}
try{
p.validate();
}catch (Exception e){
throw new Validation("Property validation failed: " + p.getValue());
}
}
if(!(isDtstampExists && isUidExists)){
throw new Validation("Event component must have DTSTAMP and UID properties");
}
if(!(this.getParent() instanceof Calendar)){
throw new Validation("Event component cannot be child of: " + this.getParent().getClass().getName());
}
if(!this.getComponents().isEmpty()){
for(Component c: this.getComponents()){
if(!(c instanceof Alarm)){
throw new Validation("Event component must have component other then Alarm");
}
try{
c.validate();
}catch (Exception e){
throw new Validation("Event sub-component validation failed");
}
}
}
super.validate();
List<PropertyType> optionalOneList = new ArrayList<PropertyType>();
optionalOneList.add(PropertyType.Class);
optionalOneList.add(PropertyType.Created);
optionalOneList.add(PropertyType.Description);
optionalOneList.add(PropertyType.Dtstamp);
optionalOneList.add(PropertyType.Dtstart);
optionalOneList.add(PropertyType.Geo);
optionalOneList.add(PropertyType.Lastmod);
optionalOneList.add(PropertyType.Location);
optionalOneList.add(PropertyType.Organizer);
optionalOneList.add(PropertyType.Priority);
optionalOneList.add(PropertyType.Recurid);
optionalOneList.add(PropertyType.Seq);
optionalOneList.add(PropertyType.Status);
optionalOneList.add(PropertyType.Summary);
optionalOneList.add(PropertyType.Transp);
optionalOneList.add(PropertyType.Uid);
optionalOneList.add(PropertyType.Url);
super.validateOptionalOneProperties(optionalOneList);
List<PropertyType> requiredOneList = new ArrayList<PropertyType>();
requiredOneList.add(PropertyType.Dtend);
requiredOneList.add(PropertyType.Duration);
super.validateRequiredOneProperties(requiredOneList);
}
}
Oops, something went wrong.

0 comments on commit 490406b

Please sign in to comment.