Java tutorial
/******************************************************************************* * 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.PersistenceException; 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.ServiceProviders; import net.navasoft.madcoin.backend.model.entities.impl.WorkRequests; import net.navasoft.madcoin.backend.model.entities.impl.WorkRequestsXServiceProviders; import net.navasoft.madcoin.backend.model.entities.impl.WorkRequestsXServiceProvidersPK; 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 * AcceptingProviderDataAccess. Description: * * @author Juan Diego Navarre Gonzalez - (${authorMail}) * @version 1.0 * @since 2/09/2014 09:31:45 PM */ @Repository("providerOrdersDAO") @Scope(BeanDefinition.SCOPE_SINGLETON) public class AcceptingProviderDataAccess extends JPAHelper<WorkRequestsXServiceProviders> implements IDataAccess<WorkRequestsXServiceProviders> { /** * Instantiates a new accepting provider data access. * * @since 2/09/2014, 09:31:45 PM */ public AcceptingProviderDataAccess() { storage = StorageFactory.buildStorage(); } /** * entity manager. * * @since 2/09/2014, 09:31:45 PM */ @PersistenceContext(unitName = "ClickNDone-PU") private EntityManager entityManager; /** * storage. * * @since 2/09/2014, 09:31:45 PM */ private Storage storage; /** * Creates the. * * @param workRequestsXServiceProviders * the work requests x service providers * @return the work requests x service providers * @throws PreexistingEntityException * the preexisting entity exception * @since 2/09/2014, 09:31:45 PM */ @Override @Transactional(propagation = Propagation.REQUIRED) public WorkRequestsXServiceProviders create(WorkRequestsXServiceProviders workRequestsXServiceProviders) throws PreexistingEntityException { try { preConstruct(workRequestsXServiceProviders); entityManager.persist(workRequestsXServiceProviders); workRequestsXServiceProviders = getByLogicalId( workRequestsXServiceProviders.getWorkRequestsXServiceProvidersPK()); postConstruct(workRequestsXServiceProviders); storage.clean(); return workRequestsXServiceProviders; } catch (PersistenceException e) { throw new AlreadyOnSourceException(e.getCause().getCause().getMessage()); } } /** * Edits the. * * @param workRequestsXServiceProviders * the work requests x service providers * @return the work requests x service providers * @throws NonexistentEntityException * the nonexistent entity exception * @throws IllegalOrphanException * the illegal orphan exception * @since 2/09/2014, 09:31:45 PM */ @Transactional(propagation = Propagation.REQUIRED) @Override public WorkRequestsXServiceProviders edit(WorkRequestsXServiceProviders workRequestsXServiceProviders) throws NonexistentEntityException, IllegalOrphanException { preEdit(workRequestsXServiceProviders); workRequestsXServiceProviders = entityManager.merge(workRequestsXServiceProviders); entityManager.flush(); postEdit(workRequestsXServiceProviders); return workRequestsXServiceProviders; } /** * Pre construct. * * @param workRequestsXServiceProviders * the work requests x service providers * @since 2/09/2014, 09:31:45 PM */ @Override public void preConstruct(WorkRequestsXServiceProviders workRequestsXServiceProviders) { if (workRequestsXServiceProviders.getWorkRequestsXServiceProvidersPK() == null) { workRequestsXServiceProviders.setWorkRequestsXServiceProvidersPK(new WorkRequestsXServiceProvidersPK()); } workRequestsXServiceProviders.getWorkRequestsXServiceProvidersPK().setIdServiceProvider( workRequestsXServiceProviders.getServiceProviders().getServiceProvidersPK().getIdServiceProvider()); workRequestsXServiceProviders.getWorkRequestsXServiceProvidersPK().setNameServiceProvider( workRequestsXServiceProviders.getServiceProviders().getServiceProvidersPK().getAppUsername()); workRequestsXServiceProviders.getWorkRequestsXServiceProvidersPK() .setCategoryId(workRequestsXServiceProviders.getWorkRequests().getWorkRequestsPK().getCategoryId()); workRequestsXServiceProviders.getWorkRequestsXServiceProvidersPK().setIdWorkRequest( workRequestsXServiceProviders.getWorkRequests().getWorkRequestsPK().getIdWorkRequests()); try { ServiceProviders serviceProviders = workRequestsXServiceProviders.getServiceProviders(); if (serviceProviders != null) { serviceProviders = entityManager.getReference(serviceProviders.getClass(), serviceProviders.getServiceProvidersPK()); workRequestsXServiceProviders.setServiceProviders(serviceProviders); storage.storeReference(AcceptingProviderStorageKeys.PROVIDER, ReferenceMode.NEW_REFERENCE, serviceProviders); } WorkRequests workRequests = workRequestsXServiceProviders.getWorkRequests(); if (workRequests != null) { workRequests = entityManager.getReference(workRequests.getClass(), workRequests.getWorkRequestsPK()); workRequestsXServiceProviders.setWorkRequests(workRequests); storage.storeReference(AcceptingProviderStorageKeys.ORDER, ReferenceMode.NEW_REFERENCE, workRequests); } } catch (Exception ex) { if (findByComplexId(entityManager, workRequestsXServiceProviders.getWorkRequestsXServiceProvidersPK(), WorkRequestsXServiceProviders.class) != null) { throw new AlreadyOnSourceException("WorkRequestsXServiceProviders " + workRequestsXServiceProviders.toString() + " already exists."); } throw ex; } } /** * Post construct. * * @param inserted * the inserted * @since 2/09/2014, 09:31:45 PM */ @Override public void postConstruct(WorkRequestsXServiceProviders inserted) { ServiceProviders serviceProviders; WorkRequests workRequests; if (storage.validateReference(AcceptingProviderStorageKeys.PROVIDER, ReferenceMode.NEW_REFERENCE)) { serviceProviders = (ServiceProviders) storage.extractReference(AcceptingProviderStorageKeys.PROVIDER, ReferenceMode.NEW_REFERENCE); serviceProviders.getWorkRequestsXServiceProvidersCollection().add(inserted); serviceProviders = entityManager.merge(serviceProviders); } if (storage.validateReference(AcceptingProviderStorageKeys.ORDER, ReferenceMode.NEW_REFERENCE)) { workRequests = (WorkRequests) storage.extractReference(AcceptingProviderStorageKeys.ORDER, ReferenceMode.NEW_REFERENCE); workRequests.getWorkRequestsXServiceProvidersCollection().add(inserted); workRequests = entityManager.merge(workRequests); } storage.clean(); } /** * Pre delete. * * @param toDelete * the to delete * @return the work requests x service providers * @throws NonexistentEntityException * the nonexistent entity exception * @throws IllegalOrphanException * the illegal orphan exception * @since 2/09/2014, 09:31:45 PM */ @Override public WorkRequestsXServiceProviders preDelete(Number toDelete) throws NonexistentEntityException, IllegalOrphanException { throw new NotImplementedException(); } /** * Pre delete. * * @param toDelete * the to delete * @return the work requests x service providers * @throws NonexistentEntityException * the nonexistent entity exception * @throws IllegalOrphanException * the illegal orphan exception * @since 2/09/2014, 09:31:45 PM */ @Override public WorkRequestsXServiceProviders preDelete(ComplexId toDelete) throws NonexistentEntityException, IllegalOrphanException { WorkRequestsXServiceProviders workRequestsXServiceProviders; try { workRequestsXServiceProviders = entityManager.getReference(WorkRequestsXServiceProviders.class, toDelete); workRequestsXServiceProviders.getWorkRequestsXServiceProvidersPK(); } catch (EntityNotFoundException enfe) { throw new NonexistentEntityException( "The workRequestsXServiceProviders with id " + toDelete.resume() + " no longer exists.", enfe); } ServiceProviders serviceProviders = workRequestsXServiceProviders.getServiceProviders(); if (serviceProviders != null) { serviceProviders.getWorkRequestsXServiceProvidersCollection().remove(workRequestsXServiceProviders); serviceProviders = entityManager.merge(serviceProviders); } WorkRequests workRequests = workRequestsXServiceProviders.getWorkRequests(); if (workRequests != null) { workRequests.getWorkRequestsXServiceProvidersCollection().remove(workRequestsXServiceProviders); workRequests = entityManager.merge(workRequests); } return workRequestsXServiceProviders; } /** * Pre edit. * * @param workRequestsXServiceProviders * the work requests x service providers * @throws NonexistentEntityException * the nonexistent entity exception * @throws IllegalOrphanException * the illegal orphan exception * @since 2/09/2014, 09:31:45 PM */ @Override public void preEdit(WorkRequestsXServiceProviders workRequestsXServiceProviders) throws NonexistentEntityException, IllegalOrphanException { workRequestsXServiceProviders.getWorkRequestsXServiceProvidersPK().setIdServiceProvider( workRequestsXServiceProviders.getServiceProviders().getServiceProvidersPK().getIdServiceProvider()); workRequestsXServiceProviders.getWorkRequestsXServiceProvidersPK().setNameServiceProvider( workRequestsXServiceProviders.getServiceProviders().getServiceProvidersPK().getAppUsername()); workRequestsXServiceProviders.getWorkRequestsXServiceProvidersPK() .setCategoryId(workRequestsXServiceProviders.getWorkRequests().getWorkRequestsPK().getCategoryId()); workRequestsXServiceProviders.getWorkRequestsXServiceProvidersPK().setIdWorkRequest( workRequestsXServiceProviders.getWorkRequests().getWorkRequestsPK().getIdWorkRequests()); try { WorkRequestsXServiceProviders persistentWorkRequestsXServiceProviders = entityManager.find( WorkRequestsXServiceProviders.class, workRequestsXServiceProviders.getWorkRequestsXServiceProvidersPK()); ServiceProviders serviceProvidersOld = persistentWorkRequestsXServiceProviders.getServiceProviders(); storage.storeReference(AcceptingProviderStorageKeys.PROVIDER, ReferenceMode.NEW_REFERENCE, serviceProvidersOld); ServiceProviders serviceProvidersNew = workRequestsXServiceProviders.getServiceProviders(); WorkRequests workRequestsOld = persistentWorkRequestsXServiceProviders.getWorkRequests(); storage.storeReference(AcceptingProviderStorageKeys.ORDER, ReferenceMode.OLD_REFERENCE, workRequestsOld); WorkRequests workRequestsNew = workRequestsXServiceProviders.getWorkRequests(); if (serviceProvidersNew != null) { serviceProvidersNew = entityManager.getReference(serviceProvidersNew.getClass(), serviceProvidersNew.getServiceProvidersPK()); workRequestsXServiceProviders.setServiceProviders(serviceProvidersNew); storage.storeReference(AcceptingProviderStorageKeys.PROVIDER, ReferenceMode.NEW_REFERENCE, serviceProvidersNew); } if (workRequestsNew != null) { workRequestsNew = entityManager.getReference(workRequestsNew.getClass(), workRequestsNew.getWorkRequestsPK()); workRequestsXServiceProviders.setWorkRequests(workRequestsNew); storage.storeReference(AcceptingProviderStorageKeys.ORDER, ReferenceMode.NEW_REFERENCE, workRequestsNew); } } catch (Exception ex) { String msg = ex.getLocalizedMessage(); if (msg == null || msg.length() == 0) { WorkRequestsXServiceProvidersPK id = workRequestsXServiceProviders .getWorkRequestsXServiceProvidersPK(); if (findByComplexId(entityManager, id, WorkRequestsXServiceProviders.class) == null) { throw new NonexistentEntityException( "The workRequestsXServiceProviders with id " + id + " no longer exists."); } } throw ex; } } /** * Post edit. * * @param workRequestsXServiceProviders * the work requests x service providers * @since 2/09/2014, 09:31:45 PM */ @Override public void postEdit(WorkRequestsXServiceProviders workRequestsXServiceProviders) { ServiceProviders serviceProvidersOld, serviceProvidersNew; WorkRequests workRequestsOld, workRequestsNew; if (storage.compareReferences(AcceptingProviderStorageKeys.PROVIDER, ReferenceMode.OLD_REFERENCE)) { serviceProvidersOld = (ServiceProviders) storage.extractReference(AcceptingProviderStorageKeys.PROVIDER, ReferenceMode.OLD_REFERENCE); serviceProvidersOld.getWorkRequestsXServiceProvidersCollection().remove(workRequestsXServiceProviders); serviceProvidersOld = entityManager.merge(serviceProvidersOld); } if (storage.compareReferences(AcceptingProviderStorageKeys.PROVIDER, ReferenceMode.NEW_REFERENCE)) { serviceProvidersNew = (ServiceProviders) storage.extractReference(AcceptingProviderStorageKeys.PROVIDER, ReferenceMode.NEW_REFERENCE); serviceProvidersNew.getWorkRequestsXServiceProvidersCollection().add(workRequestsXServiceProviders); serviceProvidersNew = entityManager.merge(serviceProvidersNew); } if (storage.compareReferences(AcceptingProviderStorageKeys.ORDER, ReferenceMode.OLD_REFERENCE)) { workRequestsOld = (WorkRequests) storage.extractReference(AcceptingProviderStorageKeys.ORDER, ReferenceMode.OLD_REFERENCE); workRequestsOld.getWorkRequestsXServiceProvidersCollection().remove(workRequestsXServiceProviders); workRequestsOld = entityManager.merge(workRequestsOld); } if (storage.compareReferences(AcceptingProviderStorageKeys.ORDER, ReferenceMode.NEW_REFERENCE)) { workRequestsNew = (WorkRequests) storage.extractReference(AcceptingProviderStorageKeys.ORDER, ReferenceMode.NEW_REFERENCE); workRequestsNew.getWorkRequestsXServiceProvidersCollection().add(workRequestsXServiceProviders); workRequestsNew = entityManager.merge(workRequestsNew); } } /** * Gets the all. * * @return the all * @since 2/09/2014, 09:31:45 PM */ @Override public List<WorkRequestsXServiceProviders> getAll() { return getAllbyQuery(entityManager, WorkRequestsXServiceProviders.class); } /** * Gets the by logical id. * * @param idEntity * the id entity * @return the by logical id * @since 2/09/2014, 09:31:45 PM */ @Override public WorkRequestsXServiceProviders getByLogicalId(Serializable idEntity) { TypedQuery<WorkRequestsXServiceProviders> query = entityManager.createNamedQuery( "WorkRequestsXServiceProviders." + NormalizedEntity.NAMED_QUERY_ID, WorkRequestsXServiceProviders.class); query.setParameter("idWorkRequest", ((WorkRequestsXServiceProvidersPK) idEntity).getIdWorkRequest()); query.setParameter("nameServiceProvider", ((WorkRequestsXServiceProvidersPK) idEntity).getNameServiceProvider()); return query.getSingleResult(); } /** * Delete by id. * * @param idEntity * the id entity * @return the work requests x service providers * @throws NonexistentEntityException * the nonexistent entity exception * @throws IllegalOrphanException * the illegal orphan exception * @since 2/09/2014, 09:31:45 PM */ @Override @Transactional(propagation = Propagation.REQUIRED) public WorkRequestsXServiceProviders deleteById(Serializable idEntity) throws NonexistentEntityException, IllegalOrphanException { WorkRequestsXServiceProviders deleted = preDelete((WorkRequestsXServiceProvidersPK) idEntity); entityManager.remove(deleted); entityManager.flush(); return deleted; } /** * Delete all. * * @throws NonexistentEntityException * the nonexistent entity exception * @throws IllegalOrphanException * the illegal orphan exception * @since 2/09/2014, 09:31:45 PM */ @Override public void deleteAll() throws NonexistentEntityException, IllegalOrphanException { for (WorkRequestsXServiceProviders cross : getAll()) { deleteById(cross.getNormalizedId()); } } /** * net.navasoft.madcoin.backend.model.controller.impl Enum enum * AcceptingProviderStorageKeys. Description: * * @author Juan Diego Navarre Gonzalez - (${authorMail}) * @version 1.0 * @since 2/09/2014 09:31:45 PM */ private enum AcceptingProviderStorageKeys implements StorageKeys { /** * provider. * * @since 2/09/2014, 09:31:45 PM */ PROVIDER("provider"), /** * order. * * @since 2/09/2014, 09:31:45 PM */ ORDER("order"); /** * final key. * * @since 2/09/2014, 09:31:45 PM */ private String finalKey; /** * Instantiates a new accepting provider storage keys. * * @param key * the key * @since 2/09/2014, 09:31:45 PM */ private AcceptingProviderStorageKeys(String key) { finalKey = key; } /** * Gets the key. * * @return the key * @since 2/09/2014, 09:31:45 PM */ @Override public String getKey() { return finalKey; } } /** * Count. * * @return the int * @since 2/09/2014, 09:31:45 PM */ @Override public int count() { return super.getQuantity(entityManager, WorkRequestsXServiceProviders.class); } }