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.ArrayList; import java.util.Collection; 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.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.NormalizedEntity; import net.navasoft.madcoin.backend.model.entities.ReferenceMode; import net.navasoft.madcoin.backend.model.entities.impl.Administrators; import net.navasoft.madcoin.backend.model.entities.impl.BusinessLines; import net.navasoft.madcoin.backend.model.entities.impl.ServiceCategories; 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 * BusinessLinesDataAccess. Description: * * @author Juan Diego Navarre Gonzalez - (${authorMail}) * @version 1.0 * @since 2/09/2014 09:31:41 PM */ @Repository("BusinessDAO") @Scope(BeanDefinition.SCOPE_SINGLETON) public class BusinessLinesDataAccess extends JPAHelper<BusinessLines> implements IDataAccess<BusinessLines> { /** * entity manager. * * @since 2/09/2014, 09:31:41 PM */ @PersistenceContext(unitName = "ClickNDone-PU") private EntityManager entityManager; /** * storage. * * @since 2/09/2014, 09:31:41 PM */ private Storage storage; /** * Instantiates a new business lines data access. * * @since 2/09/2014, 09:31:41 PM */ public BusinessLinesDataAccess() { storage = StorageFactory.buildStorage(); } /** * Gets the all. * * @return the all * @since 2/09/2014, 09:31:41 PM */ @Override public List<BusinessLines> getAll() { TypedQuery<BusinessLines> query = entityManager.createNamedQuery("BusinessLines.findAll", BusinessLines.class); return query.getResultList(); } /** * Gets the by logical id. * * @param idEntity * the id entity * @return the by logical id * @since 2/09/2014, 09:31:41 PM */ @Override public BusinessLines getByLogicalId(Serializable idEntity) { if (idEntity instanceof String) { TypedQuery<BusinessLines> query = entityManager.createNamedQuery("BusinessLines.findByLobPrefix", BusinessLines.class); query.setParameter("lobPrefix", (String) idEntity); return query.getSingleResult(); } else { TypedQuery<BusinessLines> query = entityManager.createNamedQuery( BusinessLines.class.getSimpleName() + "." + NormalizedEntity.NAMED_QUERY_ID, BusinessLines.class); query.setParameter("idbusinessLine", (Integer) idEntity); return query.getSingleResult(); } } /** * Delete by id. * * @param idEntity * the id entity * @return the serializable * @throws NonexistentEntityException * the nonexistent entity exception * @throws IllegalOrphanException * the illegal orphan exception * @since 2/09/2014, 09:31:41 PM */ @Override @Transactional(propagation = Propagation.REQUIRED) public Serializable deleteById(Serializable idEntity) throws NonexistentEntityException, IllegalOrphanException { BusinessLines businessLines = preDelete((Number) idEntity); entityManager.remove(businessLines); return businessLines; } /** * Creates the. * * @param businessLines * the business lines * @return the business lines * @since 2/09/2014, 09:31:41 PM */ @Override @Transactional(propagation = Propagation.REQUIRED) public BusinessLines create(BusinessLines businessLines) { try { preConstruct(businessLines); entityManager.persist(businessLines); businessLines = entityManager.createNamedQuery(businessLines.getNamedQueryId(), BusinessLines.class) .setParameter("idbusinessLine", businessLines.getNormalizedId()).getSingleResult(); postConstruct(businessLines); return businessLines; } catch (PersistenceException e) { throw e; } } /** * Delete all. * * @throws NonexistentEntityException * the nonexistent entity exception * @throws IllegalOrphanException * the illegal orphan exception * @since 2/09/2014, 09:31:41 PM */ @Override public void deleteAll() throws NonexistentEntityException, IllegalOrphanException { for (BusinessLines lobs : getAll()) { deleteById(lobs.getNormalizedId()); } } /** * Pre construct. * * @param toInsert * the to insert * @since 2/09/2014, 09:31:41 PM */ @Override public void preConstruct(BusinessLines toInsert) { if (toInsert.getServiceCategoriesCollection() == null) { toInsert.setServiceCategoriesCollection(new ArrayList<ServiceCategories>()); } Administrators administrators = toInsert.getAdministrators(); if (administrators != null) { administrators = entityManager.getReference(administrators.getClass(), administrators.getAdministratorsPK()); toInsert.setAdministrators(administrators); storage.storeReference(BusinessLinesStorageKeys.ADMINISTRATOR_UPDATED, ReferenceMode.NEW_REFERENCE, administrators); } Administrators administrators1 = toInsert.getAdministrators1(); if (administrators1 != null) { administrators1 = entityManager.getReference(administrators1.getClass(), administrators1.getAdministratorsPK()); toInsert.setAdministrators1(administrators1); storage.storeReference(BusinessLinesStorageKeys.ADMINISTRATOR_CREATED, ReferenceMode.NEW_REFERENCE, administrators1); } Collection<ServiceCategories> newCategories = new ArrayList<ServiceCategories>(); for (ServiceCategories newCategory : toInsert.getServiceCategoriesCollection()) { newCategory = entityManager.getReference(newCategory.getClass(), newCategory.getIdServiceCategory()); newCategories.add(newCategory); } toInsert.setServiceCategoriesCollection(newCategories); storage.storeReference(BusinessLinesStorageKeys.CATEGORIES, ReferenceMode.NEW_REFERENCE, newCategories); } /** * Post construct. * * @param inserted * the inserted * @since 2/09/2014, 09:31:41 PM */ @Override public void postConstruct(BusinessLines inserted) { Administrators administrators; Administrators administrators1; if (storage.validateReference(BusinessLinesStorageKeys.ADMINISTRATOR_UPDATED, ReferenceMode.NEW_REFERENCE)) { administrators = (Administrators) storage .extractReference(BusinessLinesStorageKeys.ADMINISTRATOR_UPDATED, ReferenceMode.NEW_REFERENCE); administrators.getBusinessLinesCollection().add(inserted); administrators = entityManager.merge(administrators); } if (storage.validateReference(BusinessLinesStorageKeys.ADMINISTRATOR_CREATED, ReferenceMode.NEW_REFERENCE)) { administrators1 = (Administrators) storage .extractReference(BusinessLinesStorageKeys.ADMINISTRATOR_CREATED, ReferenceMode.NEW_REFERENCE); administrators1.getBusinessLinesCollection().add(inserted); administrators1 = entityManager.merge(administrators1); } for (ServiceCategories newCategory : inserted.getServiceCategoriesCollection()) { BusinessLines oldBusinessLine = newCategory.getParentLob(); newCategory.setParentLob(inserted); newCategory = entityManager.merge(newCategory); if (oldBusinessLine != null) { oldBusinessLine.getServiceCategoriesCollection().remove(newCategory); oldBusinessLine = entityManager.merge(oldBusinessLine); } } } /** * Pre delete. * * @param toDelete * the to delete * @return the business lines * @throws NonexistentEntityException * the nonexistent entity exception * @throws IllegalOrphanException * the illegal orphan exception * @since 2/09/2014, 09:31:41 PM */ @Override public BusinessLines preDelete(Number toDelete) throws NonexistentEntityException, IllegalOrphanException { BusinessLines businessLines; try { businessLines = entityManager.getReference(BusinessLines.class, toDelete); businessLines.getIdbusinessLine(); } catch (EntityNotFoundException enfe) { throw new NonexistentEntityException("The businessLines with id " + toDelete + " no longer exists.", enfe); } List<String> illegalOrphanMessages = null; Collection<ServiceCategories> oldCategories = businessLines.getServiceCategoriesCollection(); for (ServiceCategories oldCategory : oldCategories) { if (illegalOrphanMessages == null) { illegalOrphanMessages = new ArrayList<String>(); } illegalOrphanMessages.add("This BusinessLines (" + businessLines + ") cannot be destroyed since the ServiceCategories " + oldCategory + " in its serviceCategoriesCollection field has a non-nullable parentLob field."); } if (illegalOrphanMessages != null) { throw new IllegalOrphanException(illegalOrphanMessages); } Administrators administrators = businessLines.getAdministrators(); if (administrators != null) { administrators.getBusinessLinesCollection().remove(businessLines); administrators = entityManager.merge(administrators); } Administrators administrators1 = businessLines.getAdministrators1(); if (administrators1 != null) { administrators1.getBusinessLinesCollection().remove(businessLines); administrators1 = entityManager.merge(administrators1); } return businessLines; } /** * Pre delete. * * @param toDelete * the to delete * @return the business lines * @throws NonexistentEntityException * the nonexistent entity exception * @throws IllegalOrphanException * the illegal orphan exception * @since 2/09/2014, 09:31:41 PM */ @Override public BusinessLines preDelete(ComplexId toDelete) throws NonexistentEntityException, IllegalOrphanException { throw new NotImplementedException(); } /** * Pre edit. * * @param toEdit * the to edit * @throws NonexistentEntityException * the nonexistent entity exception * @since 2/09/2014, 09:31:41 PM */ @Override public void preEdit(BusinessLines toEdit) throws NonexistentEntityException { try { BusinessLines existent = entityManager.find(BusinessLines.class, toEdit.getIdbusinessLine()); Administrators administratorsOld = existent.getAdministrators(); storage.storeReference(BusinessLinesStorageKeys.ADMINISTRATOR_UPDATED, ReferenceMode.OLD_REFERENCE, administratorsOld); Administrators administratorsNew = toEdit.getAdministrators(); Administrators administrators1Old = existent.getAdministrators1(); storage.storeReference(BusinessLinesStorageKeys.ADMINISTRATOR_CREATED, ReferenceMode.OLD_REFERENCE, administrators1Old); Administrators administrators1New = toEdit.getAdministrators1(); Collection<ServiceCategories> oldCategories = existent.getServiceCategoriesCollection(); storage.storeReference(BusinessLinesStorageKeys.CATEGORIES, ReferenceMode.OLD_REFERENCE, oldCategories); Collection<ServiceCategories> newCategories = toEdit.getServiceCategoriesCollection(); List<String> illegalOrphanMessages = null; for (ServiceCategories oldCategory : oldCategories) { if (!newCategories.contains(oldCategory)) { if (illegalOrphanMessages == null) { illegalOrphanMessages = new ArrayList<String>(); } illegalOrphanMessages.add("You must retain ServiceCategories " + oldCategory + " since its parentLob field is not nullable."); } } if (illegalOrphanMessages != null) { throw new IllegalOrphanException(illegalOrphanMessages); } if (administratorsNew != null) { administratorsNew = entityManager.getReference(administratorsNew.getClass(), administratorsNew.getAdministratorsPK()); toEdit.setAdministrators(administratorsNew); storage.storeReference(BusinessLinesStorageKeys.ADMINISTRATOR_UPDATED, ReferenceMode.NEW_REFERENCE, administratorsNew); } if (administrators1New != null) { administrators1New = entityManager.getReference(administrators1New.getClass(), administrators1New.getAdministratorsPK()); toEdit.setAdministrators1(administrators1New); storage.storeReference(BusinessLinesStorageKeys.ADMINISTRATOR_CREATED, ReferenceMode.NEW_REFERENCE, administratorsNew); } Collection<ServiceCategories> tempCategories = new ArrayList<ServiceCategories>(); for (ServiceCategories tempCategory : newCategories) { tempCategory = entityManager.getReference(tempCategory.getClass(), tempCategory.getIdServiceCategory()); tempCategories.add(tempCategory); } newCategories = tempCategories; toEdit.setServiceCategoriesCollection(newCategories); storage.storeReference(BusinessLinesStorageKeys.CATEGORIES, ReferenceMode.NEW_REFERENCE, newCategories); } catch (Exception ex) { String msg = ex.getLocalizedMessage(); if (msg == null || msg.length() == 0) { Integer id = toEdit.getIdbusinessLine(); if (findBySimpleId(entityManager, id, BusinessLines.class) == null) { throw new NonexistentEntityException("The businessLines with id " + id + " no longer exists."); } } } } /** * Post edit. * * @param businessLines * the business lines * @since 2/09/2014, 09:31:41 PM */ @SuppressWarnings("unchecked") @Override public void postEdit(BusinessLines businessLines) { Administrators administratorsOld; Administrators administratorsNew; Administrators administrators1Old; Administrators administrators1New; Collection<ServiceCategories> serviceCategoriesCollectionOld = (Collection<ServiceCategories>) storage .extractReferences(BusinessLinesStorageKeys.CATEGORIES, ReferenceMode.OLD_REFERENCE); Collection<ServiceCategories> serviceCategoriesCollectionNew = (Collection<ServiceCategories>) storage .extractReferences(BusinessLinesStorageKeys.CATEGORIES, ReferenceMode.NEW_REFERENCE); if (storage.compareReferences(BusinessLinesStorageKeys.ADMINISTRATOR_UPDATED, ReferenceMode.OLD_REFERENCE)) { administratorsOld = (Administrators) storage .extractReference(BusinessLinesStorageKeys.ADMINISTRATOR_UPDATED, ReferenceMode.OLD_REFERENCE); administratorsOld.getBusinessLinesCollection().remove(businessLines); administratorsOld = entityManager.merge(administratorsOld); } if (storage.compareReferences(BusinessLinesStorageKeys.ADMINISTRATOR_UPDATED, ReferenceMode.NEW_REFERENCE)) { administratorsNew = (Administrators) storage .extractReference(BusinessLinesStorageKeys.ADMINISTRATOR_UPDATED, ReferenceMode.NEW_REFERENCE); administratorsNew.getBusinessLinesCollection().add(businessLines); administratorsNew = entityManager.merge(administratorsNew); } if (storage.compareReferences(BusinessLinesStorageKeys.ADMINISTRATOR_CREATED, ReferenceMode.OLD_REFERENCE)) { administrators1Old = (Administrators) storage .extractReference(BusinessLinesStorageKeys.ADMINISTRATOR_CREATED, ReferenceMode.OLD_REFERENCE); administrators1Old.getBusinessLinesCollection().remove(businessLines); administrators1Old = entityManager.merge(administrators1Old); } if (storage.compareReferences(BusinessLinesStorageKeys.ADMINISTRATOR_CREATED, ReferenceMode.NEW_REFERENCE)) { administrators1New = (Administrators) storage .extractReference(BusinessLinesStorageKeys.ADMINISTRATOR_CREATED, ReferenceMode.NEW_REFERENCE); administrators1New.getBusinessLinesCollection().add(businessLines); administrators1New = entityManager.merge(administrators1New); } for (ServiceCategories tempNewCategory : serviceCategoriesCollectionNew) { if (!serviceCategoriesCollectionOld.contains(tempNewCategory)) { BusinessLines oldLOB = tempNewCategory.getParentLob(); tempNewCategory.setParentLob(businessLines); tempNewCategory = entityManager.merge(tempNewCategory); if (oldLOB != null && !oldLOB.equals(businessLines)) { oldLOB.getServiceCategoriesCollection().remove(tempNewCategory); oldLOB = entityManager.merge(oldLOB); } } } } /** * Edits the. * * @param updatedRecord * the updated record * @return the business lines * @throws NonexistentEntityException * the nonexistent entity exception * @throws Exception * the exception * @since 2/09/2014, 09:31:41 PM */ @Override public BusinessLines edit(BusinessLines updatedRecord) throws NonexistentEntityException, Exception { preEdit(updatedRecord); updatedRecord = entityManager.merge(updatedRecord); postEdit(updatedRecord); return updatedRecord; } /** * net.navasoft.madcoin.backend.model.controller.impl Enum enum * BusinessLinesStorageKeys. Description: * * @author Juan Diego Navarre Gonzalez - (${authorMail}) * @version 1.0 * @since 2/09/2014 09:31:41 PM */ private enum BusinessLinesStorageKeys implements StorageKeys { /** * administrator created. * * @since 2/09/2014, 09:31:41 PM */ ADMINISTRATOR_CREATED("administrators"), /** * administrator updated. * * @since 2/09/2014, 09:31:41 PM */ ADMINISTRATOR_UPDATED("administrators1"), /** * categories. * * @since 2/09/2014, 09:31:41 PM */ CATEGORIES("categories"); /** * real key. * * @since 2/09/2014, 09:31:41 PM */ private final String realKey; /** * Instantiates a new business lines storage keys. * * @param key * the key * @since 2/09/2014, 09:31:41 PM */ private BusinessLinesStorageKeys(final String key) { realKey = key; } /** * Gets the key. * * @return the key * @since 2/09/2014, 09:31:41 PM */ @Override public String getKey() { return realKey; } } /** * Count. * * @return the int * @since 2/09/2014, 09:31:41 PM */ @Override public int count() { return super.getQuantity(entityManager, BusinessLines.class); } }