From: Roland Häder Date: Sat, 3 Sep 2016 08:41:48 +0000 (+0200) Subject: Continued a bit: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e2d85c80c7561e928da8460f277e07e74be50870;p=jcontacts-business-core.git Continued a bit: - added "own" enumeration for day-of-week (the default is currently not supported by JPA) - see: http://stackoverflow.com/questions/2243730/jpa-hibernate-embedded-and-enum --- diff --git a/src/org/mxchange/jcontactsbusiness/opening_times/BusinessOpeningTimes.java b/src/org/mxchange/jcontactsbusiness/opening_times/BusinessOpeningTimes.java index 60cada6..56440d8 100644 --- a/src/org/mxchange/jcontactsbusiness/opening_times/BusinessOpeningTimes.java +++ b/src/org/mxchange/jcontactsbusiness/opening_times/BusinessOpeningTimes.java @@ -16,7 +16,6 @@ */ package org.mxchange.jcontactsbusiness.opening_times; -import java.time.DayOfWeek; import java.util.Calendar; import java.util.Objects; import javax.persistence.Basic; @@ -31,6 +30,7 @@ import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.Transient; +import org.mxchange.jcontactsbusiness.opening_times.dayofweek.DayOfTheWeek; /** * A POJO for business opening hours @@ -52,8 +52,8 @@ public class BusinessOpeningTimes implements OpeningTimes { * Ending day of opening hours (if applyable) */ @Column (name = "opening_times_end_day") - @Enumerated (EnumType.STRING) - private DayOfWeek endDay; + @Enumerated(EnumType.STRING) + private DayOfTheWeek endDay; /** * Ending time (hh:mm) @@ -76,8 +76,8 @@ public class BusinessOpeningTimes implements OpeningTimes { */ @Basic (optional = false) @Column (name = "opening_times_start_day", nullable = false) - @Enumerated (EnumType.STRING) - private DayOfWeek startDay; + @Enumerated(EnumType.STRING) + private DayOfTheWeek startDay; /** * Starting time (hh:mm) @@ -102,7 +102,7 @@ public class BusinessOpeningTimes implements OpeningTimes { * @param startDay Start day * @param startTime Start time */ - public BusinessOpeningTimes (final DayOfWeek endDay, final Calendar endTime, final Long id, final DayOfWeek startDay, final Calendar startTime) { + public BusinessOpeningTimes (final DayOfTheWeek endDay, final Calendar endTime, final Long id, final DayOfTheWeek startDay, final Calendar startTime) { // Call other constructor this(endDay, endTime, startDay, startTime); @@ -118,7 +118,7 @@ public class BusinessOpeningTimes implements OpeningTimes { * @param startDay Start day * @param startTime Start time */ - public BusinessOpeningTimes (final DayOfWeek endDay, final Calendar endTime, final DayOfWeek startDay, final Calendar startTime) { + public BusinessOpeningTimes (final DayOfTheWeek endDay, final Calendar endTime, final DayOfTheWeek startDay, final Calendar startTime) { // Set all fields this.endDay = endDay; this.endTime = endTime; @@ -154,12 +154,12 @@ public class BusinessOpeningTimes implements OpeningTimes { } @Override - public DayOfWeek getEndDay () { + public DayOfTheWeek getEndDay () { return this.endDay; } @Override - public void setEndDay (final DayOfWeek endDay) { + public void setEndDay (final DayOfTheWeek endDay) { this.endDay = endDay; } @@ -186,12 +186,12 @@ public class BusinessOpeningTimes implements OpeningTimes { } @Override - public DayOfWeek getStartDay () { + public DayOfTheWeek getStartDay () { return this.startDay; } @Override - public void setStartDay (final DayOfWeek startDay) { + public void setStartDay (final DayOfTheWeek startDay) { this.startDay = startDay; } diff --git a/src/org/mxchange/jcontactsbusiness/opening_times/OpeningTimes.java b/src/org/mxchange/jcontactsbusiness/opening_times/OpeningTimes.java index 5b6c3fe..b14b4fa 100644 --- a/src/org/mxchange/jcontactsbusiness/opening_times/OpeningTimes.java +++ b/src/org/mxchange/jcontactsbusiness/opening_times/OpeningTimes.java @@ -17,8 +17,8 @@ package org.mxchange.jcontactsbusiness.opening_times; import java.io.Serializable; -import java.time.DayOfWeek; import java.util.Calendar; +import org.mxchange.jcontactsbusiness.opening_times.dayofweek.DayOfTheWeek; /** * A POJI for opening times @@ -46,28 +46,28 @@ public interface OpeningTimes extends Serializable { *

* @return Starting day */ - DayOfWeek getStartDay (); + DayOfTheWeek getStartDay (); /** * Setter for starting day *

* @param startDay Starting day */ - void setStartDay (final DayOfWeek startDay); + void setStartDay (final DayOfTheWeek startDay); /** * Getter for ending day *

* @return Ending day */ - DayOfWeek getEndDay (); + DayOfTheWeek getEndDay (); /** * Setter for ending day *

* @param endDay Ending day */ - void setEndDay (final DayOfWeek endDay); + void setEndDay (final DayOfTheWeek endDay); /** * Getter for starting time diff --git a/src/org/mxchange/jcontactsbusiness/opening_times/dayofweek/DayOfTheWeek.java b/src/org/mxchange/jcontactsbusiness/opening_times/dayofweek/DayOfTheWeek.java new file mode 100644 index 0000000..29435d5 --- /dev/null +++ b/src/org/mxchange/jcontactsbusiness/opening_times/dayofweek/DayOfTheWeek.java @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2016 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcontactsbusiness.opening_times.dayofweek; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; + +/** + * An enumeration suitable for persisting + *

+ * @author Roland Haeder + */ +public enum DayOfTheWeek { + SUNDAY { + @Override + public int toCalendar () { + return Calendar.SUNDAY; + } + + }, + MONDAY { + @Override + public int toCalendar () { + return Calendar.MONDAY; + } + }, + TUESDAY { + @Override + public int toCalendar () { + return Calendar.TUESDAY; + } + }, + WEDNESDAY { + @Override + public int toCalendar () { + return Calendar.WEDNESDAY; + } + }, + THURSDAY { + @Override + public int toCalendar () { + return Calendar.THURSDAY; + } + }, + FRIDAY { + @Override + public int toCalendar () { + return Calendar.FRIDAY; + } + }, + SATURDAY { + @Override + public int toCalendar () { + return Calendar.SATURDAY; + } + }; + + public abstract int toCalendar (); + + public static DayOfTheWeek fromCalendarDay (final int day) { + + for (DayOfTheWeek dayOfWeek : DayOfTheWeek.values()) { + if (dayOfWeek.toCalendar() == day) { + return dayOfWeek; + } + } + + return null; // Consider throwing IllegalArgumentException + } + + public static DayOfTheWeek getByDate (final Date date) { + Calendar calendar = GregorianCalendar.getInstance(); + calendar.setTime(date); + return fromCalendarDay(calendar.get(Calendar.DAY_OF_WEEK)); + } + + /* + * Should return the localized day of the week + */ + @Override + public String toString () { + Calendar c = new GregorianCalendar(); + c.set(Calendar.DAY_OF_WEEK, this.toCalendar()); + SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat + .getInstance(); + sdf.applyPattern("EEEEEEEEEE"); //NOI18N + + return sdf.format(c.getTime()); + } + +}