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

Java tutorial

Introduction

Here is the source code for net.navasoft.madcoin.backend.model.controller.impl.RequestingUsersDataAccess.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.AlreadyOnSourceException;
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.exceptions.PreexistingEntityException;
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.NormalizedEntity;
import net.navasoft.madcoin.backend.model.entities.ReferenceMode;
import net.navasoft.madcoin.backend.model.entities.impl.EndUsers;
import net.navasoft.madcoin.backend.model.entities.impl.WorkRequests;
import net.navasoft.madcoin.backend.model.entities.impl.WorkRequestsXEndUsers;
import net.navasoft.madcoin.backend.model.entities.impl.WorkRequestsXEndUsersPK;
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
 * RequestingUsersDataAccess. Description:
 * 
 * @author Juan Diego Navarre Gonzalez - (${authorMail})
 * @version 1.0
 * @since 2/09/2014 09:31:38 PM
 */
@Repository("usersOrdersDAO")
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class RequestingUsersDataAccess extends JPAHelper<WorkRequestsXEndUsers>
        implements IDataAccess<WorkRequestsXEndUsers> {

    /**
     * Instantiates a new requesting users data access.
     * 
     * @since 2/09/2014, 09:31:38 PM
     */
    public RequestingUsersDataAccess() {
        storage = StorageFactory.buildStorage();
    }

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

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

    /**
     * Pre construct.
     * 
     * @param entity
     *            the entity
     * @since 2/09/2014, 09:31:38 PM
     */
    @Override
    public void preConstruct(WorkRequestsXEndUsers entity) {
        if (entity.getWorkRequestsXEndUsersPK() == null) {
            entity.setWorkRequestsXEndUsersPK(new WorkRequestsXEndUsersPK());
        }
        entity.getWorkRequestsXEndUsersPK()
                .setIdWorkRequest(entity.getWorkRequests().getWorkRequestsPK().getIdWorkRequests());
        entity.getWorkRequestsXEndUsersPK()
                .setCategoryId(entity.getWorkRequests().getWorkRequestsPK().getCategoryId());
        entity.getWorkRequestsXEndUsersPK().setIdEndUser(entity.getEndUsers().getEndUsersPK().getIdEndUser());
        entity.getWorkRequestsXEndUsersPK().setNameEndUser(entity.getEndUsers().getEndUsersPK().getAppUsername());
        try {
            EndUsers endUsers = entity.getEndUsers();
            if (endUsers != null) {
                endUsers = entityManager.getReference(endUsers.getClass(), endUsers.getEndUsersPK());
                entity.setEndUsers(endUsers);
                storage.storeReference(RequestingUserStorageKeys.USER, ReferenceMode.NEW_REFERENCE, endUsers);
            }
            WorkRequests workRequests = entity.getWorkRequests();
            if (workRequests != null) {
                workRequests = entityManager.getReference(workRequests.getClass(),
                        workRequests.getWorkRequestsPK());
                entity.setWorkRequests(workRequests);
                storage.storeReference(RequestingUserStorageKeys.ORDER, ReferenceMode.NEW_REFERENCE, workRequests);
            }
        } catch (Exception ex) {
            if (findByComplexId(entityManager, entity.getWorkRequestsXEndUsersPK(),
                    WorkRequestsXEndUsers.class) != null) {
                throw new AlreadyOnSourceException(
                        "WorkRequestsXEndUsers " + entity.toString() + " already exists.");
            }
            throw ex;
        }
    }

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

        EndUsers endUsers;
        WorkRequests workRequests;

        if (storage.validateReference(RequestingUserStorageKeys.USER, ReferenceMode.NEW_REFERENCE)) {
            endUsers = (EndUsers) storage.extractReference(RequestingUserStorageKeys.USER,
                    ReferenceMode.NEW_REFERENCE);
            endUsers.getWorkRequestsXEndUsersCollection().add(entity);
            endUsers = entityManager.merge(endUsers);
        }
        if (storage.validateReference(RequestingUserStorageKeys.ORDER, ReferenceMode.NEW_REFERENCE)) {
            workRequests = (WorkRequests) storage.extractReference(RequestingUserStorageKeys.ORDER,
                    ReferenceMode.NEW_REFERENCE);
            workRequests.getWorkRequestsXEndUsersCollection().add(entity);
            workRequests = entityManager.merge(workRequests);
        }
    }

    /**
     * Creates the.
     * 
     * @param workRequestsXEndUsers
     *            the work requests x end users
     * @return the work requests x end users
     * @throws PreexistingEntityException
     *             the preexisting entity exception
     * @since 2/09/2014, 09:31:38 PM
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public WorkRequestsXEndUsers create(WorkRequestsXEndUsers workRequestsXEndUsers)
            throws PreexistingEntityException {
        preConstruct(workRequestsXEndUsers);
        entityManager.persist(workRequestsXEndUsers);
        entityManager.flush();
        workRequestsXEndUsers = getByLogicalId(workRequestsXEndUsers.getNormalizedId());
        postConstruct(workRequestsXEndUsers);
        storage.clean();
        return workRequestsXEndUsers;
    }

    /**
     * Pre edit.
     * 
     * @param workRequestsXEndUsers
     *            the work requests x end users
     * @throws NonexistentEntityException
     *             the nonexistent entity exception
     * @since 2/09/2014, 09:31:39 PM
     */
    @Override
    public void preEdit(WorkRequestsXEndUsers workRequestsXEndUsers) throws NonexistentEntityException {
        workRequestsXEndUsers.getWorkRequestsXEndUsersPK()
                .setIdWorkRequest(workRequestsXEndUsers.getWorkRequests().getWorkRequestsPK().getIdWorkRequests());
        workRequestsXEndUsers.getWorkRequestsXEndUsersPK()
                .setCategoryId(workRequestsXEndUsers.getWorkRequests().getWorkRequestsPK().getCategoryId());
        workRequestsXEndUsers.getWorkRequestsXEndUsersPK()
                .setIdEndUser(workRequestsXEndUsers.getEndUsers().getEndUsersPK().getIdEndUser());
        workRequestsXEndUsers.getWorkRequestsXEndUsersPK()
                .setNameEndUser(workRequestsXEndUsers.getEndUsers().getEndUsersPK().getAppUsername());
        try {
            WorkRequestsXEndUsers persistentWorkRequestsXEndUsers = entityManager.find(WorkRequestsXEndUsers.class,
                    workRequestsXEndUsers.getWorkRequestsXEndUsersPK());

            EndUsers endUsersOld = persistentWorkRequestsXEndUsers.getEndUsers();
            storage.storeReference(RequestingUserStorageKeys.USER, ReferenceMode.OLD_REFERENCE, endUsersOld);
            EndUsers endUsersNew = workRequestsXEndUsers.getEndUsers();

            WorkRequests workRequestsOld = persistentWorkRequestsXEndUsers.getWorkRequests();
            storage.storeReference(RequestingUserStorageKeys.ORDER, ReferenceMode.OLD_REFERENCE, workRequestsOld);
            WorkRequests workRequestsNew = workRequestsXEndUsers.getWorkRequests();

            if (endUsersNew != null) {
                endUsersNew = entityManager.getReference(endUsersNew.getClass(), endUsersNew.getEndUsersPK());
                workRequestsXEndUsers.setEndUsers(endUsersNew);
                storage.storeReference(RequestingUserStorageKeys.USER, ReferenceMode.NEW_REFERENCE, endUsersNew);
            }
            if (workRequestsNew != null) {
                workRequestsNew = entityManager.getReference(workRequestsNew.getClass(),
                        workRequestsNew.getWorkRequestsPK());
                workRequestsXEndUsers.setWorkRequests(workRequestsNew);
                storage.storeReference(RequestingUserStorageKeys.ORDER, ReferenceMode.NEW_REFERENCE,
                        workRequestsNew);
            }
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                WorkRequestsXEndUsersPK id = workRequestsXEndUsers.getWorkRequestsXEndUsersPK();
                if (findByComplexId(entityManager, id, WorkRequestsXEndUsers.class) == null) {
                    throw new NonexistentEntityException(
                            "The workRequestsXEndUsers with id " + id.resume() + " no longer exists.");
                }
            }
            throw ex;
        }

    }

    /**
     * Post edit.
     * 
     * @param workRequestsXEndUsers
     *            the work requests x end users
     * @since 2/09/2014, 09:31:39 PM
     */
    public void postEdit(WorkRequestsXEndUsers workRequestsXEndUsers) {

        EndUsers endUsersOld, endUsersNew;
        WorkRequests workRequestsOld, workRequestsNew;

        if (storage.compareReferences(RequestingUserStorageKeys.USER, ReferenceMode.OLD_REFERENCE)) {
            endUsersOld = (EndUsers) storage.extractReference(RequestingUserStorageKeys.USER,
                    ReferenceMode.OLD_REFERENCE);
            endUsersOld.getWorkRequestsXEndUsersCollection().remove(workRequestsXEndUsers);
            endUsersOld = entityManager.merge(endUsersOld);
        }

        if (storage.compareReferences(RequestingUserStorageKeys.USER, ReferenceMode.NEW_REFERENCE)) {
            endUsersNew = (EndUsers) storage.extractReference(RequestingUserStorageKeys.USER,
                    ReferenceMode.NEW_REFERENCE);
            endUsersNew.getWorkRequestsXEndUsersCollection().add(workRequestsXEndUsers);
            endUsersNew = entityManager.merge(endUsersNew);
        }

        if (storage.compareReferences(RequestingUserStorageKeys.ORDER, ReferenceMode.OLD_REFERENCE)) {
            workRequestsOld = (WorkRequests) storage.extractReference(RequestingUserStorageKeys.ORDER,
                    ReferenceMode.OLD_REFERENCE);
            workRequestsOld.getWorkRequestsXEndUsersCollection().remove(workRequestsXEndUsers);
            workRequestsOld = entityManager.merge(workRequestsOld);
        }
        if (storage.compareReferences(RequestingUserStorageKeys.ORDER, ReferenceMode.NEW_REFERENCE)) {
            workRequestsNew = (WorkRequests) storage.extractReference(RequestingUserStorageKeys.ORDER,
                    ReferenceMode.NEW_REFERENCE);
            workRequestsNew.getWorkRequestsXEndUsersCollection().add(workRequestsXEndUsers);
            workRequestsNew = entityManager.merge(workRequestsNew);
        }
        storage.clean();
    }

    /**
     * Edits the.
     * 
     * @param workRequestsXEndUsers
     *            the work requests x end users
     * @return the work requests x end users
     * @throws NonexistentEntityException
     *             the nonexistent entity exception
     * @throws Exception
     *             the exception
     * @since 2/09/2014, 09:31:39 PM
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public WorkRequestsXEndUsers edit(WorkRequestsXEndUsers workRequestsXEndUsers)
            throws NonexistentEntityException, Exception {
        preEdit(workRequestsXEndUsers);
        workRequestsXEndUsers = entityManager.merge(workRequestsXEndUsers);
        postEdit(workRequestsXEndUsers);
        storage.clean();
        return workRequestsXEndUsers;
    }

    /**
     * Delete by id.
     * 
     * @param id
     *            the id
     * @return the work requests x end users
     * @throws NonexistentEntityException
     *             the nonexistent entity exception
     * @since 2/09/2014, 09:31:39 PM
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public WorkRequestsXEndUsers deleteById(Serializable id) throws NonexistentEntityException {
        WorkRequestsXEndUsers deleted = preDelete((WorkRequestsXEndUsersPK) id);
        entityManager.remove(deleted);
        entityManager.flush();
        return deleted;
    }

    /**
     * Pre delete.
     * 
     * @param id
     *            the id
     * @return the work requests x end users
     * @throws NonexistentEntityException
     *             the nonexistent entity exception
     * @since 2/09/2014, 09:31:39 PM
     */
    @Override
    public WorkRequestsXEndUsers preDelete(ComplexId id) throws NonexistentEntityException {
        WorkRequestsXEndUsers workRequestsXEndUsers;
        try {
            workRequestsXEndUsers = entityManager.getReference(WorkRequestsXEndUsers.class, id);
            workRequestsXEndUsers.getWorkRequestsXEndUsersPK();
        } catch (EntityNotFoundException enfe) {
            throw new NonexistentEntityException("The workRequestsXEndUsers with id " + id + " no longer exists.",
                    enfe);
        }

        EndUsers endUsers = workRequestsXEndUsers.getEndUsers();
        if (endUsers != null) {
            endUsers.getWorkRequestsXEndUsersCollection().remove(workRequestsXEndUsers);
            endUsers = entityManager.merge(endUsers);
        }
        WorkRequests workRequests = workRequestsXEndUsers.getWorkRequests();
        if (workRequests != null) {
            workRequests.getWorkRequestsXEndUsersCollection().remove(workRequestsXEndUsers);
            workRequests = entityManager.merge(workRequests);
        }
        return workRequestsXEndUsers;
    }

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

        /**
         * user.
         * 
         * @since 2/09/2014, 09:31:39 PM
         */
        USER("user"),

        /**
         * order.
         * 
         * @since 2/09/2014, 09:31:39 PM
         */
        ORDER("order");

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

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

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

    /**
     * Pre delete.
     * 
     * @param toDelete
     *            the to delete
     * @return the work requests x end users
     * @throws NonexistentEntityException
     *             the nonexistent entity exception
     * @throws IllegalOrphanException
     *             the illegal orphan exception
     * @since 2/09/2014, 09:31:39 PM
     */
    @Override
    public WorkRequestsXEndUsers preDelete(Number toDelete)
            throws NonexistentEntityException, IllegalOrphanException {
        throw new NotImplementedException();
    }

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

    /**
     * Gets the by logical id.
     * 
     * @param idEntity
     *            the id entity
     * @return the by logical id
     * @since 2/09/2014, 09:31:39 PM
     */
    @Override
    public WorkRequestsXEndUsers getByLogicalId(Serializable idEntity) {
        TypedQuery<WorkRequestsXEndUsers> query;
        if (idEntity instanceof WorkRequestsXEndUsersPK) {
            query = entityManager.createNamedQuery("WorkRequestsXEndUsers." + NormalizedEntity.NAMED_QUERY_ID,
                    WorkRequestsXEndUsers.class);
            query.setParameter("nameEndUser", ((WorkRequestsXEndUsersPK) idEntity).getNameEndUser());
            query.setParameter("categoryId", ((WorkRequestsXEndUsersPK) idEntity).getCategoryId());
            query.setParameter("idWorkRequest", ((WorkRequestsXEndUsersPK) idEntity).getIdWorkRequest());
        } else {
            query = entityManager.createNamedQuery("WorkRequestsXEndUsers.findByNameEndUser",
                    WorkRequestsXEndUsers.class);
            query.setParameter("nameEndUser", ((WorkRequestsXEndUsersPK) idEntity).getNameEndUser());
        }

        return query.getSingleResult();
    }

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

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

}