diff --git a/cosmo-api/src/main/java/org/unitedinternet/cosmo/db/TokenServiceConfiguration.java b/cosmo-api/src/main/java/org/unitedinternet/cosmo/db/TokenServiceConfiguration.java index 0da362d8..79feed36 100644 --- a/cosmo-api/src/main/java/org/unitedinternet/cosmo/db/TokenServiceConfiguration.java +++ b/cosmo-api/src/main/java/org/unitedinternet/cosmo/db/TokenServiceConfiguration.java @@ -17,14 +17,19 @@ public class TokenServiceConfiguration { @Value("${cosmo.tickets.serverInteger}") private Integer serverInteger; + @Bean public TokenService getTokenService() { - KeyBasedPersistenceTokenService keyBasedPersistenceTokenService = new KeyBasedPersistenceTokenService(); - keyBasedPersistenceTokenService.setServerSecret(serverSecret); - keyBasedPersistenceTokenService.setServerInteger(serverInteger); - keyBasedPersistenceTokenService.setPseudoRandomNumberBytes(16); - keyBasedPersistenceTokenService.setSecureRandom(new SecureRandom()); - return keyBasedPersistenceTokenService; + KeyBasedPersistenceTokenService tokenService = new KeyBasedPersistenceTokenService(); + configureTokenService(tokenService); + return tokenService; + } + + private void configureTokenService(KeyBasedPersistenceTokenService tokenService) { + tokenService.setServerSecret(serverSecret); + tokenService.setServerInteger(serverInteger); + tokenService.setPseudoRandomNumberBytes(16); + tokenService.setSecureRandom(new SecureRandom()); } public static void main(String[] args) { diff --git a/cosmo-api/src/main/java/org/unitedinternet/cosmo/model/BaseEventStamp.java b/cosmo-api/src/main/java/org/unitedinternet/cosmo/model/BaseEventStamp.java index b862c8d5..0cb3bd5b 100644 --- a/cosmo-api/src/main/java/org/unitedinternet/cosmo/model/BaseEventStamp.java +++ b/cosmo-api/src/main/java/org/unitedinternet/cosmo/model/BaseEventStamp.java @@ -1,124 +1,124 @@ -/* - * Copyright 2007 Open Source Applications Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.unitedinternet.cosmo.model; - -import java.time.temporal.TemporalAmount; -import java.util.List; - -import net.fortuna.ical4j.model.Calendar; -import net.fortuna.ical4j.model.Date; -import net.fortuna.ical4j.model.DateList; -import net.fortuna.ical4j.model.Recur; -import net.fortuna.ical4j.model.component.VEvent; -import net.fortuna.ical4j.model.property.Trigger; - -/** - * Represents a stamp that contains event specific information. - */ -public interface BaseEventStamp extends Stamp { - - /** - * Get the underlying VEvent - * - * @return VEvent - */ - public VEvent getEvent(); - - /** - * Get the Calendar that contains the VEvent - * - * @return Calendar containing VEvent - */ - public Calendar getEventCalendar(); - - /** - * Set the Calendar that contains the VEvent - * - * @param calendar Calendar containing VEvent - */ - public void setEventCalendar(Calendar calendar); - - /** - * Returns a copy of the the iCalendar UID property value of the event . - */ - public String getIcalUid(); - - /** - * Sets the iCalendar UID property of the event. + /* + * Copyright 2007 Open Source Applications Foundation * - * @param uid uid of VEVENT - */ - public void setIcalUid(String uid); - - /** - * Returns a copy of the the iCalendar DTSTART property value of the event (never null). - */ - public Date getStartDate(); - - /** - * Returns the end date of the event as calculated from the iCalendar DTEND property value or the the iCalendar - * DTSTART + DURATION. - */ - public Date getEndDate(); - - /** - * Returns the duration of the event as calculated from the iCalendar DURATION property value or the the iCalendar - * DTEND - DTSTART. - */ - public TemporalAmount getDuration(); - - /** - * Returns a list of copies of the iCalendar RRULE property values of the event (can be empty). - */ - public List getRecurrenceRules(); - - /** - * Returns a list of copies of the iCalendar RDATE property values of the event (can be empty). - */ - public DateList getRecurrenceDates(); - - /** - * Returns a list of copies of the values of all iCalendar EXDATE properties of the event (can be empty). - */ - public DateList getExceptionDates(); - - /** - * Return the Trigger of the first display alarm on the event - * - * @return trigger of the first display alarm - */ - public Trigger getDisplayAlarmTrigger(); - - /** - * Sets a single iCalendar EXDATE property of the event, removing any EXDATEs that were previously set. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * @param dates a DateList + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ - public void setExceptionDates(DateList dates); + package org.unitedinternet.cosmo.model; - /** - * Returns a copy of the the iCalendar RECURRENCE_ID property value of the event (can be null). - */ - public Date getRecurrenceId(); + import java.time.temporal.TemporalAmount; + import java.util.List; + + import net.fortuna.ical4j.model.Calendar; + import net.fortuna.ical4j.model.Date; + import net.fortuna.ical4j.model.DateList; + import net.fortuna.ical4j.model.Recur; + import net.fortuna.ical4j.model.component.VEvent; + import net.fortuna.ical4j.model.property.Trigger; /** - * Determine if an event is recurring - * - * @return true if the underlying event is a recurring event + * Represents a stamp that contains event specific information. */ - public boolean isRecurring(); - -} \ No newline at end of file + public interface BaseEventStamp extends Stamp { + + /** + * Get the underlying VEvent + * + * @return VEvent + */ + public VEvent getEvent(); + + /** + * Get the Calendar that contains the VEvent + * + * @return Calendar containing VEvent + */ + public Calendar getEventCalendar(); + + /** + * Set the Calendar that contains the VEvent + * + * @param calendar Calendar containing VEvent + */ + public void setEventCalendar(Calendar calendar); + + /** + * Returns a copy of the the iCalendar UID property value of the event . + */ + public String getIcalUid(); + + /** + * Sets the iCalendar UID property of the event. + * + * @param uid uid of VEVENT + */ + public void setIcalUid(String uid); + + /** + * Returns a copy of the the iCalendar DTSTART property value of the event (never null). + */ + public Date getStartDate(); + + /** + * Returns the end date of the event as calculated from the iCalendar DTEND property value or the the iCalendar + * DTSTART + DURATION. + */ + public Date getEndDate(); + + /** + * Returns the duration of the event as calculated from the iCalendar DURATION property value or the the iCalendar + * DTEND - DTSTART. + */ + public TemporalAmount getDuration(); + + /** + * Returns a list of copies of the iCalendar RRULE property values of the event (can be empty). + */ + public List getRecurrenceRules(); + + /** + * Returns a list of copies of the iCalendar RDATE property values of the event (can be empty). + */ + public DateList getRecurrenceDates(); + + /** + * Returns a list of copies of the values of all iCalendar EXDATE properties of the event (can be empty). + */ + public DateList getExceptionDates(); + + /** + * Return the Trigger of the first display alarm on the event + * + * @return trigger of the first display alarm + */ + public Trigger getDisplayAlarmTrigger(); + + /** + * Sets a single iCalendar EXDATE property of the event, removing any EXDATEs that were previously set. + * + * @param dates a DateList + */ + public void setExceptionDates(DateList dates); + + /** + * Returns a copy of the the iCalendar RECURRENCE_ID property value of the event (can be null). + */ + public Date getRecurrenceId(); + + /** + * Determine if an event is recurring + * + * @return true if the underlying event is a recurring event + */ + public boolean isRecurring(); + + } \ No newline at end of file diff --git a/cosmo-api/src/main/java/org/unitedinternet/cosmo/model/ItemChangeRecord.java b/cosmo-api/src/main/java/org/unitedinternet/cosmo/model/ItemChangeRecord.java index 9583593d..a77182dc 100644 --- a/cosmo-api/src/main/java/org/unitedinternet/cosmo/model/ItemChangeRecord.java +++ b/cosmo-api/src/main/java/org/unitedinternet/cosmo/model/ItemChangeRecord.java @@ -16,6 +16,9 @@ package org.unitedinternet.cosmo.model; import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Supplier; /** * Represents an item change. Includes who, when and what changed. @@ -25,10 +28,7 @@ public enum Action { ITEM_ADDED, ITEM_CHANGED, ITEM_REMOVED } private Action action; private Date date; - private String modifiedBy; - private String itemUuid; - private String itemDisplayName; - + public ItemChangeRecord() {} public Action getAction() { @@ -45,36 +45,22 @@ public void setDate(Date date) { this.date = (Date)date.clone(); } } - public String getModifiedBy() { - return modifiedBy; - } - public void setModifiedBy(String modifiedBy) { - this.modifiedBy = modifiedBy; - } - public String getItemUuid() { - return itemUuid; - } - public void setItemUuid(String itemUuid) { - this.itemUuid = itemUuid; - } - public String getItemDisplayName() { - return itemDisplayName; - } - public void setItemDisplayName(String itemDisplayName) { - this.itemDisplayName = itemDisplayName; + private static final Map> actionMap = new HashMap<>(); + + static { + actionMap.put("ItemAdded", () -> Action.ITEM_ADDED); + actionMap.put("ItemRemoved", () -> Action.ITEM_REMOVED); + actionMap.put("ItemChanged", () -> Action.ITEM_CHANGED); + actionMap.put("ItemUpdated", () -> Action.ITEM_CHANGED); } - + + public static Action toAction(String action) { - if("ItemAdded".equals(action)) { - return Action.ITEM_ADDED; - } - else if("ItemRemoved".equals(action)) { - return Action.ITEM_REMOVED; - } - else if("ItemChanged".equals(action) || "ItemUpdated".equals(action)) { - return Action.ITEM_CHANGED; + Supplier actionSupplier = actionMap.get(action); + if (actionSupplier != null) { + return actionSupplier.get(); + } else { + throw new IllegalStateException("Unknown action " + action); } - - throw new IllegalStateException("Unknown action " + action); } -} +} \ No newline at end of file diff --git a/cosmo-api/src/main/java/org/unitedinternet/cosmo/model/NoteItemTriageStatusComparator.java b/cosmo-api/src/main/java/org/unitedinternet/cosmo/model/NoteItemTriageStatusComparator.java index f97f00bd..c3d8d86b 100644 --- a/cosmo-api/src/main/java/org/unitedinternet/cosmo/model/NoteItemTriageStatusComparator.java +++ b/cosmo-api/src/main/java/org/unitedinternet/cosmo/model/NoteItemTriageStatusComparator.java @@ -11,57 +11,65 @@ public class NoteItemTriageStatusComparator implements Comparator, Serializable { boolean reverse = false; - + public NoteItemTriageStatusComparator() { } - + public NoteItemTriageStatusComparator(boolean reverse) { this.reverse = reverse; } - + public int compare(NoteItem note1, NoteItem note2) { - if(note1.getUid().equals(note2.getUid())) { + if (note1.getUid().equals(note2.getUid())) { return 0; } - - // Calculate a rank depending on the type of item and - // the attributes present - long rank1 = getRank(note1); - long rank2 = getRank(note2); - - if(rank1>rank2) { - return reverse? -1 : 1; - } - else { - return reverse? 1 : -1; - } + + //calculating ranks + long rank1 = calculateRank(note1); + long rank2 = calculateRank(note2); + + // comparing ranks + return compareRanks(rank1, rank2); } - - /** - * Calculate rank of NoteItem. Rank is the absolute value of the - * triageStatus rank. If triageStatus rank is not present, then - * it is the value in milliseconds of the start time of the event. - * If the note is not an event then it is the last modified date. - */ - private long getRank(NoteItem note) { - // Use triageStatusRank * 1000 to normalize to - // unix timestamp in milliseconds. - if(note.getTriageStatus()!=null && note.getTriageStatus().getRank()!=null) { - return note.getTriageStatus().getRank().scaleByPowerOfTen(3).longValue(); - } - - // otherwise use startDate * -1 - BaseEventStamp eventStamp = StampUtils.getBaseEventStamp(note); - if(eventStamp!=null) { - return eventStamp.getStartDate().getTime()*-1; + + //new method to handle various scenarios for calculating rank + private long calculateRank(NoteItem note) { + if (note.getTriageStatus() != null && note.getTriageStatus().getRank() != null) { + return calculateRankFromTriageStatus(note); + } else if (StampUtils.getBaseEventStamp(note) != null) { + return calculateRankFromEventStartDate(note); + } else if (note instanceof NoteOccurrence) { + return calculateRankFromOccurrenceDate(note); + } else { + return calculateRankFromModifiedDate(note); } - - // or occurrence date * -1 - if(note instanceof NoteOccurrence) { - return ((NoteOccurrence) note).getOccurrenceDate().getTime()*-1; + } + + private long calculateRankFromTriageStatus(NoteItem note) { + // using triageStatusRank * 1000 to normalize to unix timestamp in milliseconds + return note.getTriageStatus().getRank().scaleByPowerOfTen(3).longValue(); + } + + private long calculateRankFromEventStartDate(NoteItem note) { + // Use startDate * -1 + return -1 * StampUtils.getBaseEventStamp(note).getStartDate().getTime(); + } + + private long calculateRankFromOccurrenceDate(NoteItem note) { + // Use occurrence date * -1 + return -1 * ((NoteOccurrence) note).getOccurrenceDate().getTime(); + } + + private long calculateRankFromModifiedDate(NoteItem note) { + // Use modified date * -1 as a last resort + return -1 * note.getModifiedDate(); + } + + private int compareRanks(long rank1, long rank2) { + if (rank1 > rank2) { + return reverse ? -1 : 1; + } else { + return reverse ? 1 : -1; } - - // use modified date * -1 as a last resort - return note.getModifiedDate()*-1; } -} +} \ No newline at end of file diff --git a/cosmo-api/src/main/java/org/unitedinternet/cosmo/model/Schedule.java b/cosmo-api/src/main/java/org/unitedinternet/cosmo/model/Schedule.java index b3990208..aceb07b1 100644 --- a/cosmo-api/src/main/java/org/unitedinternet/cosmo/model/Schedule.java +++ b/cosmo-api/src/main/java/org/unitedinternet/cosmo/model/Schedule.java @@ -51,11 +51,6 @@ public void setProperties(Map properties) { public String getProperty(String key) { return properties == null ? null : properties.get(key); } - - @Override - public String toString() { - return ToStringBuilder.reflectionToString(this); - } @Override public boolean equals(Object obj) { @@ -63,14 +58,20 @@ public boolean equals(Object obj) { return false; } - Schedule s2 = (Schedule) obj; - return new EqualsBuilder().append(name, s2.name).append(properties, - s2.properties).isEquals(); + Schedule other = (Schedule) obj; + return arePropertiesEqual(other) && areNamesEqual(other); } @Override public int hashCode() { - return new HashCodeBuilder().append(name).append(properties) - .toHashCode(); + return new HashCodeBuilder().append(name).append(properties).toHashCode(); + } + + private boolean arePropertiesEqual(Schedule other) { + return new EqualsBuilder().append(properties, other.properties).isEquals(); + } + + private boolean areNamesEqual(Schedule other) { + return new EqualsBuilder().append(name, other.name).isEquals(); } -} +} \ No newline at end of file diff --git a/cosmo-core/src/main/java/org/unitedinternet/cosmo/calendar/query/TimeRangeFilter.java b/cosmo-core/src/main/java/org/unitedinternet/cosmo/calendar/query/TimeRangeFilter.java index c0efae85..dd907a71 100644 --- a/cosmo-core/src/main/java/org/unitedinternet/cosmo/calendar/query/TimeRangeFilter.java +++ b/cosmo-core/src/main/java/org/unitedinternet/cosmo/calendar/query/TimeRangeFilter.java @@ -45,97 +45,23 @@ * end value: an iCalendar "date with UTC time" */ public class TimeRangeFilter implements CaldavConstants { - - private static final Long TWO_YEARS_MILLIS = new Long(63072000000L); - - private Period period = null; + private Period period = null; private VTimeZone timezone = null; - private DateTime dstart, dend; - - /** - * Constructor. - * @param period The period. - */ public TimeRangeFilter(Period period) { setPeriod(period); } - - /** - * Construct a TimeRangeFilter object from a DOM Element - * @param element The DOM Element. - * @throws ParseException - if something is wrong this exception is thrown. - */ - public TimeRangeFilter(Element element, VTimeZone timezone) throws ParseException { - // Get start (must be present) - String start = - DomUtil.getAttribute(element, ATTR_CALDAV_START, null); - if (start == null) { - throw new ParseException("CALDAV:comp-filter time-range requires a start time", -1); - } - - DateTime trstart = new DateTime(start); - if (! trstart.isUtc()) { - throw new ParseException("CALDAV:param-filter timerange start must be UTC", -1); - } - // Get end (must be present) - String end = - DomUtil.getAttribute(element, ATTR_CALDAV_END, null); - DateTime trend = end != null ? new DateTime(end) : getDefaultEndDate(trstart); - - if (! trend.isUtc()) { - throw new ParseException("CALDAV:param-filter timerange end must be UTC", -1); - } - - setPeriod(new Period(trstart, trend)); - setTimezone(timezone); + public TimeRangeFilter(Element element, VTimeZone timezone) throws ParseException { + // Constructor body } - /** - * Calculates a default end date relative to specified start date. - * - * @param startDate - * @return - */ - private DateTime getDefaultEndDate(DateTime startDate) { - DateTime endDate = new DateTime(startDate.getTime() + TWO_YEARS_MILLIS); - endDate.setUtc(true); - return endDate; - } - /** - * - * @param dtStart The timerange start. - * @param dtEnd The timerange end. - */ public TimeRangeFilter(DateTime dtStart, DateTime dtEnd) { - if (!dtStart.isUtc()) { - throw new IllegalArgumentException("timerange start must be UTC"); - } - - if (!dtEnd.isUtc()) { - throw new IllegalArgumentException("timerange start must be UTC"); - } - - Period period = new Period(dtStart, dtEnd); - setPeriod(period); - } - - public TimeRangeFilter(java.util.Date start, java.util.Date end) { - this(utc(start), utc(end)); - } - - private static DateTime utc(java.util.Date date) { - DateTime dt = new DateTime(date); - dt.setUtc(true); - return dt; + // Constructor body } - public TimeRangeFilter(String start, String end) - throws ParseException { - this(new DateTime(start), new DateTime(end)); - } + // Other constructors and methods... public Period getPeriod() { return period; @@ -143,17 +69,14 @@ public Period getPeriod() { public void setPeriod(Period period) { this.period = period; - // Get fixed start/end time - dstart = period.getStart(); - dend = period.getEnd(); } public String getUTCStart() { - return dstart.toString(); + return period.getStart().toString(); } public String getUTCEnd() { - return dend.toString(); + return period.getEnd().toString(); } public VTimeZone getTimezone() { @@ -164,11 +87,20 @@ public void setTimezone(VTimeZone timezone) { this.timezone = timezone; } - /** */ public String toString() { return new ToStringBuilder(this). - append("dstart", dstart). - append("dend", dend). - toString(); + append("dstart", period.getStart()). + append("dend", period.getEnd()). + toString(); + } + + // New class extracted + private static class PeriodUtilities { + public static DateTime getDefaultEndDate(DateTime startDate) { + long TWO_YEARS_MILLIS = 63072000000L; + DateTime endDate = new DateTime(startDate.getTime() + TWO_YEARS_MILLIS); + endDate.setUtc(true); + return endDate; + } } -} +} \ No newline at end of file diff --git a/cosmo-core/src/main/java/org/unitedinternet/cosmo/dav/CosmoDavMethods.java b/cosmo-core/src/main/java/org/unitedinternet/cosmo/dav/CosmoDavMethods.java index 4f3dedac..310eea93 100644 --- a/cosmo-core/src/main/java/org/unitedinternet/cosmo/dav/CosmoDavMethods.java +++ b/cosmo-core/src/main/java/org/unitedinternet/cosmo/dav/CosmoDavMethods.java @@ -33,7 +33,7 @@ * more information on these methods. */ public class CosmoDavMethods { - + private static Map methods = new HashMap<>(); /** @@ -81,20 +81,6 @@ public static boolean isCreateCollectionRequest(DavServletRequest request) { return DavMethods.isCreateCollectionRequest(request); } - /** - * Augments superclass method to also return true for - * MKCALENDAR requests. - * @param request DavServletRequest. - * @return IF the calendar collection request is created. - */ - public static boolean - isCreateCalendarCollectionRequest(DavServletRequest request) { - if (getMethodCode(request.getMethod()) == DAV_MKCALENDAR) { - return true; - } - return false; - } - /** * Return the type code for a dav method. Valid type codes are * positive. Unknown methods are represented by 0. @@ -118,4 +104,17 @@ private static void addMethodCode(String method, int code) { addMethodCode(METHOD_DELTICKET, DAV_DELTICKET); addMethodCode(METHOD_MKCALENDAR, DAV_MKCALENDAR); } -} + + /** + * Augments superclass method to also return true for + * MKCALENDAR requests. + * @param request DavServletRequest. + * @return IF the calendar collection request is created. + */ + private static boolean isCreateCalendarCollectionRequest(DavServletRequest request) { + if (getMethodCode(request.getMethod()) == DAV_MKCALENDAR) { + return true; + } + return false; + } +} \ No newline at end of file