net.navasoft.madcoin.backend.model.controller.impl.StatusDataAccess.java Source code

Java tutorial

Introduction

Here is the source code for net.navasoft.madcoin.backend.model.controller.impl.StatusDataAccess.java

Source

/*******************************************************************************
 * Copyright 2014 Juan Diego Navarre Gonzalez
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package net.navasoft.madcoin.backend.model.controller.impl;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import net.navasoft.madcoin.backend.model.controller.exceptions.IllegalOrphanException;
import net.navasoft.madcoin.backend.model.controller.exceptions.NonexistentEntityException;
import net.navasoft.madcoin.backend.model.controller.helper.ComplexId;
import net.navasoft.madcoin.backend.model.controller.helper.impl.JPAHelper;
import net.navasoft.madcoin.backend.model.entities.ReferenceMode;
import net.navasoft.madcoin.backend.model.entities.impl.AppUserXStatus;
import net.navasoft.madcoin.backend.model.entities.impl.AppUsers;
import net.navasoft.madcoin.backend.model.entities.impl.FaultActions;
import net.navasoft.madcoin.backend.model.entities.impl.UserStatus;
import net.navasoft.madcoin.backend.services.rest.impl.IDataAccess;

import org.apache.commons.lang.NotImplementedException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * net.navasoft.madcoin.backend.model.controller.impl Class class
 * StatusDataAccess. Description:
 * 
 * @author Juan Diego Navarre Gonzalez - (${authorMail})
 * @version 1.0
 * @since 2/09/2014 09:31:47 PM
 */
