Permalink
Browse files

Implementing property validation via super class methods

  • Loading branch information...
dgkncelik committed May 14, 2018
1 parent beb9ffe commit 72a04b4e24e6a3e19b16217649475295764c3bbc
Showing with 273 additions and 111 deletions.
  1. +157 −0 src/main/java/edu/itu/cavabunga/core/entity/Property.java
  2. +0 −1 src/main/java/edu/itu/cavabunga/core/entity/parameter/Fmttype.java
  3. +0 −1 src/main/java/edu/itu/cavabunga/core/entity/parameter/Language.java
  4. +0 −1 src/main/java/edu/itu/cavabunga/core/entity/parameter/Member.java
  5. +0 −2 src/main/java/edu/itu/cavabunga/core/entity/parameter/Partstat.java
  6. +2 −3 src/main/java/edu/itu/cavabunga/core/entity/parameter/Range.java
  7. +2 −4 src/main/java/edu/itu/cavabunga/core/entity/parameter/Related.java
  8. +2 −4 src/main/java/edu/itu/cavabunga/core/entity/parameter/Reltype.java
  9. +2 −4 src/main/java/edu/itu/cavabunga/core/entity/parameter/Role.java
  10. +2 −4 src/main/java/edu/itu/cavabunga/core/entity/parameter/Rsvp.java
  11. +2 −4 src/main/java/edu/itu/cavabunga/core/entity/parameter/SentBy.java
  12. +15 −1 src/main/java/edu/itu/cavabunga/core/entity/parameter/Tzid.java
  13. +2 −5 src/main/java/edu/itu/cavabunga/core/entity/parameter/Value.java
  14. +5 −13 src/main/java/edu/itu/cavabunga/core/entity/property/Attach.java
  15. +3 −17 src/main/java/edu/itu/cavabunga/core/entity/property/Calscale.java
  16. +2 −13 src/main/java/edu/itu/cavabunga/core/entity/property/Class.java
  17. +2 −14 src/main/java/edu/itu/cavabunga/core/entity/property/Comment.java
  18. +2 −20 src/main/java/edu/itu/cavabunga/core/entity/property/Description.java
  19. +5 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Geo.java
  20. +5 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Location.java
  21. +5 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Method.java
  22. +10 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Percent.java
  23. +5 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Priority.java
  24. +5 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Prodid.java
  25. +29 −0 src/main/java/edu/itu/cavabunga/core/entity/property/PropertyValueType.java
  26. +4 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Resources.java
  27. +5 −0 src/main/java/edu/itu/cavabunga/core/entity/property/Version.java
@@ -6,6 +6,7 @@
import edu.itu.cavabunga.exception.Validation;
import lombok.Data;
import org.hibernate.annotations.DiscriminatorOptions;
import org.omg.PortableServer.THREAD_POLICY_ID;
import javax.persistence.*;
import javax.persistence.Version;
@@ -101,5 +102,161 @@ 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){
if(parameterList != null && !parameterList.isEmpty()) {
Integer instanceCount = parameterList.size();
Integer foundInstance = 0;
for (Parameter pr : parameterList) {
for (Parameter search : parameters) {
if (pr.getClass().getName().equals(search.getClass().getName())) {
foundInstance++;
}
}
}
if (foundInstance != instanceCount) {
throw new Validation(this.getClass().getName() + "property musthaveList is not valid");
}
}
}
public void validateCannotHaveParameters(List<Parameter> parameterList){
if(parameterList != null && !parameterList.isEmpty()) {
for (Parameter pr : parameterList) {
for (Parameter search : parameters) {
if (pr.getClass().getName().equals(search.getClass().getName())) {
throw new Validation(this.getClass().getName() + " property cannot have child parameter of: " + search.getClass().getName());
}
}
}
}
}
public void validateValueType(PropertyValueType propertyValueType){
if(propertyValueType != null) {
if (propertyValueType == PropertyValueType.BINARY) {
if (!value.matches("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$")) {
throw new Validation(this.getClass().getName() + " value is not valid BINARY type");
}
}
if (propertyValueType == PropertyValueType.BOOLEAN) {
if (value != "TRUE" && value != "FALSE") {
throw new Validation(this.getClass().getName() + " value is not valid BOOLEAN type " + value);
}
}
if (propertyValueType == PropertyValueType.CALADDRESS) {
//TODO: there is a full rfc about it: rfc3986
}
if (propertyValueType == PropertyValueType.DATE) {
String[] parts = value.split(",");
for (String part : parts) {
if (!part.matches("^[0-9]{8}$")) {
throw new Validation(this.getClass().getName() + " value is not valid DATE type " + value);
}
}
}
if (propertyValueType == PropertyValueType.DATETIME) {
String[] parts = value.split(",");
for (String part : parts) {
if (!part.matches("^[0-9]{8}T[0-9]{6}|[0-9]{8}T[0-9]{6}Z")) {
throw new Validation(this.getClass().getName() + " value is not valid DATE-TIME type " + value);
}
}
}
if (propertyValueType == PropertyValueType.DURATION) {
//TODO: rfc5545 pg 35
}
if (propertyValueType == PropertyValueType.FLOAT) {
String[] parts = value.split(",");
for (String part : parts) {
try {
Float f = Float.parseFloat(value);
} catch (NumberFormatException e) {
throw new Validation(this.getClass().getName() + " value is not valid FLOAT type " + value);
}
}
}
if (propertyValueType == PropertyValueType.INTEGER) {
String[] parts = value.split(",");
for (String part : parts) {
try {
Integer i = Integer.parseInt(value);
} catch (NumberFormatException e) {
throw new Validation(this.getClass().getName() + " value is not valid INTEGER type" + value);
}
}
}
if (propertyValueType == PropertyValueType.PERIOD) {
//TODO: rfc5545 pg 36
}
if (propertyValueType == PropertyValueType.RECUR) {
//TODO: rfc5545 pg 37-45
}
if (propertyValueType == PropertyValueType.TEXT) {
//TODO: rfc5545 pg 45
}
if (propertyValueType == PropertyValueType.TIME) {
String[] parts = value.split(",");
for (String part : parts) {
if (!part.matches("^[0-9]{6}$|^[0-9]{6}Z$")) {
throw new Validation(this.getClass().getName() + " value is not valid TIME type " + value);
}
}
}
if (propertyValueType == PropertyValueType.URI) {
//TODO: there is a full rfc about it: rfc3986
}
if (propertyValueType == PropertyValueType.UTCOFFSET) {
String[] parts = value.split(",");
for (String part : parts) {
if (!part.matches("^+[0-9]{4}$|^+[0-9]{4}Z$")) {
throw new Validation(this.getClass().getName() + " value is not valid TIME type " + value);
}
}
}
}
}
}
@@ -11,7 +11,6 @@
public void validate(){
//TODO: This parameter can be specified on properties that are used to reference an object
super.validate();
if(this.getValue().matches("(\\w+)/(\\w+)") != true){
throw new Validation("FMTYPE must have valid mime type: " + this.getValue());
}
@@ -11,7 +11,6 @@
@Override
public void validate(){
super.validate();
try {
LocaleUtils.toLocale(this.getValue());
}catch (IllegalArgumentException e){
@@ -11,7 +11,6 @@
public void validate(){
//TODO: This parameter can be specified on properties with a CAL-ADDRESS value type
super.validate();
if(this.getValue().substring(0,1) != "\"" || this.getValue().substring(this.getValue().length() -1) != "\""){
throw new Validation("MEMBER parameter must start and end with DQUOTE char.");
}
@@ -44,7 +44,5 @@ public void validate(){
throw new Validation("PARTSTAT value is different from acceptable value range in a Journal component " + this.getValue());
}
}
// TODO: value of partstat may depend on component type which this parameter's property have used in
// check RFC 5545
}
}
@@ -10,9 +10,8 @@
public class Range extends Parameter {
@Override
public void validate(){
if(this.getValue().trim() == ""){
throw new Validation("RANGE parameter cannot be empty");
}
//TODO: This parameter can be specified on a property that specifies a recurrence identifier.
super.validate();
if(this.getValue() != "THISANDFUTURE"){
throw new Validation("RANGE parameter can only have THISANDFUTURE value: " + this.getValue());
@@ -9,10 +9,8 @@
public class Related extends Parameter {
@Override
public void validate(){
if(this.getValue().trim() != ""){
throw new Validation("RELATED parameter cannot be empty");
}
//TODO: This parameter can be specified on properties that specify an alarm trigger with a "DURATION" value type.
super.validate();
if(this.getValue() != "START" && this.getValue() != "END"){
throw new Validation("RELATED value is different from acceptable value range: " + this.getValue());
}
@@ -9,10 +9,8 @@
public class Reltype extends Parameter{
@Override
public void validate(){
if(this.getValue().trim() != ""){
throw new Validation("RELTYPE parameter cannot be empty");
}
//TODO: This parameter can be specified on properties with a CAL-ADDRESS value type
super.validate();
if(this.getValue() != "PARENT" &&
this.getValue() != "CHILD" &&
this.getValue() != "SIBLING"){
@@ -9,10 +9,8 @@
public class Role extends Parameter {
@Override
public void validate(){
if(this.getValue().trim() != ""){
throw new Validation("ROLE parameter cannot be empty");
}
//TODO: This parameter can be specified on properties with a CAL-ADDRESS value type.
super.validate();
if(this.getValue() != "CHAIR" &&
this.getValue() != "REQ-PARTICIPANT" &&
this.getValue() != "OPT-PARTICIPANT" &&
@@ -9,10 +9,8 @@
public class Rsvp extends Parameter {
@Override
public void validate(){
if(this.getValue().trim() != ""){
throw new Validation("RSVP parameter cannot be empty");
}
//TODO: This parameter can be specified on properties with a CAL-ADDRESS value type.
super.validate();
if(this.getValue() != "TRUE" &&
this.getValue() != "FALSE"){
throw new Validation("RSVP value is different from acceptable value range: " + this.getValue());
@@ -9,10 +9,8 @@
public class SentBy extends Parameter {
@Override
public void validate(){
if(this.getValue().trim() == ""){
throw new Validation("SENT-BY parameter cannot be empty");
}
//TODO:This parameter can be specified on properties with a CAL-ADDRESS value type
super.validate();
if(this.getValue().substring(0,1) != "\"" || this.getValue().substring(this.getValue().length() -1) != "\""){
throw new Validation("SENT-BY parameter must start and end with DQUOTE char.");
}
@@ -1,13 +1,27 @@
package edu.itu.cavabunga.core.entity.parameter;
import edu.itu.cavabunga.core.entity.Parameter;
import edu.itu.cavabunga.core.entity.component.Calendar;
import edu.itu.cavabunga.core.entity.property.*;
import edu.itu.cavabunga.exception.Validation;
import javax.persistence.Entity;
@Entity
public class Tzid extends Parameter {
@Override
public void validate(){
//TODO: RFC 5545 must implemented
super.validate();
if(!(this.getProperty() instanceof Dtstart) &&
!(this.getProperty() instanceof Dtend) &&
!(this.getProperty() instanceof Due) &&
!(this.getProperty() instanceof Exdate) &&
!(this.getProperty() instanceof Rdate)){
throw new Validation("TZID parameter can only be use in Dtstart, Dtend, Due, Exdate and Rdate properties: " + this.getProperty().getClass().getName());
}
//TODO: Validate TZID string matches with the Calendar's VTIMEZONE component's TZID identifier
}
}
@@ -9,14 +9,12 @@
public class Value extends Parameter{
@Override
public void validate(){
if(this.getValue().trim() == ""){
throw new Validation("VALUE value cannot be empty");
}
super.validate();
if(this.getValue() != "BINARY" &&
this.getValue() != "BOOLEAN" &&
this.getValue() != "CAL-ADDRESS" &&
this.getValue() != "DATE-TIME" &&
this.getValue() != "DATE" &&
this.getValue() != "DURATION" &&
this.getValue() != "FLOAT" &&
this.getValue() != "INTEGER" &&
@@ -28,6 +26,5 @@ public void validate(){
this.getValue() != "UTC-OFFSET"){
throw new Validation("VALUE value is different from acceptable value range: " + this.getValue());
}
}
}
@@ -11,19 +11,11 @@
public class Attach extends Property {
@Override
public void validate(){
if(this.getValue().trim() != ""){
throw new Validation("ATTACH property cannot be empty");
}
if(!this.getParameters().isEmpty()){
for(Parameter pr : this.getParameters()){
try {
pr.validate();
}catch (Exception e){
throw new Validation("ATTACH parameter validation failed: " + pr.getValue());
}
}
super.validate();
try {
super.validateValueType(PropertyValueType.URI);
}catch (Exception e){
super.validateValueType(PropertyValueType.BINARY);
}
}
}
@@ -7,32 +7,18 @@
import edu.itu.cavabunga.exception.Validation;
import javax.persistence.Entity;
import java.util.List;
@Entity
public class Calscale extends Property {
@Override
public void validate(){
if(this.getValue().trim() != ""){
throw new Validation("CALSCALE property cannot be empty");
}
super.validate();
super.validateValueType(PropertyValueType.TEXT);
//TODO: is cavabunga support only for Gregorian calendar?
if(this.getValue() != "GREGORIAN" ){
throw new Validation("CALSCALE value is different from acceptable value range: " + this.getValue());
}
if(!(this.getComponent() instanceof Calendar)){
throw new Validation("CALSCALE property can only be use in Calendar component");
}
if(!this.getParameters().isEmpty()){
for(Parameter pr : this.getParameters()){
try {
pr.validate();
}catch (Exception e){
throw new Validation("CALENDAR parameter validation failed: " + pr.getValue());
}
}
}
}
}
Oops, something went wrong.

0 comments on commit 72a04b4

Please sign in to comment.