Commit b4c1dedd authored by Doğukan ÇELİK's avatar Doğukan ÇELİK

ical and core object parsers have been implemented.

parent c40cebba
......@@ -7,7 +7,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
@SpringBootApplication
@SpringBootApplication(scanBasePackages={"edu.itu.cavabunga"})
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class CavabungaCollectorLwnCalendarApplication {
......
package edu.itu.cavabunga.cavabungacollectorlwncalendar.controller;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.LwnObjectMapper;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element.CalMContinue;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element.CalMDate;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element.CalMEvent;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.parser.IcalParser;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.parser.LwnCalendarParser;
import edu.itu.cavabunga.lib.entity.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
......@@ -17,10 +21,21 @@ public class MainController {
@Autowired
private LwnCalendarParser lwnCalendarParser;
@GetMapping
public List<CalMEvent> parse(){
return lwnCalendarParser.parseCalendar(lwnCalendarParser.parseCalendarForDate("2018","07"),
lwnCalendarParser.parseCalendarForEvent("2018","07"));
@Autowired
private LwnObjectMapper lwnObjectMapper;
@Autowired
private IcalParser icalParser;
@GetMapping(value = "ics/{year}/{month}", produces = "text/calendar")
public String parseToIcs(@PathVariable(value = "year") String year, @PathVariable(value="month") String month){
return icalParser.parseComponentToIcal(lwnObjectMapper.mapEventsToCoreObject(lwnCalendarParser.parseCalendar(lwnCalendarParser.parseCalendarForDate("2018","07"),
lwnCalendarParser.parseCalendarForEvent(year,month))));
}
@GetMapping(value = "json/{year}/{month}", produces = "application/json")
public Component parseToJson(@PathVariable(value = "year") String year, @PathVariable(value="month") String month){
return lwnObjectMapper.mapEventsToCoreObject(lwnCalendarParser.parseCalendar(lwnCalendarParser.parseCalendarForDate("2018","07"),
lwnCalendarParser.parseCalendarForEvent(year,month)));
}
}
package edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element.CalMEvent;
import edu.itu.cavabunga.lib.entity.Parameter;
import edu.itu.cavabunga.lib.entity.Property;
import edu.itu.cavabunga.lib.entity.component.ComponentType;
import edu.itu.cavabunga.lib.entity.parameter.ParameterType;
import edu.itu.cavabunga.lib.entity.property.PropertyType;
import edu.itu.cavabunga.lib.factory.ParameterFactory;
import edu.itu.cavabunga.lib.factory.ParameterFactoryImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class LwnObjectMapper {
private edu.itu.cavabunga.lib.factory.ComponentFactory componentFactory;
private edu.itu.cavabunga.lib.factory.PropertyFactory propertyFactory;
private ParameterFactory parameterFactory;
@Autowired
public LwnObjectMapper(edu.itu.cavabunga.lib.factory.ComponentFactoryImpl componentFactory,
edu.itu.cavabunga.lib.factory.PropertyFactoryImpl propertyFactory,
ParameterFactoryImpl parameterFactory){
this.componentFactory = componentFactory;
this.propertyFactory = propertyFactory;
this.parameterFactory = parameterFactory;
}
public edu.itu.cavabunga.lib.entity.Component mapEventsToCoreObject(List<CalMEvent> calMEventList){
edu.itu.cavabunga.lib.entity.Component calendar = componentFactory.createComponent(ComponentType.Calendar);
Property version = propertyFactory.createProperty(PropertyType.Version);
version.setValue("2.0");
Property prodid = propertyFactory.createProperty(PropertyType.Prodid);
prodid.setValue("-//CAVABUNGA Open calendar server//");
Property calscale = propertyFactory.createProperty(PropertyType.Calscale);
calscale.setValue("GREGORIAN");
calendar.addProperty(version);
calendar.addProperty(prodid);
calendar.addProperty(calscale);
for(CalMEvent calMEvent : calMEventList){
edu.itu.cavabunga.lib.entity.Component event = componentFactory.createComponent(ComponentType.Event);
Property summary = propertyFactory.createProperty(PropertyType.Summary);
summary.setValue(calMEvent.getText());
Property url = propertyFactory.createProperty(PropertyType.Url);
url.setValue(calMEvent.getEventUrl());
Property dtstart = propertyFactory.createProperty(PropertyType.Dtstart);
dtstart.setValue(calMEvent.getEventDate());
Parameter value = parameterFactory.createParameter(ParameterType.Value);
value.setValue("DATE");
dtstart.addParameter(value);
Property dtend = propertyFactory.createProperty(PropertyType.Dtend);
dtend.setValue(calMEvent.getEventDate());
dtend.addParameter(value);
event.addProperty(summary);
event.addProperty(url);
event.addProperty(dtstart);
event.addProperty(dtend);
calendar.addComponent(event);
}
return calendar;
}
}
package edu.itu.cavabunga.cavabungacollectorlwncalendar.parser;
import edu.itu.cavabunga.lib.entity.Parameter;
import edu.itu.cavabunga.lib.entity.Property;
import org.springframework.stereotype.Component;
@Component
public class IcalParser {
public String parseComponentToIcal(edu.itu.cavabunga.lib.entity.Component component){
String result = "BEGIN:V" + component.getClass().getSimpleName().toUpperCase() + "\n";
if(component.getProperties() != null && !component.getProperties().isEmpty()){
for(Property p: component.getProperties()){
result = result + parsePropertyToIcal(p);
}
}
if(component.getComponents() != null && !component.getComponents().isEmpty()){
for(edu.itu.cavabunga.lib.entity.Component c : component.getComponents()){
result = result + parseComponentPropertyToIcal(c);
}
}
result = result + "END:V" + component.getClass().getSimpleName().toUpperCase() + "\n";
return result;
}
protected String parseComponentPropertyToIcal(edu.itu.cavabunga.lib.entity.Component component){
String result = "BEGIN:V" + component.getClass().getSimpleName().toUpperCase() + "\n";
if(component.getProperties() != null && !component.getProperties().isEmpty()){
for(Property t: component.getProperties()){
result = result + parsePropertyToIcal(t);
}
}
result = result + "END:V" + component.getClass().getSimpleName().toUpperCase() + "\n";
System.out.println(result);
return result;
}
public String parsePropertyToIcal(Property property){
String parameters = new String() ;
if(property.getParameters() != null && !property.getParameters().isEmpty()){
for(Parameter parameter: property.getParameters()){
parameters = parameters + ";" + parseParemeterToIcal(parameter);
}
}
System.out.println(property.getClass().getSimpleName().toUpperCase() + parameters + ":" + property.getValue());
return property.getClass().getSimpleName().toUpperCase() + parameters + ":" + property.getValue() + "\n";
}
public String parseParemeterToIcal(Parameter parameter){
System.out.print(parameter.getClass().getSimpleName().toUpperCase() + "=" + parameter.getValue());
return parameter.getClass().getSimpleName().toUpperCase() + "=" + parameter.getValue();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment