/*
 * Copyright (C) 2014 Lucio Martínez <luciomartinez@openmailbox.org>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

package devtime.controller;

import devtime.dao.CategoriesDao;
import devtime.dao.CategoriesDaoImpl;
import devtime.dao.LanguagesDao;
import devtime.dao.LanguagesDaoImpl;
import devtime.dao.PlatformsDao;
import devtime.dao.PlatformsDaoImpl;
import devtime.dao.PrioritiesDao;
import devtime.dao.PrioritiesDaoImpl;
import devtime.entity.Categories;
import devtime.entity.Languages;
import devtime.entity.Platforms;
import devtime.entity.Priorities;
import devtime.util.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 *
 * @author Lucio Martínez <luciomartinez@openmailbox.org>
 */
public class EditEntitiesController {

    private static boolean emptyOrWitheSpace(String s) {
        return (s == null || s.trim().isEmpty());
    }

    private static boolean validName(String name) {
        return !emptyOrWitheSpace(name);
    }





    // *~* *~* *~* *~* *~* *~* //
    //                         //
    //      ADD ENTITIES       //
    //                         //
    // *~* *~* *~* *~* *~* *~* //

    /**
     * Add a new Category
     * @param name the name of the category
     * @return The new category if it has been generated successfully
     * @throws InvalidArgumentException when the name has an invalid format (e.g. empty)
     * @throws StorageException when there is an internal error on storage system
     */
    public static Categories addCategory(String name) throws InvalidArgumentException, StorageException {

        if (!validName(name))
            throw new InvalidArgumentException("The name entered is invalid.");

        Categories newObj = new Categories(name);
        Session session = HibernateUtil.getSessionFactory().openSession();

        try {

            session.beginTransaction();
            new CategoriesDaoImpl(session).add(newObj);
            session.getTransaction().commit();
            session.close();

            return newObj;

        } catch(HibernateException e) {
            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }
            throw new StorageException("The Category could not be save due to an \n"
                                    + "internal error while trying to store it.\n\n"
                                    + "Please, verify that the entered name \n"
                                    + "has not been used before.");
        }

    }


    /**
     * Add a new Language
     * @param name the name of the language
     * @return The new language if it has been generated successfully
     * @throws InvalidArgumentException when the name has an invalid format (e.g. empty)
     * @throws StorageException when there is an internal error on storage system
     */
    public static Languages addLanguage(String name) throws InvalidArgumentException, StorageException {

        if (!validName(name))
            throw new InvalidArgumentException("The name entered is invalid.");

        Languages newObj = new Languages(name);
        Session session = HibernateUtil.getSessionFactory().openSession();

        try {

            session.beginTransaction();
            new LanguagesDaoImpl(session).add(newObj);
            session.getTransaction().commit();
            session.close();

            return newObj;

        } catch(HibernateException e) {
            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }
            throw new StorageException("The Language could not be save due to an \n"
                                    + "internal error while trying to store it.\n\n"
                                    + "Please, verify that the entered name \n"
                                    + "has not been used before.");
        }

    }


    /**
     * Add a new Platform
     * @param name the name of the platform
     * @return The new platform if it has been generated successfully
     * @throws InvalidArgumentException when the name has an invalid format (e.g. empty)
     * @throws StorageException when there is an internal error on storage system
     */
    public static Platforms addPlatform(String name) throws InvalidArgumentException, StorageException {

        if (!validName(name))
            throw new InvalidArgumentException("The name entered is invalid.");

        Platforms newObj = new Platforms(name);
        Session session = HibernateUtil.getSessionFactory().openSession();

        try {

            session.beginTransaction();
            new PlatformsDaoImpl(session).add(newObj);
            session.getTransaction().commit();
            session.close();

            return newObj;

        } catch(HibernateException e) {
            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }
            throw new StorageException("The Platform could not be save due to an \n"
                                    + "internal error while trying to store it.\n\n"
                                    + "Please, verify that the entered name \n"
                                    + "has not been used before.");
        }

    }


    /**
     * Add a new Priority
     * @param name the name of the priority
     * @return The new priority if it has been generated successfully
     * @throws InvalidArgumentException when the name has an invalid format (e.g. empty)
     * @throws StorageException when there is an internal error on storage system
     */
    public static Priorities addPriority(String name) throws InvalidArgumentException, StorageException {

        if (!validName(name))
            throw new InvalidArgumentException("The name entered is invalid.");

        Priorities newObj = new Priorities(name);
        Session session = HibernateUtil.getSessionFactory().openSession();

        try {

            session.beginTransaction();
            new PrioritiesDaoImpl(session).add(newObj);
            session.getTransaction().commit();
            session.close();

            return newObj;

        } catch(HibernateException e) {
            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }
            throw new StorageException("The Priority could not be save due to an \n"
                                    + "internal error while trying to store it.\n\n"
                                    + "Please, verify that the entered name \n"
                                    + "has not been used before.");
        }

    }





    // *~* *~* *~* *~* *~* *~* //
    //                         //
    //      EDIT ENTITIES      //
    //                         //
    // *~* *~* *~* *~* *~* *~* //


    /**
     * Update an Category by its name
     * @param oldName the actual name of the Category
     * @param newName the new name of the Category
     * @throws InvalidArgumentException when the Category could not be found by the oldName
     * @throws StorageException when there is an internal error on storage system
     */
    public static void editCategory(String oldName, String newName) throws InvalidArgumentException, StorageException {
        Categories obj;

        if (!validName(newName))
            throw new InvalidArgumentException("The name entered is invalid.");

        Session session = HibernateUtil.getSessionFactory().openSession();
        CategoriesDao dao = (CategoriesDao) new CategoriesDaoImpl(session);

        try {

            session.beginTransaction();
            obj = dao.getElementByName(oldName);
            session.getTransaction().commit();

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Category could not be loaded due to an "
                                    + "internal error while trying to fetch it.");
        }

        if (obj == null)
            throw new InvalidArgumentException("No Category could be found with the name entered.");

        obj.setCategory(newName);

        try {

            session.beginTransaction();
            dao.update(obj);
            session.getTransaction().commit();
            session.close();

        } catch(HibernateException e) {
            // Rever the category name to its previous state!
            obj.setCategory(oldName);

            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }

            throw new StorageException("The Category could not be updated due to an "
                                    + "internal error while trying to store it.");
        }

    }


    /**
     * Update an Category by its ID
     * @param ID the ID of the Category
     * @param newName the new name of the Category
     * @throws InvalidArgumentException when the Category could not be found by the ID
     * @throws StorageException when there is an internal error on storage system
     */
    public static void editCategory(Integer ID, String newName) throws InvalidArgumentException, StorageException {
        Categories obj;

        if (!validName(newName))
            throw new InvalidArgumentException("The name entered is invalid.");

        Session session = HibernateUtil.getSessionFactory().openSession();
        CategoriesDao dao = (CategoriesDao) new CategoriesDaoImpl(session);

        try {

            session.beginTransaction();
            obj = dao.get(ID);
            session.getTransaction().commit();

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Category could not be loaded due to an "
                                    + "internal error while trying to fetch it.");
        }

        if (obj == null)
            throw new InvalidArgumentException("No Category could be found with the ID entered.");

        // Store previous name to be able to rollback at a later time
        String oldName = obj.getCategory();
        // Update the name
        obj.setCategory(newName);

        try {

            session.beginTransaction();
            dao.update(obj);
            session.getTransaction().commit();
            session.close();

        } catch(HibernateException e) {
            // Rever the category name to its previous state!
            obj.setCategory(oldName);

            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }

            throw new StorageException("The Category could not be updated due to an "
                                    + "internal error while trying to store it.");
        }

    }



    /**
     * Update a Language by its name
     * @param oldName the actual name of the Language
     * @param newName the new name of the Language
     * @throws InvalidArgumentException when the Language could not be found by the oldName
     * @throws StorageException when there is an internal error on storage system
     */
    public static void editLanguage(String oldName, String newName) throws InvalidArgumentException, StorageException {
        Languages obj;

        if (!validName(newName))
            throw new InvalidArgumentException("The name entered is invalid.");

        Session session = HibernateUtil.getSessionFactory().openSession();
        LanguagesDao dao = (LanguagesDao) new LanguagesDaoImpl(session);

        try {

            session.beginTransaction();
            obj = dao.getElementByName(oldName);
            session.getTransaction().commit();

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Language could not be loaded due to an "
                                    + "internal error while trying to fetch it.");
        }

        if (obj == null)
            throw new InvalidArgumentException("No Language could be found with the name entered.");

        obj.setLanguage(newName);

        try {

            session.beginTransaction();
            dao.update(obj);
            session.getTransaction().commit();
            session.close();

        } catch(HibernateException e) {
            // Rever the category name to its previous state!
            obj.setLanguage(oldName);

            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }

            throw new StorageException("The Language could not be updated due to an "
                                    + "internal error while trying to store it.");
        }

    }

    /**
     * Update a Language by its ID
     * @param ID the ID of the Language
     * @param newName the new name of the Language
     * @throws InvalidArgumentException when the Language could not be found by the ID
     * @throws StorageException when there is an internal error on storage system
     */
    public static void editLanguage(Short ID, String newName) throws InvalidArgumentException, StorageException {
        Languages obj;

        if (!validName(newName))
            throw new InvalidArgumentException("The name entered is invalid.");

        Session session = HibernateUtil.getSessionFactory().openSession();
        LanguagesDao dao = (LanguagesDao) new LanguagesDaoImpl(session);

        try {

            session.beginTransaction();
            obj = dao.get(ID);
            session.getTransaction().commit();

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Language could not be loaded due to an "
                                    + "internal error while trying to fetch it.");
        }

        if (obj == null)
            throw new InvalidArgumentException("No Language could be found with the ID entered.");

        // Store previous name to be able to rollback at a later time
        String oldName = obj.getLanguage();
        // Update the name
        obj.setLanguage(newName);

        try {

            session.beginTransaction();
            dao.update(obj);
            session.getTransaction().commit();
            session.close();

        } catch(HibernateException e) {
            // Rever the category name to its previous state!
            obj.setLanguage(oldName);

            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }

            throw new StorageException("The Language could not be updated due to an "
                                    + "internal error while trying to store it.");
        }

    }


    /**
     * Update a Platform by its name
     * @param oldName the actual name of the Platform
     * @param newName the new name of the Platform
     * @throws InvalidArgumentException when the Platform could not be found by the oldName
     * @throws StorageException when there is an internal error on storage system
     */
    public static void editPlatform(String oldName, String newName) throws InvalidArgumentException, StorageException {
        Platforms obj = null;

        if (!validName(newName))
            throw new InvalidArgumentException("The name entered is invalid.");

        Session session = HibernateUtil.getSessionFactory().openSession();
        PlatformsDao dao = (PlatformsDao) new PlatformsDaoImpl(session);

        try {

            session.beginTransaction();
            obj = dao.getElementByName(oldName);
            session.getTransaction().commit();

        } catch(HibernateException e) {
            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }
            throw new StorageException("The Platform could not be loaded due to an "
                                    + "internal error while trying to fetch it.");
        }

        if (obj == null)
            throw new InvalidArgumentException("No Platform could be found with the name entered.");

        obj.setPlatform(newName);

        try {

            session.beginTransaction();
            dao.update(obj);
            session.getTransaction().commit();
            session.close();

        } catch(HibernateException e) {
            // Rever the category name to its previous state!
            obj.setPlatform(oldName);

            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }

            throw new StorageException("The Platform could not be updated due to an "
                                    + "internal error while trying to store it.");
        }

    }

    /**
     * Update a Platform by its ID
     * @param ID the ID of the Platform
     * @param newName the new name of the Platform
     * @throws InvalidArgumentException when the Platform could not be found by the ID
     * @throws StorageException when there is an internal error on storage system
     */
    public static void editPlatform(Short ID, String newName) throws InvalidArgumentException, StorageException {
        Platforms obj;

        if (!validName(newName))
            throw new InvalidArgumentException("The name entered is invalid.");

        Session session = HibernateUtil.getSessionFactory().openSession();
        PlatformsDao dao = (PlatformsDao) new PlatformsDaoImpl(session);

        try {

            session.beginTransaction();
            obj = dao.get(ID);
            session.getTransaction().commit();

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Platform could not be loaded due to an "
                                    + "internal error while trying to fetch it.");
        }

        if (obj == null)
            throw new InvalidArgumentException("No Platform could be found with the ID entered.");

        // Store previous name to be able to rollback at a later time
        String oldName = obj.getPlatform();
        // Update the name
        obj.setPlatform(newName);

        try {

            session.beginTransaction();
            dao.update(obj);
            session.getTransaction().commit();
            session.close();

        } catch(HibernateException e) {
            // Rever the category name to its previous state!
            obj.setPlatform(oldName);

            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }

            throw new StorageException("The Platform could not be updated due to an "
                                    + "internal error while trying to store it.");
        }

    }


    /**
     * Update a Priority by its name
     * @param oldName the actual name of the Priority
     * @param newName the new name of the Priority
     * @throws InvalidArgumentException when the Priority could not be found by the oldName
     * @throws StorageException when there is an internal error on storage system
     */
    public static void editPriority(String oldName, String newName) throws InvalidArgumentException, StorageException {
        Priorities obj;

        if (!validName(newName))
            throw new InvalidArgumentException("The name entered is invalid.");

        Session session = HibernateUtil.getSessionFactory().openSession();
        PrioritiesDao dao = (PrioritiesDao) new PrioritiesDaoImpl(session);

        try {

            session.beginTransaction();
            obj = dao.getElementByName(oldName);
            session.getTransaction().commit();

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Priority could not be loaded due to an "
                                    + "internal error while trying to fetch it.");
        }

        if (obj == null)
            throw new InvalidArgumentException("No Priority could be found with the name entered.");

        obj.setPriority(newName);

        try {

            session.beginTransaction();
            dao.update(obj);
            session.getTransaction().commit();
            session.close();

        } catch(HibernateException e) {
            // Rever the category name to its previous state!
            obj.setPriority(oldName);

            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }

            throw new StorageException("The Priority could not be updated due to an "
                                    + "internal error while trying to store it.");
        }

    }

    /**
     * Update a Priority by its ID
     * @param ID the ID of the Priority
     * @param newName the new name of the Priority
     * @throws InvalidArgumentException when the Priority could not be found by the ID
     * @throws StorageException when there is an internal error on storage system
     */
    public static void editPriority(Short ID, String newName) throws InvalidArgumentException, StorageException {
        Priorities obj;

        if (!validName(newName))
            throw new InvalidArgumentException("The name entered is invalid.");

        Session session = HibernateUtil.getSessionFactory().openSession();
        PrioritiesDao dao = (PrioritiesDao) new PrioritiesDaoImpl(session);

        try {

            session.beginTransaction();
            obj = dao.get(ID);
            session.getTransaction().commit();

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Priority could not be loaded due to an "
                                    + "internal error while trying to fetch it.");
        }

        if (obj == null)
            throw new InvalidArgumentException("No Priority could be found with the ID entered.");

        // Store previous name to be able to rollback at a later time
        String oldName = obj.getPriority();
        // Update the name
        obj.setPriority(newName);

        try {

            session.beginTransaction();
            dao.update(obj);
            session.getTransaction().commit();
            session.close();

        } catch(HibernateException e) {
            // Rever the category name to its previous state!
            obj.setPriority(oldName);

            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }

            throw new StorageException("The Priority could not be updated due to an "
                                    + "internal error while trying to store it.");
        }

    }





    // *~* *~* *~* *~* *~* *~* //
    //                         //
    //      GET ENTITIES       //
    //                         //
    // *~* *~* *~* *~* *~* *~* //

    /**
     * Get all the Categories
     * @return a list of Categories
     * @throws StorageException when there is an internal error on storage system
     */
    public static List getAllCategories() throws StorageException {
        List l;
        Session session = HibernateUtil.getSessionFactory().openSession();

        try {

            session.beginTransaction();

            l = ((CategoriesDao) new CategoriesDaoImpl(session)).fetchAll();

            session.getTransaction().commit();
            session.close();

            return l;

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Categories could not be loaded due to an "
                                    + "internal error while trying to fetch them.");
        }

    }

    /**
     * Get all the Languages
     * @return a list of Languages
     * @throws StorageException when there is an internal error on storage system
     */
    public static List getAllLanguages() throws StorageException {
        List l;
        Session session = HibernateUtil.getSessionFactory().openSession();

        try {

            session.beginTransaction();

            l = ((LanguagesDao) new LanguagesDaoImpl(session)).fetchAll();

            session.getTransaction().commit();
            session.close();

            return l;

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Languages could not be loaded due to an "
                                    + "internal error while trying to fetch them.");
        }

    }

    /**
     * Get all the Platforms
     * @return a list of Platforms
     * @throws StorageException when there is an internal error on storage system
     */
    public static List getAllPlatforms() throws StorageException {
        List l;
        Session session = HibernateUtil.getSessionFactory().openSession();

        try {

            session.beginTransaction();

            l = ((PlatformsDao) new PlatformsDaoImpl(session)).fetchAll();

            session.getTransaction().commit();
            session.close();

            return l;

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Platforms could not be loaded due to an "
                                    + "internal error while trying to fetch them.");
        }

    }

    /**
     * Get all the Priorities
     * @return a list of Priorities
     * @throws StorageException when there is an internal error on storage system
     */
    public static List getAllPriorities() throws StorageException {
        List l;
        Session session = HibernateUtil.getSessionFactory().openSession();

        try {

            session.beginTransaction();

            l = ((PrioritiesDao) new PrioritiesDaoImpl(session)).fetchAll();

            session.getTransaction().commit();
            session.close();

            return l;

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Priorities could not be loaded due to an "
                                    + "internal error while trying to fetch them.");
        }

    }





    // *~* *~* *~* *~* *~* *~* //
    //                         //
    //      REMOVE ENTITIES    //
    //                         //
    // *~* *~* *~* *~* *~* *~* //

    /**
     * Remove an Category
     * @param ID object's primary key to find it on storage
     * @throws StorageException when there is an internal error on storage system
     * @throws InvalidArgumentException when the Platform could not be found by the ID
     */
    public static void removeCategory(Integer ID) throws StorageException, InvalidArgumentException {
        Categories obj;
        Session session = HibernateUtil.getSessionFactory().openSession();
        CategoriesDao dao = (CategoriesDao) new CategoriesDaoImpl(session);

        try {

            session.beginTransaction();
            obj = dao.get(ID);
            session.getTransaction().commit();

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Category could not be loaded due to an "
                                    + "internal error while trying to fetch it.");
        }

        if (obj == null)
            throw new InvalidArgumentException("No Category could be found with the ID entered.");

        try {

            session.beginTransaction();
            dao.delete(obj);
            session.getTransaction().commit();
            session.close();

        } catch(HibernateException e) {
            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }
            throw new StorageException("The Category could not be deleted due to an "
                                    + "internal error while trying to remove it.");
        }
    }

    /**
     * Remove a list of Categories
     * @param IDs a list of each object's primary key to find them on storage
     * @throws StorageException when there is an internal error on storage system
     * @throws InvalidArgumentException when the Platform could not be found by the ID
     */
    public static void removeCategories(List<Integer> IDs) throws StorageException, InvalidArgumentException {
        List<Categories> objs = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        CategoriesDao dao = (CategoriesDao) new CategoriesDaoImpl(session);

        try {

            session.beginTransaction();

            for (Integer id : IDs) {
                objs.add(dao.get(id));
            }

            session.getTransaction().commit();

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Categories could not be loaded due to an "
                                    + "internal error while trying to fetch it.");
        }

        try {

            session.beginTransaction();
            dao.delete(objs);
            session.getTransaction().commit();
            session.close();

        } catch(HibernateException e) {
            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }
            throw new StorageException("The Category could not be deleted due to an "
                                    + "internal error while trying to remove it.");
        }

    }



    /**
     * Remove a Language
     * @param ID object's primary key to find it on storage
     * @throws StorageException when there is an internal error on storage system
     * @throws InvalidArgumentException when the Platform could not be found by the ID
     */
    public static void removeLanguage(Short ID) throws StorageException, InvalidArgumentException {
        Languages obj;
        Session session = HibernateUtil.getSessionFactory().openSession();
        LanguagesDao dao = (LanguagesDao) new LanguagesDaoImpl(session);

        try {

            session.beginTransaction();
            obj = dao.get(ID);
            session.getTransaction().commit();

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Language could not be loaded due to an "
                                    + "internal error while trying to fetch it.");
        }

        if (obj == null)
            throw new InvalidArgumentException("No Language could be found with the ID entered.");

        try {

            session.beginTransaction();
            dao.delete(obj);
            session.getTransaction().commit();
            session.close();

        } catch(HibernateException e) {
            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }
            throw new StorageException("The Language could not be deleted due to an "
                                    + "internal error while trying to remove it.");
        }
    }

    /**
     * Remove a list of Languages
     * @param IDs a list of each object's primary key to find them on storage
     * @throws StorageException when there is an internal error on storage system
     * @throws InvalidArgumentException when the Platform could not be found by the ID
     */
    public static void removeLanguages(List<Short> IDs) throws StorageException, InvalidArgumentException {
        List<Languages> objs = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        LanguagesDao dao = (LanguagesDao) new LanguagesDaoImpl(session);

        try {

            session.beginTransaction();

            for (Short id : IDs) {
                objs.add(dao.get(id));
            }

            session.getTransaction().commit();

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Language could not be loaded due to an "
                                    + "internal error while trying to fetch it.");
        }

        try {

            session.beginTransaction();
            dao.delete(objs);
            session.getTransaction().commit();
            session.close();

        } catch(HibernateException e) {
            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }
            throw new StorageException("The Language could not be deleted due to an "
                                    + "internal error while trying to remove it.");
        }

    }


    /**
     * Remove a Platform
     * @param ID object's primary key to find it on storage
     * @throws StorageException when there is an internal error on storage system
     * @throws InvalidArgumentException when the Platform could not be found by the ID
     */
    public static void removePlatform(Short ID) throws StorageException, InvalidArgumentException {
        Platforms obj;
        Session session = HibernateUtil.getSessionFactory().openSession();
        PlatformsDao dao = (PlatformsDao) new PlatformsDaoImpl(session);

        try {

            session.beginTransaction();
            obj = dao.get(ID);
            session.getTransaction().commit();

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Platform could not be loaded due to an "
                                    + "internal error while trying to fetch it.");
        }

        if (obj == null)
            throw new InvalidArgumentException("No Platform could be found with the ID entered.");

        try {

            session.beginTransaction();
            dao.delete(obj);
            session.getTransaction().commit();
            session.close();

        } catch(HibernateException e) {
            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }
            throw new StorageException("The Platform could not be deleted due to an "
                                    + "internal error while trying to remove it.");
        }
    }

    /**
     * Remove a list of Platforms
     * @param IDs a list of each object's primary key to find them on storage
     * @throws StorageException when there is an internal error on storage system
     * @throws InvalidArgumentException when the Platform could not be found by the ID
     */
    public static void removePlatforms(List<Short> IDs) throws StorageException, InvalidArgumentException {
        List<Platforms> objs = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        PlatformsDao dao = (PlatformsDao) new PlatformsDaoImpl(session);

        try {

            session.beginTransaction();

            for (Short id : IDs) {
                objs.add(dao.get(id));
            }

            session.getTransaction().commit();

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Platform could not be loaded due to an "
                                    + "internal error while trying to fetch it.");
        }

        try {

            session.beginTransaction();
            dao.delete(objs);
            session.getTransaction().commit();
            session.close();

        } catch(HibernateException e) {
            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }
            throw new StorageException("The Platform could not be deleted due to an "
                                    + "internal error while trying to remove it.");
        }

    }


    /**
     * Remove a Priority
     * @param ID object's primary key to find it on storage
     * @throws StorageException when there is an internal error on storage system
     * @throws InvalidArgumentException when the Priority could not be found by the ID
     */
    public static void removePriority(Short ID) throws StorageException, InvalidArgumentException {
        Priorities obj = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        PrioritiesDao dao = (PrioritiesDao) new PrioritiesDaoImpl(session);

        try {

            session.beginTransaction();
            obj = dao.get(ID);
            session.getTransaction().commit();

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Priority could not be loaded due to an "
                                    + "internal error while trying to fetch it.");
        }

        if (obj == null)
            throw new InvalidArgumentException("No Priority could be found with the ID entered.");

        try {

            session.beginTransaction();
            dao.delete(obj);
            session.getTransaction().commit();
            session.close();

        } catch(HibernateException e) {
            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }
            throw new StorageException("The Priority could not be deleted due to an "
                                    + "internal error while trying to remove it.");
        }
    }

    /**
     * Remove a list of Priorities
     * @param IDs a list of each object's primary key to find them on storage
     * @throws StorageException when there is an internal error on storage system
     * @throws InvalidArgumentException when the Priority could not be found by the ID
     */
    public static void removePriorities(List<Short> IDs) throws StorageException, InvalidArgumentException {
        List<Priorities> objs = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        PrioritiesDao dao = (PrioritiesDao) new PrioritiesDaoImpl(session);

        try {

            session.beginTransaction();

            for (Short id : IDs) {
                objs.add(dao.get(id));
            }

            session.getTransaction().commit();

        } catch(HibernateException e) {
            if (session != null) {
                session.close();
            }
            throw new StorageException("The Priority could not be loaded due to an "
                                    + "internal error while trying to fetch it.");
        }

        try {

            session.beginTransaction();
            dao.delete(objs);
            session.getTransaction().commit();
            session.close();

        } catch(HibernateException e) {
            if (session != null) {
                session.getTransaction().rollback();
                session.close();
            }
            throw new StorageException("The Priority could not be deleted due to an "
                                    + "internal error while trying to remove it.");
        }

    }

}
