src/main/java/de/uapcore/lightpit/dao/ChildEntityDao.java

Thu, 15 Oct 2020 18:36:05 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 15 Oct 2020 18:36:05 +0200
changeset 130
7ef369744fd1
parent 128
947d0f6a6a83
permissions
-rw-r--r--

adds the possibility to specify path parameters to RequestMapping

universe@34 1 /*
universe@34 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@34 3 *
universe@34 4 * Copyright 2018 Mike Becker. All rights reserved.
universe@34 5 *
universe@34 6 * Redistribution and use in source and binary forms, with or without
universe@34 7 * modification, are permitted provided that the following conditions are met:
universe@34 8 *
universe@34 9 * 1. Redistributions of source code must retain the above copyright
universe@34 10 * notice, this list of conditions and the following disclaimer.
universe@34 11 *
universe@34 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@34 13 * notice, this list of conditions and the following disclaimer in the
universe@34 14 * documentation and/or other materials provided with the distribution.
universe@34 15 *
universe@34 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@34 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@34 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@34 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@34 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@34 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@34 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@34 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@34 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@34 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@34 26 * POSSIBILITY OF SUCH DAMAGE.
universe@34 27 *
universe@34 28 */
universe@34 29 package de.uapcore.lightpit.dao;
universe@34 30
universe@34 31 import java.sql.SQLException;
universe@128 32 import java.util.List;
universe@34 33
universe@128 34 public interface ChildEntityDao<T, P> {
universe@128 35
universe@128 36 /**
universe@128 37 * Lists all entities being a child of the specified parent.
universe@128 38 * @param parent the parent
universe@128 39 * @return the list of child instances
universe@128 40 * @throws SQLException on any kind of SQL errors
universe@128 41 */
universe@128 42 List<T> list(P parent) throws SQLException;
universe@38 43
universe@38 44 /**
universe@47 45 * Finds an entity by its integer ID.
universe@75 46 * It is not guaranteed that referenced entities are automatically joined.
universe@47 47 *
universe@47 48 * @param id the id
universe@47 49 * @return the enity or null if there is no such entity
universe@47 50 * @throws SQLException on any kind of SQL errors
universe@47 51 */
universe@47 52 T find(int id) throws SQLException;
universe@47 53
universe@47 54 /**
universe@38 55 * Inserts an instance into database.
universe@75 56 * It is not guaranteed that generated fields will be updated in the instance.
universe@38 57 *
universe@38 58 * @param instance the instance to insert
universe@128 59 * @param parent a reference to the parent
universe@38 60 * @throws SQLException on any kind of SQL errors
universe@38 61 */
universe@128 62 void save(T instance, P parent) throws SQLException;
universe@38 63
universe@38 64 /**
universe@38 65 * Updates an instance in the database.
universe@38 66 *
universe@38 67 * @param instance the instance to insert
universe@38 68 * @return true if an instance has been updated, false if no instance with the specified ID was found
universe@38 69 * @throws SQLException on any kind of SQL errors
universe@38 70 */
universe@38 71 boolean update(T instance) throws SQLException;
universe@38 72
universe@38 73 /**
universe@128 74 * Updates an instance in the database changing the parent.
universe@128 75 * This operation is not supported by default.
universe@128 76 *
universe@128 77 * @param instance the instance to insert
universe@128 78 * @param newParent a reference to the new parent
universe@128 79 * @return true if an instance has been updated, false if no instance with the specified ID was found
universe@128 80 * @throws SQLException on any kind of SQL errors
universe@128 81 * @see #isChangingParentSupported()
universe@128 82 */
universe@128 83 default boolean update(T instance, P newParent) throws SQLException {
universe@128 84 throw new UnsupportedOperationException();
universe@128 85 }
universe@128 86
universe@128 87 /**
universe@128 88 * Returns true if changing the parent is supported by this DAO.
universe@128 89 * This method must return true, if {@link #update(Object, Object)} is implemented.
universe@128 90 * @return true, if changing the parent is supported
universe@128 91 */
universe@128 92 default boolean isChangingParentSupported() {
universe@128 93 return false;
universe@128 94 }
universe@128 95
universe@128 96 /**
universe@38 97 * Inserts or updates an instance in the database.
universe@38 98 * Tries an update first and if that fails, performs a save.
universe@128 99 * If changing a parent is not supported by this DAO,
universe@128 100 * specifying an alternate parent for an existing instance has no effect.
universe@38 101 *
universe@38 102 * @param instance the instance to insert or update
universe@128 103 * @param parent a reference to the parent
universe@38 104 * @throws SQLException on any kind of SQL errors
universe@38 105 * @see #update(Object)
universe@128 106 * @see #update(Object, Object)
universe@128 107 * @see #save(Object, Object)
universe@38 108 */
universe@128 109 default void saveOrUpdate(T instance, P parent) throws SQLException {
universe@128 110 if (isChangingParentSupported()) {
universe@128 111 if (!update(instance, parent)) save(instance, parent);
universe@128 112 } else {
universe@128 113 if (!update(instance)) save(instance, parent);
universe@128 114 }
universe@38 115 }
universe@34 116 }

mercurial