]> git.mxchange.org Git - jcontacts-business-core.git/commitdiff
Continued a bit:
authorRoland Häder <roland@mxchange.org>
Sat, 3 Sep 2016 08:41:48 +0000 (10:41 +0200)
committerRoland Häder <roland@mxchange.org>
Sat, 3 Sep 2016 09:05:45 +0000 (11:05 +0200)
- 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

src/org/mxchange/jcontactsbusiness/opening_times/BusinessOpeningTimes.java
src/org/mxchange/jcontactsbusiness/opening_times/OpeningTimes.java
src/org/mxchange/jcontactsbusiness/opening_times/dayofweek/DayOfTheWeek.java [new file with mode: 0644]

index 60cada6bf23ff565bd43fd6450cfb79c595cb05e..56440d82fb2e6e0ae7c1d5821add284325886241 100644 (file)
@@ -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;
        }
 
index 5b6c3fecd14a50bbceb8c9b2334efeb928cc7dcf..b14b4fa87edbb7509eef6d200bd0bb73fa5c29da 100644 (file)
@@ -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 {
         * <p>
         * @return Starting day
         */
-       DayOfWeek getStartDay ();
+       DayOfTheWeek getStartDay ();
 
        /**
         * Setter for starting day
         * <p>
         * @param startDay Starting day
         */
-       void setStartDay (final DayOfWeek startDay);
+       void setStartDay (final DayOfTheWeek startDay);
 
        /**
         * Getter for ending day
         * <p>
         * @return Ending day
         */
-       DayOfWeek getEndDay ();
+       DayOfTheWeek getEndDay ();
 
        /**
         * Setter for ending day
         * <p>
         * @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 (file)
index 0000000..29435d5
--- /dev/null
@@ -0,0 +1,107 @@
+/*\r
+ * Copyright (C) 2016 Roland Haeder\r
+ *\r
+ * This program is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
+package org.mxchange.jcontactsbusiness.opening_times.dayofweek;\r
+\r
+import java.text.SimpleDateFormat;\r
+import java.util.Calendar;\r
+import java.util.Date;\r
+import java.util.GregorianCalendar;\r
+\r
+/**\r
+ * An enumeration suitable for persisting\r
+ * <p>\r
+ * @author Roland Haeder<roland@mxchange.org>\r
+ */\r
+public enum DayOfTheWeek {\r
+       SUNDAY {\r
+               @Override\r
+               public int toCalendar () {\r
+                       return Calendar.SUNDAY;\r
+               }\r
+\r
+       },\r
+       MONDAY {\r
+               @Override\r
+               public int toCalendar () {\r
+                       return Calendar.MONDAY;\r
+               }\r
+       },\r
+       TUESDAY {\r
+               @Override\r
+               public int toCalendar () {\r
+                       return Calendar.TUESDAY;\r
+               }\r
+       },\r
+       WEDNESDAY {\r
+               @Override\r
+               public int toCalendar () {\r
+                       return Calendar.WEDNESDAY;\r
+               }\r
+       },\r
+       THURSDAY {\r
+               @Override\r
+               public int toCalendar () {\r
+                       return Calendar.THURSDAY;\r
+               }\r
+       },\r
+       FRIDAY {\r
+               @Override\r
+               public int toCalendar () {\r
+                       return Calendar.FRIDAY;\r
+               }\r
+       },\r
+       SATURDAY {\r
+               @Override\r
+               public int toCalendar () {\r
+                       return Calendar.SATURDAY;\r
+               }\r
+       };\r
+\r
+       public abstract int toCalendar ();\r
+\r
+       public static DayOfTheWeek fromCalendarDay (final int day) {\r
+\r
+               for (DayOfTheWeek dayOfWeek : DayOfTheWeek.values()) {\r
+                       if (dayOfWeek.toCalendar() == day) {\r
+                               return dayOfWeek;\r
+                       }\r
+               }\r
+\r
+               return null; // Consider throwing IllegalArgumentException\r
+       }\r
+\r
+       public static DayOfTheWeek getByDate (final Date date) {\r
+               Calendar calendar = GregorianCalendar.getInstance();\r
+               calendar.setTime(date);\r
+               return fromCalendarDay(calendar.get(Calendar.DAY_OF_WEEK));\r
+       }\r
+\r
+       /*\r
+        * Should return the localized day of the week\r
+        */\r
+       @Override\r
+       public String toString () {\r
+               Calendar c = new GregorianCalendar();\r
+               c.set(Calendar.DAY_OF_WEEK, this.toCalendar());\r
+               SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat\r
+                                                .getInstance();\r
+               sdf.applyPattern("EEEEEEEEEE"); //NOI18N\r
+\r
+               return sdf.format(c.getTime());\r
+       }\r
+\r
+}\r