Skip to content

Commit

Permalink
Merge pull request #16 from ical4j/develop
Browse files Browse the repository at this point in the history
Support prototypes
  • Loading branch information
benfortuna authored Aug 3, 2024
2 parents a52e354 + 978d669 commit 2592ed0
Show file tree
Hide file tree
Showing 40 changed files with 337 additions and 20 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/create-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Create Release

on:
push:
tags:
- "ical4j-template-*"
- "!ical4j-template-*-pre"

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
# - name: Generate changelog
# run: make changelog
- name: Release
uses: softprops/action-gh-release@v2
with:
generate_release_notes: true
# body_path: CHANGELOG.md
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ dependencies {
"org.spockframework:spock-core"

// logging
testImplementation "org.slf4j:slf4j-log4j12:$slf4jVersion",
"org.apache.logging.log4j:log4j:$log4jVersion"
testImplementation "org.apache.logging.log4j:log4j-core:$log4jVersion"
}

test {
Expand Down
9 changes: 4 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
ical4jVersion = 4.0.0-rc4
ical4jVcardVersion = 2.0.0-beta3
ical4jVersion = 4.0.2
ical4jVcardVersion = 2.0.0-rc1
ical4jExtensionsVersion = 2.0.0-rc4

groovyVersion=3.0.20
groovyVersion=3.0.21
bndVersion = 6.1.0
slf4jVersion = 2.0.9
log4jVersion = 2.22.1
log4jVersion = 2.23.1
j2htmlVersion = 1.5.0

spockVersion = 2.4-M1-groovy-3.0
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/org/ical4j/template/AbstractTemplate.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package org.ical4j.template;

import net.fortuna.ical4j.model.PropertyContainer;

import java.lang.reflect.InvocationTargetException;
import java.util.function.UnaryOperator;

/**
* Base class for templates with the added ability to construct new object instances.
* Base class for templates with support for a prototype instance. A prototype is an
* instance of the applicable object type used to construct a new instance or modify
* an existing instance prior to applying the template.
*
* @param <T>
* @param <T> the applicable object type
*/
public abstract class AbstractTemplate<T> implements UnaryOperator<T> {
public abstract class AbstractTemplate<T extends PropertyContainer> implements Template<T> {

private final Class<? extends T> typeClass;

Expand All @@ -31,4 +34,11 @@ public T apply() throws NoSuchMethodException, InvocationTargetException, Instan

return apply(typeClass.getDeclaredConstructor().newInstance());
}

protected T applyPrototype(T target) {
if (prototype != null) {
getPrototype().getProperties().forEach(p -> target.add(p.copy()));
}
return target;
}
}
26 changes: 26 additions & 0 deletions src/main/java/org/ical4j/template/Template.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.ical4j.template;

import java.lang.reflect.InvocationTargetException;
import java.util.function.UnaryOperator;

/**
* A template encapsulates rules for creating and modifying complex iCalendar and
* vCard objects.
*
* @param <T> the applicable object type
*/
public interface Template<T> extends UnaryOperator<T> {

/**
* Apply the template to a new object instance. The use of this method requires
* that the applicable object type contains a default noargs constructor that
* can be used to create a new object instance.
* @return a new object instance resulting from applying the template
* @throws NoSuchMethodException if a noargs constructor doesn't exist on the applicable object type
* @throws InvocationTargetException an error resulting from invoking the noargs constructor
* @throws InstantiationException an error resulting from invoking the noargs constructor
* @throws IllegalAccessException an error resulting from invoking the noargs constructor
*/
T apply() throws NoSuchMethodException, InvocationTargetException, InstantiationException,
IllegalAccessException;
}
2 changes: 2 additions & 0 deletions src/main/java/org/ical4j/template/agile/Backlog.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public <T extends VToDo> Backlog(T prototype) {

@Override
public VToDo apply(VToDo vToDo) {
applyPrototype(vToDo);

return vToDo;
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/ical4j/template/agile/Criteria.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public <T extends VJournal> Criteria(T prototype) {

@Override
public VJournal apply(VJournal vJournal) {
applyPrototype(vJournal);

return vJournal;
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/ical4j/template/agile/Epic.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public <T extends VToDo> Epic(T prototype) {

@Override
public VToDo apply(VToDo vToDo) {
applyPrototype(vToDo);

return vToDo;
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/ical4j/template/agile/Retrospective.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public <T extends VToDo> Retrospective(T prototype) {

@Override
public VToDo apply(VToDo vToDo) {
applyPrototype(vToDo);

return vToDo;
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/ical4j/template/agile/Sprint.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public <T extends VEvent> Sprint(T prototype) {

@Override
public VEvent apply(VEvent vEvent) {
applyPrototype(vEvent);

return vEvent;
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/ical4j/template/agile/Story.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public <T extends VToDo> Story(T prototype) {

@Override
public VToDo apply(VToDo vToDo) {
applyPrototype(vToDo);

return vToDo;
}
}
7 changes: 5 additions & 2 deletions src/main/java/org/ical4j/template/groupware/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/**
* This represents an action undertaken by an actor in response to a related component.
* Typically, actions are chained together to form a workflow.
* Actions may be chained together to form a workflow.
*
* See: <a href="https://schema.org/Action">Action</a>
*/
Expand Down Expand Up @@ -58,9 +58,12 @@ public Action due(Temporal due) {

@Override
public VToDo apply(VToDo vToDo) {
vToDo.with(COMPLETED, completed);
applyPrototype(vToDo);

// vToDo.with(CONCEPT, ImmutableConcept.ACTION);
vToDo.with(SUMMARY, summary);
vToDo.with(DUE, due);
vToDo.with(COMPLETED, completed);
vToDo.with((BiFunction<VToDo, Participant, VToDo>) (c, p) -> { if (p != null) c.add(p); return c;},
participant);
return vToDo;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/ical4j/template/groupware/Agenda.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public Agenda nextItem(VToDo item) {

@Override
public VToDo apply(VToDo vToDo) {
applyPrototype(vToDo);

vToDo.replace(ActionType.AGENDA);
vToDo.with(SUMMARY, summary);
vToDo.with(DESCRIPTION, description);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/ical4j/template/groupware/Anniversary.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public Anniversary withDate(LocalDate date) {

@Override
public VEvent apply(VEvent vEvent) {
applyPrototype(vEvent);

vEvent.with(DTSTART, date);
vEvent.with(RRULE, schedule);
return vEvent;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/ical4j/template/groupware/Appointment.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public Appointment start(ZonedDateTime start) {

@Override
public VEvent apply(VEvent vEvent) {
applyPrototype(vEvent);

vEvent.with(DTSTART, start);
return vEvent;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/ical4j/template/groupware/Attendance.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public Attendance end(Temporal end) {

@Override
public VJournal apply(VJournal vJournal) {
applyPrototype(vJournal);

vJournal.with(DTSTART, start);
vJournal.with(DTEND, end);
vJournal.with(RELATED_COMPONENT, context);
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/org/ical4j/template/groupware/Individual.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.ical4j.template.groupware;

import net.fortuna.ical4j.vcard.GeneralPropertyModifiers;
import net.fortuna.ical4j.vcard.IdentificationPropertyModifiers;
import net.fortuna.ical4j.vcard.VCard;
import net.fortuna.ical4j.vcard.property.N;
import net.fortuna.ical4j.vcard.property.immutable.ImmutableKind;
import org.ical4j.template.AbstractTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Individual extends AbstractTemplate<VCard> {

private String familyName;

private String givenName;

private final List<String> additionalNames = new ArrayList<>();

private final List<String> prefixes = new ArrayList<>();

private final List<String> suffixes = new ArrayList<>();

public Individual() {
super(VCard.class);
}

public <T extends VCard> Individual(T prototype) {
super(prototype.getClass());
setPrototype(prototype);
}

public Individual familyName(String familyName) {
this.familyName = familyName;
return this;
}

public Individual givenName(String givenName) {
this.givenName = givenName;
return this;
}

public Individual additionalName(String... additionalName) {
this.additionalNames.addAll(Arrays.asList(additionalName));
return this;
}

public Individual prefix(String... prefix) {
this.prefixes.addAll(Arrays.asList(prefix));
return this;
}

public Individual suffix(String... suffix) {
this.suffixes.addAll(Arrays.asList(suffix));
return this;
}

@Override
public VCard apply(VCard vCard) {
applyPrototype(vCard);

vCard.with(GeneralPropertyModifiers.KIND, ImmutableKind.INDIVIDUAL);
vCard.with(IdentificationPropertyModifiers.N, new N(familyName, givenName,
additionalNames.toArray(new String[0]), prefixes.toArray(new String[0]),
suffixes.toArray(new String[0])));
return vCard;
}
}
5 changes: 1 addition & 4 deletions src/main/java/org/ical4j/template/groupware/Meeting.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@ public Meeting agenda(VToDo agenda) {

@Override
public VEvent apply(VEvent vEvent) {
// apply prototype..
if (getPrototype() != null) {
getPrototype().getProperties().forEach(vEvent::add);
}
applyPrototype(vEvent);

// apply mandatory properties..
vEvent.replace(EventType.MEETING);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/ical4j/template/groupware/Note.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public Note location(VLocation location) {

@Override
public VJournal apply(VJournal vJournal) {
applyPrototype(vJournal);

vJournal.with(SUMMARY, title);
vJournal.with(DTSTART, date);
vJournal.add(location);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/ical4j/template/groupware/Observance.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public Observance repeats(Repeats<LocalDate> schedule) {

@Override
public VEvent apply(VEvent vEvent) {
applyPrototype(vEvent);

vEvent.replace(ImmutableTransp.TRANSPARENT);
vEvent.with(DescriptivePropertyModifiers.SUMMARY, title);
vEvent.with(DateTimePropertyModifiers.DTSTART, start);
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/org/ical4j/template/groupware/Organization.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.ical4j.template.groupware;

import net.fortuna.ical4j.vcard.GeneralPropertyModifiers;
import net.fortuna.ical4j.vcard.IdentificationPropertyModifiers;
import net.fortuna.ical4j.vcard.VCard;
import net.fortuna.ical4j.vcard.property.Fn;
import net.fortuna.ical4j.vcard.property.immutable.ImmutableKind;
import org.ical4j.template.AbstractTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Organization extends AbstractTemplate<VCard> {

private final List<String> names = new ArrayList<>();

public Organization() {
super(VCard.class);
}

public <T extends VCard> Organization(T prototype) {
super(prototype.getClass());
setPrototype(prototype);
}

public Organization name(String...name) {
names.addAll(Arrays.asList(name));
return this;
}

@Override
public VCard apply(VCard vCard) {
applyPrototype(vCard);

vCard.with(GeneralPropertyModifiers.KIND, ImmutableKind.ORG);
names.forEach(name -> vCard.with(IdentificationPropertyModifiers.FN, new Fn(name)));
return vCard;
}
}
Loading

0 comments on commit 2592ed0

Please sign in to comment.