Skip to content

Commit

Permalink
7: Провести рефакторинг примера сервисов
Browse files Browse the repository at this point in the history
Task-Url: #7
  • Loading branch information
amivanoff committed Jun 9, 2017
1 parent 14b8e1c commit 7f84a30
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 249 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void initUI(BorderPane pane) {
void destroyUI() {
if(calendarServices != null) {
for (ICalendarSourceProvider provider : calendarServices) {
provider.removeView(view);
provider.removeCalendarSources(view.getCalendarSources());
}
}
}
Expand All @@ -53,7 +53,7 @@ Node initWeek() {
view = new WeekPage();
if(calendarServices != null) {
for (ICalendarSourceProvider provider : calendarServices) {
provider.addView(view);
provider.addCalendarSources(view.getCalendarSources());
}
}
view.setRequestedTime(LocalTime.now());
Expand All @@ -66,7 +66,7 @@ Node initCal() {
((CalendarView)view).setTransitionsEnabled(true);
if(calendarServices != null) {
for (ICalendarSourceProvider provider : calendarServices) {
provider.addView(view);
provider.addCalendarSources(view.getCalendarSources());
}
}
view.setRequestedTime(LocalTime.now());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="ru.agentlab.calendar.consumer.impl.CalendarConsumerImpl">
<service>
<provide interface="ru.agentlab.calendar.service.api.ICalendarServiceConsumer"/>
<provide interface="ru.agentlab.calendar.consumer.ICalendarSourceProvider"/>
</service>
<reference bind="addCalendarService" interface="ru.agentlab.calendar.service.api.ICalendarService" name="CalendarService" policy="dynamic" unbind="removeCalendarService"/>
<reference bind="addCalendarService" cardinality="0..n" interface="ru.agentlab.calendar.service.api.ICalendarService" name="CalendarService" policy="dynamic" unbind="removeCalendarService"/>
<implementation class="ru.agentlab.calendar.consumer.impl.CalendarConsumerImpl"/>
</scr:component>
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@
*/
package ru.agentlab.calendar.consumer;

import com.calendarfx.view.DateControl;
import com.calendarfx.model.CalendarSource;

import javafx.collections.ObservableList;

/**
* @author Ivanov_AM
*
*/
public interface ICalendarSourceProvider {

/**
* TODO JavaDoc
*
* @param view
*/
void addView(DateControl view);

void removeView(DateControl view);
void addCalendarSources(ObservableList<CalendarSource> calendarSources);

void removeCalendarSources(ObservableList<CalendarSource> calendarSources);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,83 @@

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;

import com.calendarfx.model.Calendar.Style;
import com.calendarfx.model.CalendarEvent;
import com.calendarfx.model.CalendarSource;
import com.calendarfx.model.Entry;
import com.calendarfx.view.CalendarView;
import com.calendarfx.view.DateControl;
import com.google.common.collect.HashBiMap;

import javafx.application.Platform;
import javafx.collections.ObservableList;
import ru.agentlab.calendar.consumer.ICalendarSourceProvider;
import ru.agentlab.calendar.service.api.Calendar;
import ru.agentlab.calendar.service.api.Event;
import ru.agentlab.calendar.service.api.ICalendarService;
import ru.agentlab.calendar.service.api.ICalendarServiceConsumer;

@Component
public class CalendarConsumerImpl implements ICalendarServiceConsumer, ICalendarSourceProvider {
public class CalendarConsumerImpl implements ICalendarSourceProvider {

protected HashBiMap<ICalendarService, CalendarSource> calendarServicesSources = HashBiMap.create(1);

protected CalendarView view;
protected ObservableList<CalendarSource> viewSources;

@Reference(policy=ReferencePolicy.DYNAMIC)

@Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.MULTIPLE)
public void addCalendarService(ICalendarService service) {
CalendarSource fxCalendarSource = new CalendarSource(service.toString());
calendarServicesSources.put(service, fxCalendarSource);
if(view != null) {
runLater(() -> view.getCalendarSources().setAll(fxCalendarSource));
if(calendarServicesSources.containsKey(service)) {
return;
}

try {
CalendarSource fxCalendarSource = getAllEvents(service);
calendarServicesSources.put(service, fxCalendarSource);

if(viewSources != null) {
runLater(() -> viewSources.addAll(fxCalendarSource));
}
}
catch (Exception e) {
e.printStackTrace();
}
}

public void removeCalendarService(ICalendarService service) {
CalendarSource fxCalendarSource = calendarServicesSources.remove(service);
if(view != null) {
view.getCalendarSources().removeAll(fxCalendarSource);
if(viewSources != null) {
viewSources.removeAll(fxCalendarSource);
}
}

@Override
public void onCalendarsAdded(Collection<Calendar> calendars) {
public CalendarSource getAllEvents(ICalendarService service) throws Exception {
Collection<Calendar> calendars = service.getCalendars();
CalendarSource fxCalendarSource = new CalendarSource(service.toString());

for (ru.agentlab.calendar.service.api.Calendar calendar : calendars) {
com.calendarfx.model.Calendar fxCal = new com.calendarfx.model.Calendar();
fxCal.setShortName(calendar.getId());
String summary = calendar.getSummary();
fxCal.setName(summary);
fxCal.setStyle(Style.STYLE1);

ICalendarService calendarService = calendar.getSourceService();
CalendarSource fxCalendarSource = calendarServicesSources.get(calendarService);
if(fxCalendarSource == null) {
addCalendarService(service);
fxCalendarSource = calendarServicesSources.get(service);
}

fxCal.addEventHandler(evt -> {
if (evt.getEventType().equals(CalendarEvent.ENTRY_CHANGED)) {
calendarServicesSources.values().stream().filter(src -> src.getCalendars().contains(calendar)).findFirst().ifPresent(src2 -> {
ICalendarService service = calendarServicesSources.inverse().get(src2);
ICalendarService service2 = calendarServicesSources.inverse().get(src2);

Entry<?> entry = evt.getEntry();
Event event = new Event(entry.getTitle());
event.setStartDateTime(entry.getEndAsLocalDateTime());
event.setEndDateTime(entry.getStartAsLocalDateTime());
try {
service.addEvent(event);
service2.addEvent(event);
}
catch (Exception e) {
e.printStackTrace();
Expand All @@ -80,12 +94,50 @@ public void onCalendarsAdded(Collection<Calendar> calendars) {
});

try {
Collection<Entry<?>> entries = getEntries(calendar, calendarService);
List<Event> events = service.getEvents(calendar.getId());
Collection<Entry<?>> entries = new LinkedList<>();

for (Event event : events) {
Entry<String> entry = new Entry<String>();
entry.setTitle(event.getTitle());
entry.setId(event.getId());
entry.setLocation(event.getLocation());
entry.setUserObject(event.getDescription());

String recurrence = event.getRecurrence();
if(recurrence != null && (!recurrence.contains("RDATE"))) { //$NON-NLS-1$
entry.setRecurrenceRule(event.getRecurrence());
}

LocalDateTime startDateTime = event.getStartDateTime();
LocalDateTime endDateTime = event.getEndDateTime();

if((startDateTime != null) && (endDateTime != null)) {
entry.setInterval(startDateTime, endDateTime);

Period p = Period.between(startDateTime.toLocalDate(), endDateTime.toLocalDate());
if(p.getDays() > 0)
entry.setFullDay(true);
}
else {
if(startDateTime != null) {
entry.changeStartDate(startDateTime.toLocalDate());
entry.changeStartTime(startDateTime.toLocalTime());
}
if(endDateTime != null) {
entry.changeEndDate(endDateTime.toLocalDate());
entry.changeEndTime(endDateTime.toLocalTime());
}
}
entries.add(entry);
}

fxCal.addEntries(entries);

Runnable r = () -> fxCalendarSource.getCalendars().add(fxCal);
CalendarSource fxCalendarSource2 = fxCalendarSource;
Runnable r = () -> fxCalendarSource2.getCalendars().add(fxCal);

if(view != null ) {
if(viewSources != null ) {
runLater(r);
}
else {
Expand All @@ -96,79 +148,23 @@ public void onCalendarsAdded(Collection<Calendar> calendars) {
e.printStackTrace();
}
}
}

/**
* TODO JavaDoc
*
* @param calendar
* @param calendarService
* @return
* @throws Exception
*/
private Collection<Entry<?>> getEntries(ru.agentlab.calendar.service.api.Calendar calendar, ICalendarService calendarService) throws Exception {
List<Event> events = calendarService.getEvents(calendar.getId());
Collection<Entry<?>> entries = new LinkedList<>();
for (Event event : events) {
Entry<String> entry = new Entry<String>();
entry.setTitle(event.getTitle());
entry.setId(event.getId());
entry.setLocation(event.getLocation());
entry.setUserObject(event.getDescription());

String recurrence = event.getRecurrence();
if(recurrence != null && (!recurrence.contains("RDATE"))) { //$NON-NLS-1$
entry.setRecurrenceRule(event.getRecurrence());
}

LocalDateTime startDateTime = event.getStartDateTime();
LocalDateTime endDateTime = event.getEndDateTime();

if((startDateTime != null) && (endDateTime != null)) {
entry.setInterval(startDateTime, endDateTime);

Period p = Period.between(startDateTime.toLocalDate(), endDateTime.toLocalDate());
if(p.getDays() > 0)
entry.setFullDay(true);
}
else {
if(startDateTime != null) {
entry.changeStartDate(startDateTime.toLocalDate());
entry.changeStartTime(startDateTime.toLocalTime());
}
if(endDateTime != null) {
entry.changeEndDate(endDateTime.toLocalDate());
entry.changeEndTime(endDateTime.toLocalTime());
}
}
entries.add(entry);
}
return entries;
return fxCalendarSource;
}

void runLater(Runnable r) {
Platform.runLater(r);
}

@Override
public void onEventAdded(Event e) {
}

@Override
public void onEventDeleted(Event e) {
}

@Override
public void addView(DateControl view) {
public void addCalendarSources(ObservableList<CalendarSource> calendarSources) {
viewSources = calendarSources;
for (CalendarSource fxCalendarSource : calendarServicesSources.values()) {
runLater(() -> view.getCalendarSources().setAll(fxCalendarSource));
runLater(() -> viewSources.addAll(fxCalendarSource));
}
}

@Override
public void removeView(DateControl view) {
for (CalendarSource fxCalendarSource : calendarServicesSources.values()) {
runLater(() -> view.getCalendarSources().setAll(fxCalendarSource));
}
public void removeCalendarSources(ObservableList<CalendarSource> calendarSources) {
runLater(() -> calendarSources.clear());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class Event {
protected String id;
protected String title;

protected Calendar calendar;
protected String calendarId;
protected String description;

protected LocalDateTime startDateTime;
Expand Down Expand Up @@ -54,16 +54,16 @@ public void setTitle(String title) {
this.title = title;
}
/**
* @return the calendar
* @return the calendarId
*/
public Calendar getCalendar() {
return calendar;
public String getCalendarId() {
return calendarId;
}
/**
* @param calendar the calendar to set
* @param calendarId the calendar to set
*/
public void setCalendar(Calendar calendar) {
this.calendar = calendar;
public void setCalendarId(String calendarId) {
this.calendarId = calendarId;
}
/**
* @return the description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
public interface ICalendarService {
void addEvent(Event e) throws Exception;
void deleteEvent(Event e) throws Exception;
/**
* TODO JavaDoc
* @param id
*
* @return
* @throws Exception
*/

List<Calendar> getCalendars() throws Exception;

List<Event> getEvents(String id) throws Exception;
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ Bundle-ClassPath: .,
target/dependency/jetty-util-6.1.26.jar,
target/dependency/servlet-api-2.5-20081211.jar
Service-Component: OSGI-INF/ru.agentlab.calendar.service.google.GoogleServiceImpl.xml
Export-Package: com.google.api.services.calendar.model
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
<service>
<provide interface="ru.agentlab.calendar.service.api.ICalendarService"/>
</service>
<reference bind="addConsumer" cardinality="0..n" interface="ru.agentlab.calendar.service.api.ICalendarServiceConsumer" name="Consumer" policy="dynamic" unbind="removeConsumer"/>
<implementation class="ru.agentlab.calendar.service.google.GoogleServiceImpl"/>
</scr:component>
Loading

0 comments on commit 7f84a30

Please sign in to comment.