@Repository("statusDAO")
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class StatusDataAccess extends JPAHelper<AppUserXStatus> implements IDataAccess<AppUserXStatus> {

    /**
     * Instantiates a new status data access.
     * 
     * @since 2/09/2014, 09:31:47 PM
     */
    public StatusDataAccess() {
        storage = StorageFactory.buildStorage();
    }

    /**
     * entity manager.
     * 
     * @since 2/09/2014, 09:31:47 PM
     */
    @PersistenceContext(unitName = "ClickNDone-PU")
    private EntityManager entityManager;

    /**
     * storage.
     * 
     * @since 2/09/2014, 09:31:47 PM
     */
    private Storage storage;

    /**
     * Creates the.
     * 
     * @param appUserXStatus
     *            the app user x status
     * @return the app user x status
     * @since 2/09/2014, 09:31:47 PM
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public AppUserXStatus create(AppUserXStatus appUserXStatus) {
        preConstruct(appUserXStatus);
        entityManager.persist(appUserXStatus);
        entityManager.flush();
        appUserXStatus = findLast(entityManager, AppUserXStatus.class, appUserXStatus);
        postConstruct(appUserXStatus);
        return appUserXStatus;
    }

    /**
     * Pre construct.
     * 
     * @param entity
     *            the entity
     * @since 2/09/2014, 09:31:47 PM
     */
    @Override
    public void preConstruct(AppUserXStatus entity) {

        UserStatus idUserStatus = entity.getIdUserStatus();
        if (idUserStatus != null) {
            idUserStatus = entityManager.getReference(idUserStatus.getClass(), idUserStatus.getIdUserStatus());
            entity.setIdUserStatus(idUserStatus);
            storage.storeReference(StatusStorageKeys.APP_STATUS, ReferenceMode.NEW_REFERENCE, idUserStatus);
        }

        FaultActions idFaultApplication = entity.getIdFaultApplication();
        if (idFaultApplication != null) {
            idFaultApplication = entityManager.getReference(idFaultApplication.getClass(),
                    idFaultApplication.getIdFaultAction());
            entity.setIdFaultApplication(idFaultApplication);
            storage.storeReference(StatusStorageKeys.FAULT_ACTIONS, ReferenceMode.NEW_REFERENCE,
                    idFaultApplication);
        }

        AppUsers idUser = entity.getIdUser();
        if (idUser != null) {
            idUser = entityManager.getReference(idUser.getClass(), idUser.getIdApplicationUsers());
            entity.setIdUser(idUser);
            storage.storeReference(StatusStorageKeys.APP_USER, ReferenceMode.NEW_REFERENCE, idUser);
        }
    }

    /**
     * Post construct.
     * 
     * @param entity
     *            the entity
     * @since 2/09/2014, 09:31:47 PM
     */
    @Override
    public void postConstruct(AppUserXStatus entity) {

        UserStatus idUserStatus;
        FaultActions idFaultApplication;
        AppUsers idUser;

        if (storage.validateReference(StatusStorageKeys.APP_STATUS, ReferenceMode.NEW_REFERENCE)) {
            idUserStatus = (UserStatus) storage.extractReference(StatusStorageKeys.APP_STATUS,
                    ReferenceMode.NEW_REFERENCE);
            idUserStatus.getAppUserXStatusCollection().add(entity);
            idUserStatus = entityManager.merge(idUserStatus);
        }
        if (storage.validateReference(StatusStorageKeys.FAULT_ACTIONS, ReferenceMode.NEW_REFERENCE)) {
            idFaultApplication = (FaultActions) storage.extractReference(StatusStorageKeys.FAULT_ACTIONS,
                    ReferenceMode.NEW_REFERENCE);
            idFaultApplication.getAppUserXStatusCollection().add(entity);
            idFaultApplication = entityManager.merge(idFaultApplication);
        }

        if (storage.validateReference(StatusStorageKeys.APP_USER, ReferenceMode.NEW_REFERENCE)) {
            idUser = (AppUsers) storage.extractReference(StatusStorageKeys.APP_USER, ReferenceMode.NEW_REFERENCE);
            idUser.getAppUserXStatusCollection().add(entity);
            idUser = entityManager.merge(idUser);
        }
        storage.clean();
    }

    /**
     * Edits the.
     * 
     * @param appUserXStatus
     *            the app user x status
     * @return the app user x status
     * @since 2/09/2014, 09:31:47 PM
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public AppUserXStatus edit(AppUserXStatus appUserXStatus) {
        preEdit(appUserXStatus);
        appUserXStatus = entityManager.merge(appUserXStatus);
        entityManager.flush();
        postEdit(appUserXStatus);
        return appUserXStatus;
    }

    /**
     * Pre edit.
     * 
     * @param status
     *            the status
     * @since 2/09/2014, 09:31:47 PM
     */
    @Override
    public void preEdit(AppUserXStatus status) {
        AppUserXStatus oldStatus = entityManager.find(AppUserXStatus.class, status.getIdUserXStatus());

        UserStatus idUserStatusOld = oldStatus.getIdUserStatus();
        storage.storeReference(StatusStorageKeys.APP_STATUS, ReferenceMode.OLD_REFERENCE, idUserStatusOld);
        UserStatus idUserStatusNew = status.getIdUserStatus();

        FaultActions idFaultApplicationOld = oldStatus.getIdFaultApplication();
        storage.storeReference(StatusStorageKeys.FAULT_ACTIONS, ReferenceMode.OLD_REFERENCE, idFaultApplicationOld);
        FaultActions idFaultApplicationNew = status.getIdFaultApplication();

        AppUsers idUserOld = oldStatus.getIdUser();
        storage.storeReference(StatusStorageKeys.APP_USER, ReferenceMode.OLD_REFERENCE, idUserOld);
        AppUsers idUserNew = status.getIdUser();

        if (idUserStatusNew != null) {
            idUserStatusNew = entityManager.getReference(idUserStatusNew.getClass(),
                    idUserStatusNew.getIdUserStatus());
            status.setIdUserStatus(idUserStatusNew);
            storage.storeReference(StatusStorageKeys.APP_STATUS, ReferenceMode.NEW_REFERENCE, idUserStatusNew);
        }
        if (idFaultApplicationNew != null) {
            idFaultApplicationNew = entityManager.getReference(idFaultApplicationNew.getClass(),
                    idFaultApplicationNew.getIdFaultAction());
            status.setIdFaultApplication(idFaultApplicationNew);
            storage.storeReference(StatusStorageKeys.FAULT_ACTIONS, ReferenceMode.NEW_REFERENCE,
                    idFaultApplicationNew);
        }
        if (idUserNew != null) {
            idUserNew = entityManager.getReference(idUserNew.getClass(), idUserNew.getIdApplicationUsers());
            status.setIdUser(idUserNew);
            storage.storeReference(StatusStorageKeys.APP_USER, ReferenceMode.NEW_REFERENCE, idUserNew);
        }
    }

    /**
     * Post edit.
     * 
     * @param status
     *            the status
     * @since 2/09/2014, 09:31:47 PM
     */
    @Override
    public void postEdit(AppUserXStatus status) {

        UserStatus idUserStatusOld, idUserStatusNew;
        FaultActions idFaultApplicationOld, idFaultApplicationNew;
        AppUsers idUserOld, idUserNew;

        if (storage.compareReferences(StatusStorageKeys.APP_STATUS, ReferenceMode.OLD_REFERENCE)) {
            idUserStatusOld = (UserStatus) storage.extractReference(StatusStorageKeys.APP_STATUS,
                    ReferenceMode.OLD_REFERENCE);
            idUserStatusOld.getAppUserXStatusCollection().remove(status);
            idUserStatusOld = entityManager.merge(idUserStatusOld);
        }
        if (storage.compareReferences(StatusStorageKeys.APP_STATUS, ReferenceMode.NEW_REFERENCE)) {
            idUserStatusNew = (UserStatus) storage.extractReference(StatusStorageKeys.APP_STATUS,
                    ReferenceMode.NEW_REFERENCE);
            idUserStatusNew.getAppUserXStatusCollection().add(status);
            idUserStatusNew = entityManager.merge(idUserStatusNew);
        }

        if (storage.compareReferences(StatusStorageKeys.FAULT_ACTIONS, ReferenceMode.OLD_REFERENCE)) {
            idFaultApplicationOld = (FaultActions) storage.extractReference(StatusStorageKeys.FAULT_ACTIONS,
                    ReferenceMode.OLD_REFERENCE);
            idFaultApplicationOld.getAppUserXStatusCollection().remove(status);
            idFaultApplicationOld = entityManager.merge(idFaultApplicationOld);
        }
        if (storage.compareReferences(StatusStorageKeys.FAULT_ACTIONS, ReferenceMode.NEW_REFERENCE)) {
            idFaultApplicationNew = (FaultActions) storage.extractReference(StatusStorageKeys.FAULT_ACTIONS,
                    ReferenceMode.NEW_REFERENCE);
            idFaultApplicationNew.getAppUserXStatusCollection().add(status);
            idFaultApplicationNew = entityManager.merge(idFaultApplicationNew);
        }
        if (storage.compareReferences(StatusStorageKeys.APP_USER, ReferenceMode.OLD_REFERENCE)) {
            idUserOld = (AppUsers) storage.extractReference(StatusStorageKeys.APP_USER,
                    ReferenceMode.OLD_REFERENCE);
            idUserOld.getAppUserXStatusCollection().remove(status);
            idUserOld = entityManager.merge(idUserOld);
        }
        if (storage.compareReferences(StatusStorageKeys.APP_USER, ReferenceMode.NEW_REFERENCE)) {
            idUserNew = (AppUsers) storage.extractReference(StatusStorageKeys.APP_USER,
                    ReferenceMode.NEW_REFERENCE);
            idUserNew.getAppUserXStatusCollection().add(status);
            idUserNew = entityManager.merge(idUserNew);
        }
        storage.clean();
    }

    /**
     * Delete by id.
     * 
     * @param id
     *            the id
     * @return the app user x status
     * @throws NonexistentEntityException
     *             the nonexistent entity exception
     * @throws IllegalOrphanException
     *             the illegal orphan exception
     * @since 2/09/2014, 09:31:47 PM
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public AppUserXStatus deleteById(Serializable id) throws NonexistentEntityException, IllegalOrphanException {
        AppUserXStatus deleted = preDelete((Number) id);
        entityManager.remove(deleted);
        entityManager.flush();
        return deleted;
    }

    /**
     * Pre delete.
     * 
     * @param id
     *            the id
     * @return the app user x status
     * @throws NonexistentEntityException
     *             the nonexistent entity exception
     * @throws IllegalOrphanException
     *             the illegal orphan exception
     * @since 2/09/2014, 09:31:47 PM
     */
    @Override
    public AppUserXStatus preDelete(Number id) throws NonexistentEntityException, IllegalOrphanException {
        AppUserXStatus appUserXStatus;
        try {
            appUserXStatus = entityManager.getReference(AppUserXStatus.class, id);
            appUserXStatus.getIdUserXStatus();
        } catch (EntityNotFoundException enfe) {
            throw new NonexistentEntityException("The appUserXStatus with id " + id + " no longer exists.", enfe);
        }
        UserStatus idUserStatus = appUserXStatus.getIdUserStatus();
        if (idUserStatus != null) {
            idUserStatus.getAppUserXStatusCollection().remove(appUserXStatus);
            idUserStatus = entityManager.merge(idUserStatus);
        }
        FaultActions idFaultApplication = appUserXStatus.getIdFaultApplication();
        if (idFaultApplication != null) {
            idFaultApplication.getAppUserXStatusCollection().remove(appUserXStatus);
            idFaultApplication = entityManager.merge(idFaultApplication);
        }
        AppUsers idUser = appUserXStatus.getIdUser();
        if (idUser != null) {
            idUser.getAppUserXStatusCollection().remove(appUserXStatus);
            idUser = entityManager.merge(idUser);
        }
        return appUserXStatus;
    }

    /**
     * net.navasoft.madcoin.backend.model.controller.impl Enum enum
     * StatusStorageKeys. Description:
     * 
     * @author Juan Diego Navarre Gonzalez - (${authorMail})
     * @version 1.0
     * @since 2/09/2014 09:31:47 PM
     */
    private enum StatusStorageKeys implements StorageKeys {

        /**
         * app status.
         * 
         * @since 2/09/2014, 09:31:47 PM
         */
        APP_STATUS("status"),

        /**
         * fault actions.
         * 
         * @since 2/09/2014, 09:31:47 PM
         */
        FAULT_ACTIONS("fault"),

        /**
         * app user.
         * 
         * @since 2/09/2014, 09:31:47 PM
         */
        APP_USER("user");

        /**
         * final key.
         * 
         * @since 2/09/2014, 09:31:47 PM
         */
        private String finalKey;

        /**
         * Instantiates a new status storage keys.
         * 
         * @param key
         *            the key
         * @since 2/09/2014, 09:31:47 PM
         */
        private StatusStorageKeys(String key) {
            finalKey = key;
        }

        /**
         * Gets the key.
         * 
         * @return the key
         * @since 2/09/2014, 09:31:47 PM
         */
        @Override
        public String getKey() {
            return finalKey;
        }

    }

    /**
     * Pre delete.
     * 
     * @param toDelete
     *            the to delete
     * @return the app user x status
     * @throws NonexistentEntityException
     *             the nonexistent entity exception
     * @throws IllegalOrphanException
     *             the illegal orphan exception
     * @since 2/09/2014, 09:31:47 PM
     */
    @Override
    public AppUserXStatus preDelete(ComplexId toDelete) throws NonexistentEntityException, IllegalOrphanException {
        throw new NotImplementedException();
    }

    /**
     * Gets the all.
     * 
     * @return the all
     * @since 2/09/2014, 09:31:47 PM
     */
    @Override
    public List<AppUserXStatus> getAll() {
        return super.getAllbyQuery(entityManager, AppUserXStatus.class);
    }

    /**
     * Gets the by logical id.
     * 
     * @param idEntity
     *            the id entity
     * @return the by logical id
     * @since 2/09/2014, 09:31:47 PM
     */
    @Override
    public AppUserXStatus getByLogicalId(Serializable idEntity) {
        TypedQuery<AppUserXStatus> query = entityManager.createNamedQuery("AppUserXStatus.findByIdUser",
                AppUserXStatus.class);
        query.setParameter("idAppUser", ((Long) idEntity));
        return query.getSingleResult();
    }

    /**
     * Delete all.
     * 
     * @throws NonexistentEntityException
     *             the nonexistent entity exception
     * @throws IllegalOrphanException
     *             the illegal orphan exception
     * @since 2/09/2014, 09:31:47 PM
     */
    @Override
    public void deleteAll() throws NonexistentEntityException, IllegalOrphanException {
        for (AppUserXStatus status : getAll()) {
            deleteById(status.getNormalizedId());
        }
    }

    /**
     * Count.
     * 
     * @return the int
     * @since 2/09/2014, 09:31:47 PM
     */
    @Override
    public int count() {
        return super.getQuantity(entityManager, AppUserXStatus.class);
    }

}