Permalink
Browse files

Database seeder

  • Loading branch information...
dgkncelik committed May 20, 2018
1 parent 490406b commit 7e74024a4d7b0d495e9314ee4ed74b708a39cb87
@@ -20,6 +20,7 @@
@ExceptionHandler(NotFound.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ErrorResponse handleNotFound(NotFound e){
System.out.printf(e.getMessage());
return new ErrorResponse(1,e.getMessage(), null);
}
@@ -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);
}
@@ -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);
}
@@ -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);
}
@@ -68,6 +72,7 @@ public ErrorResponse handleIllegalArgument(IllegalArgumentException e){
@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);
}
@@ -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()) {
@@ -178,7 +178,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()) {
@@ -255,7 +255,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()) {
@@ -0,0 +1,127 @@
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.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
public String seedDataBase(){
calendarManagerService.addParticipant(participantService.createParticipant("testuser", ParticipantType.User));
calendarManagerService.addParticipant(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";
}
}
@@ -18,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)
@@ -121,16 +120,13 @@ public void validateRequiredOneProperties(List<PropertyType> propertyTypeList){
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());
if(propertyCount != 1){
throw new Validation("Component validation failed in required-one properties check. Count " + propertyCount + ": " + pt.create().getClass().getName());
}
propertyCount = 0;
}
}
@@ -1,9 +1,6 @@
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;
@@ -21,7 +18,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"),
@@ -40,8 +37,10 @@
@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(){
@@ -225,7 +225,7 @@ public void validateValueType(PropertyValueType propertyValueType){
if (propertyValueType == PropertyValueType.UTCOFFSET) {
String[] parts = value.split(",");
for (String part : parts) {
if (!part.matches("^+[0-9]{4}$|^+[0-9]{4}Z$")) {
if (!part.matches("^[-+][0-9]{4}$|^[-+][0-9]{4}Z$")) {
throw new Validation(this.getClass().getName() + " value is not valid TIME type " + value);
}
}
@@ -43,7 +43,6 @@ public void validate(){
List<PropertyType> requiredOneList = new ArrayList<PropertyType>();
requiredOneList.add(PropertyType.Dtend);
requiredOneList.add(PropertyType.Duration);
super.validateRequiredOneProperties(requiredOneList);
}
}
@@ -42,7 +42,7 @@ public ParticipantServiceImpl(
public Participant createParticipant(String userName, ParticipantType participantType){
Participant newParticipant = participantFactory.createParticipant(participantType);
newParticipant.setUserName(userName);
participantRepository.save(newParticipant);
//participantRepository.save(newParticipant); //TODO: no need to create just create, while you saving: it will give error "New participant cannot have id field"
return newParticipant;
}

0 comments on commit 7e74024

Please sign in to comment.