Skip to content

Commit

Permalink
Merge pull request #32 from ITUBIDB/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
dgkncelik committed May 29, 2018
2 parents 57f93ce + eef6691 commit 57b0715
Show file tree
Hide file tree
Showing 91 changed files with 1,295 additions and 52 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Expand Up @@ -78,6 +78,11 @@
<version>2.8.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -20,6 +20,7 @@ public class ExceptionHandlerAdvice {
@ExceptionHandler(NotFound.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ErrorResponse handleNotFound(NotFound e){
System.out.printf(e.getMessage());
return new ErrorResponse(1,e.getMessage(), null);
}

Expand All @@ -32,6 +33,7 @@ public ErrorResponse handleNotFound(NotFound e){
@ExceptionHandler(Conflict.class)
@ResponseStatus(HttpStatus.CONFLICT)
public ErrorResponse handleConflict(Conflict e){
System.out.printf(e.getMessage());
return new ErrorResponse(1,e.getMessage(),null);
}

Expand All @@ -44,6 +46,7 @@ public ErrorResponse handleConflict(Conflict e){
@ExceptionHandler(IllegalStateException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleIllegalState(IllegalStateException e){
System.out.printf(e.getMessage());
return new ErrorResponse(1,e.getMessage(),null);
}

Expand All @@ -56,6 +59,7 @@ public ErrorResponse handleIllegalState(IllegalStateException e){
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleIllegalArgument(IllegalArgumentException e){
System.out.printf(e.getMessage());
return new ErrorResponse(1,e.getMessage(),null);
}

Expand All @@ -65,11 +69,12 @@ public ErrorResponse handleIllegalArgument(IllegalArgumentException e){
* @param e caught exception
* @return ErrorResponse with exception message with related HTTP status
*/
@ExceptionHandler(Exception.class)
/* @ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponse handleException(Exception e){
System.out.printf(e.getMessage());
return new ErrorResponse(1,e.getMessage(), null);
}
}*/


}
Expand Up @@ -110,7 +110,7 @@ public void addComponent(Component component, String owner, Long parentComponent

Assert.state(
component.getId() == null,
"New component cannot have id field, please use update methods"
"44New component cannot have id field, please use update methods"
);

if(!participantService.getParticipantByUserName(owner).isPresent()) {
Expand Down Expand Up @@ -192,7 +192,7 @@ public void addProperty(Property property, Long parentComponentId){
Assert.notNull(parentComponentId, "ParentComponentId must not be null!");
Assert.state(
property.getId() == null,
"New Property cannot have id field, please use update methods"
"55New Property cannot have id field, please use update methods"
);

if(!icalService.getComponentById(parentComponentId).isPresent()) {
Expand Down Expand Up @@ -269,7 +269,7 @@ public void addParameter(Parameter parameter, Long parentPropertyId){
Assert.notNull(parentPropertyId, "ParentPropertyId must not be null!");
Assert.state(
parameter.getId() == null,
"New parameter cannot have id field, please use update methods"
"77New parameter cannot have id field, please use update methods"
);

if(!icalService.getPropertyById(parentPropertyId).isPresent()) {
Expand Down
130 changes: 130 additions & 0 deletions src/main/java/edu/itu/cavabunga/controller/SeederController.java
@@ -0,0 +1,130 @@
package edu.itu.cavabunga.controller;

import edu.itu.cavabunga.business.CalendarManagerService;
import edu.itu.cavabunga.core.entity.Component;
import edu.itu.cavabunga.core.entity.Parameter;
import edu.itu.cavabunga.core.entity.Participant;
import edu.itu.cavabunga.core.entity.Property;
import edu.itu.cavabunga.core.entity.component.ComponentType;
import edu.itu.cavabunga.core.entity.parameter.ParameterType;
import edu.itu.cavabunga.core.entity.participant.ParticipantType;
import edu.itu.cavabunga.core.entity.property.PropertyType;
import edu.itu.cavabunga.core.entity.property.PropertyValueType;
import edu.itu.cavabunga.core.service.IcalService;
import edu.itu.cavabunga.core.service.ParticipantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.awt.geom.AreaOp;

@RestController
@RequestMapping(path="/seeddb")
public class SeederController {
@Autowired
private CalendarManagerService calendarManagerService;

@Autowired
private ParticipantService participantService;

@Autowired
private IcalService icalService;




@GetMapping
@Transactional
public String seedDataBase(){
participantService.saveParticipant(participantService.createParticipant("testuser", ParticipantType.User));
participantService.saveParticipant(participantService.createParticipant("testgroup", ParticipantType.Group));


Component calendar = icalService.createComponent(ComponentType.Calendar);
Property prodid = icalService.createProperty(PropertyType.Prodid);
prodid.setValue("-//Test Inc//Cavabunga Calendar//");
Property version = icalService.createProperty(PropertyType.Version);
version.setValue("2.0");
Property calscale = icalService.createProperty(PropertyType.Calscale);
calscale.setValue("GREGORIAN");
Property method = icalService.createProperty(PropertyType.Method);
method.setValue("PUBLISH");
calendar.addProperty(prodid);
calendar.addProperty(version);
calendar.addProperty(calscale);
calendar.addProperty(method);

calendar.setOwner(calendarManagerService.getParticipantByUserName("testuser"));


Component timezone = icalService.createComponent(ComponentType.Timezone);
Property tzid = icalService.createProperty(PropertyType.Tzid);
tzid.setValue("Europe/Istanbul");
timezone.addProperty(tzid);
Component standard = icalService.createComponent(ComponentType.Standard);
Property tzoffsetfrom = icalService.createProperty(PropertyType.Tzoffsetfrom);
tzoffsetfrom.setValue("+0300");
Property tzoffsetto = icalService.createProperty(PropertyType.Tzoffsetto);
tzoffsetto.setValue("+0300");
Property tzname = icalService.createProperty(PropertyType.Tzname);
tzname.setValue("+03");
standard.addProperty(tzoffsetfrom);
standard.addProperty(tzoffsetto);
standard.addProperty(tzname);
timezone.addComponent(standard);
calendar.addComponent(timezone);

Component event = icalService.createComponent(ComponentType.Event);
Property dtstart = icalService.createProperty(PropertyType.Dtstart);
dtstart.setValue("20160423T170000Z");
Property dtend = icalService.createProperty(PropertyType.Dtend);
dtend.setValue("20160423T180000Z");
Property dtstamp = icalService.createProperty(PropertyType.Dtstamp);
dtstamp.setValue("20180520T181535Z");
Property organizer = icalService.createProperty(PropertyType.Organizer);
organizer.setValue("mailto:unknownorganizer@calendar.cavabunga.com");
Parameter cn = icalService.createParameter(ParameterType.Cn);
cn.setValue("unknownorganizer@calendar.google.com");
organizer.addParameter(cn);
Property uid = icalService.createProperty(PropertyType.Uid);
uid.setValue("7kukuqrfedlm2f9t0vr42q2qc8cm9l3o7vn9g00q3j3s5mhdo2ovuahsd9hf54qk3j60");
Property attendee = icalService.createProperty(PropertyType.Attendee);
attendee.setValue("mailto:dgkncelik@gmail.com");
Parameter cutype = icalService.createParameter(ParameterType.Cutype);
cutype.setValue("INDIVIDUAL");
Parameter role = icalService.createParameter(ParameterType.Role);
role.setValue("REQ-PARTICIPANT");
Parameter partstat = icalService.createParameter(ParameterType.Partstat);
partstat.setValue("ACCEPTED");
attendee.addParameter(cutype);
attendee.addParameter(role);
attendee.addParameter(partstat);
Property classs = icalService.createProperty(PropertyType.Class);
classs.setValue("PRIVATE");
Property created = icalService.createProperty(PropertyType.Created);
created.setValue("20160111T065955Z");
Property description = icalService.createProperty(PropertyType.Description);
description.setValue("---description_here---");
Property lastmod = icalService.createProperty(PropertyType.Lastmod);
lastmod.setValue("20160112T053844Z");
Property seq = icalService.createProperty(PropertyType.Seq);
seq.setValue("0");
event.addProperty(dtstart);
event.addProperty(dtend);
event.addProperty(dtstamp);
event.addProperty(organizer);
event.addProperty(uid);
event.addProperty(attendee);
event.addProperty(classs);
event.addProperty(created);
event.addProperty(description);
event.addProperty(lastmod);
event.addProperty(seq);
calendar.addComponent(event);

icalService.saveComponent(calendar);

return "ok";
}
}
79 changes: 76 additions & 3 deletions src/main/java/edu/itu/cavabunga/core/entity/Component.java
Expand Up @@ -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;
Expand All @@ -15,7 +18,6 @@
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
@EntityListeners(AuditingEntityListener.class)
@DiscriminatorOptions(force=true)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
Expand Down Expand Up @@ -69,7 +71,78 @@ public void addProperty(Property property){
properties.add(property);
}

public boolean validate(){
return true;
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 != 1){
throw new Validation("Component validation failed in required-one properties check. Count " + propertyCount + ": " + pt.create().getClass().getName());
}

propertyCount = 0;
}
}

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());
}

}
}
}
9 changes: 8 additions & 1 deletion src/main/java/edu/itu/cavabunga/core/entity/Parameter.java
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import edu.itu.cavabunga.core.entity.parameter.*;
import edu.itu.cavabunga.exception.Validation;
import lombok.Data;
import org.hibernate.annotations.DiscriminatorOptions;

Expand Down Expand Up @@ -48,8 +49,14 @@ public abstract class Parameter {

private String value;

@ManyToOne(cascade = CascadeType.ALL)
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "property_id")
@JsonBackReference
private Property property;

public void validate(){
if((this.value == null) || (this.value.trim().isEmpty())){
throw new Validation(this.getClass().getName() + " parameter cannot be empty");
}
}
}
18 changes: 11 additions & 7 deletions src/main/java/edu/itu/cavabunga/core/entity/Participant.java
@@ -1,9 +1,7 @@

package edu.itu.cavabunga.core.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.*;
import edu.itu.cavabunga.core.entity.participant.Group;
import edu.itu.cavabunga.core.entity.participant.User;
import lombok.Data;
Expand All @@ -21,7 +19,7 @@
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
@EntityListeners(AuditingEntityListener.class)
@DiscriminatorOptions(force=true)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonIgnoreProperties(ignoreUnknown = true, value= {"components"}) //TODO: participants with no component will give error while building JSON
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(value = User.class, name = "User"),
Expand All @@ -40,11 +38,17 @@ public abstract class Participant {
@CreatedDate
private Date creationDate;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "owner")

@OneToMany(fetch = FetchType.LAZY, mappedBy = "owner")
@JsonManagedReference(value = "participantAndComponent")
@JsonIgnore
private List<Component> components = new ArrayList<>();

public Participant(){

}
}

public void validate(){

}
}

0 comments on commit 57b0715

Please sign in to comment.