universe@34: /* universe@34: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@34: * universe@34: * Copyright 2018 Mike Becker. All rights reserved. universe@34: * universe@34: * Redistribution and use in source and binary forms, with or without universe@34: * modification, are permitted provided that the following conditions are met: universe@34: * universe@34: * 1. Redistributions of source code must retain the above copyright universe@34: * notice, this list of conditions and the following disclaimer. universe@34: * universe@34: * 2. Redistributions in binary form must reproduce the above copyright universe@34: * notice, this list of conditions and the following disclaimer in the universe@34: * documentation and/or other materials provided with the distribution. universe@34: * universe@34: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@34: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@34: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@34: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@34: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@34: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@34: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@34: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@34: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@34: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@34: * POSSIBILITY OF SUCH DAMAGE. universe@34: * universe@34: */ universe@34: package de.uapcore.lightpit.dao; universe@34: universe@34: import java.sql.SQLException; universe@128: import java.util.List; universe@34: universe@128: public interface ChildEntityDao { universe@128: universe@128: /** universe@128: * Lists all entities being a child of the specified parent. universe@128: * @param parent the parent universe@128: * @return the list of child instances universe@128: * @throws SQLException on any kind of SQL errors universe@128: */ universe@128: List list(P parent) throws SQLException; universe@38: universe@38: /** universe@47: * Finds an entity by its integer ID. universe@75: * It is not guaranteed that referenced entities are automatically joined. universe@47: * universe@47: * @param id the id universe@47: * @return the enity or null if there is no such entity universe@47: * @throws SQLException on any kind of SQL errors universe@47: */ universe@47: T find(int id) throws SQLException; universe@47: universe@47: /** universe@38: * Inserts an instance into database. universe@75: * It is not guaranteed that generated fields will be updated in the instance. universe@38: * universe@38: * @param instance the instance to insert universe@128: * @param parent a reference to the parent universe@38: * @throws SQLException on any kind of SQL errors universe@38: */ universe@128: void save(T instance, P parent) throws SQLException; universe@38: universe@38: /** universe@38: * Updates an instance in the database. universe@38: * universe@38: * @param instance the instance to insert universe@38: * @return true if an instance has been updated, false if no instance with the specified ID was found universe@38: * @throws SQLException on any kind of SQL errors universe@38: */ universe@38: boolean update(T instance) throws SQLException; universe@38: universe@38: /** universe@128: * Updates an instance in the database changing the parent. universe@128: * This operation is not supported by default. universe@128: * universe@128: * @param instance the instance to insert universe@128: * @param newParent a reference to the new parent universe@128: * @return true if an instance has been updated, false if no instance with the specified ID was found universe@128: * @throws SQLException on any kind of SQL errors universe@128: * @see #isChangingParentSupported() universe@128: */ universe@128: default boolean update(T instance, P newParent) throws SQLException { universe@128: throw new UnsupportedOperationException(); universe@128: } universe@128: universe@128: /** universe@128: * Returns true if changing the parent is supported by this DAO. universe@128: * This method must return true, if {@link #update(Object, Object)} is implemented. universe@128: * @return true, if changing the parent is supported universe@128: */ universe@128: default boolean isChangingParentSupported() { universe@128: return false; universe@128: } universe@128: universe@128: /** universe@38: * Inserts or updates an instance in the database. universe@38: * Tries an update first and if that fails, performs a save. universe@128: * If changing a parent is not supported by this DAO, universe@128: * specifying an alternate parent for an existing instance has no effect. universe@38: * universe@38: * @param instance the instance to insert or update universe@128: * @param parent a reference to the parent universe@38: * @throws SQLException on any kind of SQL errors universe@38: * @see #update(Object) universe@128: * @see #update(Object, Object) universe@128: * @see #save(Object, Object) universe@38: */ universe@128: default void saveOrUpdate(T instance, P parent) throws SQLException { universe@128: if (isChangingParentSupported()) { universe@128: if (!update(instance, parent)) save(instance, parent); universe@128: } else { universe@128: if (!update(instance)) save(instance, parent); universe@128: } universe@38: } universe@34: }