Skip to content

Commit

Permalink
- Converted to a Maven project only
Browse files Browse the repository at this point in the history
-  Added a bi-directional relationship between WorkSchedule and Rotation
-  Upgraded to JPA version 2.2
  • Loading branch information
point85 committed Feb 16, 2019
1 parent da26687 commit bf9d3f7
Show file tree
Hide file tree
Showing 26 changed files with 4,751 additions and 0 deletions.
Binary file added javadoc/javadoc.zip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.point85.workschedule.test.app;

import java.time.Duration;
import java.time.LocalTime;
import java.util.NoSuchElementException;
import java.util.Optional;

import org.point85.workschedule.WorkSchedule;

import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;

public class BaseWorkScheduleController {
// no text
protected static final String EMPTY_STRING = "";

// Reference to the main application
private WorkScheduleApp app;

// work schedule being edited
protected WorkSchedule currentSchedule;

protected WorkScheduleApp getApp() {
return this.app;
}

protected void setApp(WorkScheduleApp app) {
this.app = app;
}

// display a general alert
protected ButtonType showAlert(Stage dialogStage, AlertType type, String title, String header,
String errorMessage) {
// Show the error message.
Alert alert = new Alert(type);
alert.initOwner(dialogStage);
alert.setTitle(title);
alert.setHeaderText(header);
alert.setContentText(errorMessage);

Optional<ButtonType> result = alert.showAndWait();

ButtonType buttonType = null;
try {
buttonType = result.get();
} catch (NoSuchElementException e) {

}
return buttonType;
}

// display an error dialog
protected void showErrorDialog(Stage dialogStage, String message) {
showAlert(dialogStage, AlertType.ERROR, "Application Error", "Exception", message);
}

// display an error dialog
protected void showErrorDialog(Stage dialogStage, Exception e) {
String message = e.getMessage();

if (message == null) {
message = e.getClass().getSimpleName();
}
showAlert(dialogStage, AlertType.ERROR, "Application Error", "Exception", message);
}

// display an ok/cancel dialog
protected ButtonType showConfirmationDialog(Stage dialogStage, String message) {
return showAlert(dialogStage, AlertType.CONFIRMATION, "Confirmation", "Confirm Action", message);
}

protected Duration durationFromString(String hrsMins) throws Exception {

String[] fields = hrsMins.split(":");

if (fields.length != 2) {
throw new Exception("Both hours and minutes for the shift duration must be specified.");
}

long seconds = Integer.valueOf(fields[0]) * 3600 + Integer.valueOf(fields[1]) * 60;

Duration duration = Duration.ofSeconds(seconds);

return duration;
}

protected LocalTime localTimeFromString(String hrsMins) throws Exception {
String[] fields = hrsMins.split(":");

if (fields.length != 2) {
throw new Exception("Both hours and minutes for the start time of day must be specified.");
}

LocalTime time = LocalTime.of(Integer.valueOf(fields[0]), Integer.valueOf(fields[1]));

return time;
}

protected String stringFromLocalTime(LocalTime time) {
return String.format("%02d", time.getHour()) + ":" + String.format("%02d", time.getMinute());
}

protected String stringFromDuration(Duration duration) {
long seconds = duration.getSeconds();
long hours = seconds / 3600;
long minutes = (seconds - hours * 3600) / 60;

return String.format("%02d", hours) + ":" + String.format("%02d", minutes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/*
MIT License
Copyright (c) 2016 Kent Randall
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package org.point85.workschedule.test.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.point85.workschedule.Rotation;
import org.point85.workschedule.Team;
import org.point85.workschedule.WorkSchedule;

/**
* Class to persist a {@link WorkSchedule} to a database
*
* @author Kent Randall
*
*/
public class PersistentWorkSchedule {
// JPA persistence unit name
private static final String PERSISTENCE_UNIT = "WORK_SCHEDULE";

private static final String NQ_WS_BY_KEY = "WS.ByKey";
private static final String NQ_WS_BY_NAME = "WS.ByName";
private static final String NQ_WS_NAMES = "WS.Names";
private static final String NQ_TEAM_BY_KEY = "TEAM.ByKey";
private static final String NQ_ROTATION_CROSS_REF = "ROTATION.CrossRef";

// entity manager factory
private EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);

// entity manager
private EntityManager em;

private static PersistentWorkSchedule persistentWorkSchedule = new PersistentWorkSchedule();

private PersistentWorkSchedule() {
}

public static PersistentWorkSchedule getInstance() {
return persistentWorkSchedule;
}

// get the EntityManager, create if necessary
private EntityManager getEntityManager() {

if (em == null) {
em = emf.createEntityManager();
}
return em;

}

// execute the named query
List<?> executeNamedQuery(String queryName, Map<String, Object> parameters) {
Query query = getEntityManager().createNamedQuery(queryName);

if (parameters != null) {
for (Entry<String, Object> entry : parameters.entrySet()) {
query.setParameter(entry.getKey(), entry.getValue());
}
}
return query.getResultList();
}

public List<String> fetchNames() {
@SuppressWarnings("unchecked")
List<String> values = (List<String>) executeNamedQuery(PersistentWorkSchedule.NQ_WS_NAMES, null);

return values;
}

// fetch WorkSchedule by its primary key
public WorkSchedule fetchWorkScheduleByKey(Integer key) throws Exception {
WorkSchedule result = null;

if (key != null) {
Map<String, Object> parameters = new HashMap<>();
parameters.put("key", key);

result = fetchWorkSchedule(NQ_WS_BY_KEY, parameters);
}
return result;
}

// fetch WorkSchedule by its unique name
public WorkSchedule fetchWorkScheduleByName(String name) throws Exception {
WorkSchedule result = null;

if (name != null) {
Map<String, Object> parameters = new HashMap<>();
parameters.put("name", name);

result = fetchWorkSchedule(NQ_WS_BY_NAME, parameters);
}
return result;
}

// fetch WorkSchedule by a named query
WorkSchedule fetchWorkSchedule(String queryName, Map<String, Object> parameters) throws Exception {
WorkSchedule schedule = null;

Query query = getEntityManager().createNamedQuery(queryName);

if (parameters != null) {
for (Entry<String, Object> entry : parameters.entrySet()) {
query.setParameter(entry.getKey(), entry.getValue());
}
}

schedule = (WorkSchedule) query.getSingleResult();
return schedule;
}

// remove the WorkSchedule from the persistence context
public void evictWorkSchedule(WorkSchedule schedule) {
getEntityManager().detach(schedule);
}

// save the WorkSchedule to the database
public synchronized WorkSchedule saveWorkSchedule(WorkSchedule schedule) throws Exception {
try {
// start transaction
getEntityManager().getTransaction().begin();

// merge this entity into the PU
WorkSchedule merged = getEntityManager().merge(schedule);

// commit transaction
getEntityManager().getTransaction().commit();

return merged;
} catch (Throwable t) {
// roll back transaction
if (getEntityManager().getTransaction().isActive()) {
getEntityManager().getTransaction().rollback();
}
throw new Exception(t.getMessage());
}
}

// delete the WorkSchedule from the database
public synchronized void deleteWorkSchedule(WorkSchedule schedule) throws Exception {
if (schedule == null) {
return;
}

try {
// start transaction
getEntityManager().getTransaction().begin();

// delete
getEntityManager().remove(getEntityManager().merge(schedule));

// commit transaction
getEntityManager().getTransaction().commit();
} catch (Throwable t) {
// roll back transaction
if (getEntityManager().getTransaction().isActive()) {
getEntityManager().getTransaction().rollback();
}
throw new Exception(t.getMessage());
}
}

// fetch Team by its primary key
public Team fetchTeamByKey(Integer key) throws Exception {
Query query = getEntityManager().createNamedQuery(NQ_TEAM_BY_KEY);
query.setParameter("key", key);

Team team = (Team) query.getSingleResult();

return team;
}

// get any Team references to the Rotation
public List<Team> getCrossReferences(Rotation rotation) throws Exception {
Long key = rotation.getKey();

Query query = getEntityManager().createNamedQuery(NQ_ROTATION_CROSS_REF);
query.setParameter(1, key);

@SuppressWarnings("unchecked")
List<Integer> keys = (List<Integer>) query.getResultList();

List<Team> referencingTeams = new ArrayList<>(keys.size());

// get the referenced Teams
for (Integer primaryKey : keys) {
Team referencing = fetchTeamByKey(primaryKey);
referencingTeams.add(referencing);
}
return referencingTeams;
}
}
Loading

0 comments on commit bf9d3f7

Please sign in to comment.