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

Java tutorial

Introduction

Here is the source code for net.navasoft.madcoin.backend.model.controller.impl.ProvidersDAO.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.ArrayList;
import java.util.Collection;
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.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.AppUsers;
import net.navasoft.madcoin.backend.model.entities.impl.ProviderTypes;
import net.navasoft.madcoin.backend.model.entities.impl.ServiceProviders;
import net.navasoft.madcoin.backend.model.entities.impl.ServiceProvidersPK;
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 ProvidersDAO.
 * Description:
 * 
 * @author Juan Diego Navarre Gonzalez - (${authorMail})
 * @version 1.0
 * @since 2/09/2014 09:31:49 PM
 */
@Repository("dataAccessProvider")
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class ProvidersDAO extends JPAHelper<ServiceProviders> implements IDataAccess<ServiceProviders> {

    /**
     * Instantiates a new providers dao.
     * 
     * @since 2/09/2014, 09:31:49 PM
     */
    public ProvidersDAO() {
        storage = StorageFactory.buildStorage();
    }

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

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

    /**
     * Creates the.
     * 
     * @param serviceProviders
     *            the service providers
     * @return the service providers
     * @since 2/09/2014, 09:31:49 PM
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ServiceProviders create(ServiceProviders serviceProviders) {
        preConstruct(serviceProviders);
        entityManager.persist(serviceProviders);
        entityManager.flush();
        serviceProviders = findLast(entityManager, ServiceProviders.class, serviceProviders);
        postConstruct(serviceProviders);
        return serviceProviders;
    }

    /**
     * Edits the.
     * 
     * @param serviceProviders
     *            the service providers
     * @return the service providers
     * @throws NonexistentEntityException
     *             the nonexistent entity exception
     * @throws Exception
     *             the exception
     * @since 2/09/2014, 09:31:49 PM
     */
    public ServiceProviders edit(ServiceProviders serviceProviders) throws NonexistentEntityException, Exception {
        preEdit(serviceProviders);
        serviceProviders = entityManager.merge(serviceProviders);
        entityManager.flush();
        postEdit(serviceProviders);
        storage.clean();
        return serviceProviders;
    }

    /**
     * Delete by id.
     * 
     * @param id
     *            the id
     * @return the service providers
     * @throws NonexistentEntityException
     *             the nonexistent entity exception
     * @since 2/09/2014, 09:31:49 PM
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public ServiceProviders deleteById(Serializable id) throws NonexistentEntityException {
        ServiceProviders deleted = preDelete((ComplexId) id);
        entityManager.remove(deleted);
        entityManager.flush();
        return deleted;
    }

    /**
     * Pre delete.
     * 
     * @param id
     *            the id
     * @return the service providers
     * @throws NonexistentEntityException
     *             the nonexistent entity exception
     * @since 2/09/2014, 09:31:49 PM
     */
    public ServiceProviders preDelete(ComplexId id) throws NonexistentEntityException {
        ServiceProviders serviceProviders;
        try {
            serviceProviders = entityManager.getReference(ServiceProviders.class, id);
            serviceProviders.getServiceProvidersPK();
        } catch (EntityNotFoundException enfe) {
            throw new NonexistentEntityException("The serviceProviders with id " + id + " no longer exists.", enfe);
        }
        AppUsers appUsers = serviceProviders.getAppUsers();
        if (appUsers != null) {
            appUsers.getServiceProvidersCollection().remove(serviceProviders);
            appUsers = entityManager.merge(appUsers);
        }
        ProviderTypes providerRole = serviceProviders.getProviderRole();
        if (providerRole != null) {
            providerRole.getServiceProvidersCollection().remove(serviceProviders);
            providerRole = entityManager.merge(providerRole);
        }
        ServiceProviders serviceProvidersRel = serviceProviders.getServiceProviders();
        if (serviceProvidersRel != null) {
            serviceProvidersRel.getServiceProvidersCollection().remove(serviceProviders);
            serviceProvidersRel = entityManager.merge(serviceProvidersRel);
        }
        Administrators administrators = serviceProviders.getAdministrators();
        if (administrators != null) {
            administrators.getServiceProvidersCollection().remove(serviceProviders);
            administrators = entityManager.merge(administrators);
        }
        Administrators administrators1 = serviceProviders.getAdministrators1();
        if (administrators1 != null) {
            administrators1.getServiceProvidersCollection().remove(serviceProviders);
            administrators1 = entityManager.merge(administrators1);
        }
        Collection<ServiceProviders> serviceProvidersCollection = serviceProviders.getServiceProvidersCollection();
        for (ServiceProviders serviceProvidersCollectionServiceProviders : serviceProvidersCollection) {
            serviceProvidersCollectionServiceProviders.setServiceProviders(null);
            serviceProvidersCollectionServiceProviders = entityManager
                    .merge(serviceProvidersCollectionServiceProviders);
        }
        return serviceProviders;
    }

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

        /**
         * selves.
         * 
         * @since 2/09/2014, 09:31:49 PM
         */
        SELVES("self"),

        /**
         * super type.
         * 
         * @since 2/09/2014, 09:31:49 PM
         */
        SUPER_TYPE("appUsers"),

        /**
         * role.
         * 
         * @since 2/09/2014, 09:31:49 PM
         */
        ROLE("providerRole"),

        /**
         * updated.
         * 
         * @since 2/09/2014, 09:31:49 PM
         */
        UPDATED("serviceProvidersRel"),

        /**
         * updated admin.
         * 
         * @since 2/09/2014, 09:31:49 PM
         */
        UPDATED_ADMIN("administrators"),

        /**
         * authorized by.
         * 
         * @since 2/09/2014, 09:31:49 PM
         */
        AUTHORIZED_BY("administrators1");

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

        /**
         * Instantiates a new provider storage keys.
         * 
         * @param value
         *            the value
         * @since 2/09/2014, 09:31:49 PM
         */
        private ProviderStorageKeys(String value) {
            finalKey = value;
        }

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

    /**
     * Pre construct.
     * 
     * @param serviceProviders
     *            the service providers
     * @since 2/09/2014, 09:31:49 PM
     */
    @Override
    public void preConstruct(ServiceProviders serviceProviders) {
        if (serviceProviders.getServiceProvidersPK() == null) {
            serviceProviders.setServiceProvidersPK(new ServiceProvidersPK());
        }
        if (serviceProviders.getServiceProvidersCollection() == null) {
            serviceProviders.setServiceProvidersCollection(new ArrayList<ServiceProviders>());
        }
        serviceProviders.getServiceProvidersPK()
                .setIdServiceProvider(serviceProviders.getAppUsers().getIdApplicationUsers());
        try {
            AppUsers appUsers = serviceProviders.getAppUsers();
            if (appUsers != null) {
                appUsers = entityManager.getReference(appUsers.getClass(), appUsers.getIdApplicationUsers());
                serviceProviders.setAppUsers(appUsers);
                storage.storeReference(ProviderStorageKeys.SUPER_TYPE, ReferenceMode.NEW_REFERENCE, appUsers);
            }
            ProviderTypes providerRole = serviceProviders.getProviderRole();
            if (providerRole != null) {
                providerRole = entityManager.getReference(providerRole.getClass(),
                        providerRole.getIdProviderType());
                serviceProviders.setProviderRole(providerRole);
                storage.storeReference(ProviderStorageKeys.ROLE, ReferenceMode.NEW_REFERENCE, providerRole);
            }
            ServiceProviders serviceProvidersRel = serviceProviders.getServiceProviders();
            if (serviceProvidersRel != null) {
                serviceProvidersRel = entityManager.getReference(serviceProvidersRel.getClass(),
                        serviceProvidersRel.getServiceProvidersPK());
                serviceProviders.setServiceProviders(serviceProvidersRel);
                storage.storeReference(ProviderStorageKeys.UPDATED, ReferenceMode.NEW_REFERENCE,
                        serviceProvidersRel);
            }
            Administrators administrators = serviceProviders.getAdministrators();
            if (administrators != null) {
                administrators = entityManager.getReference(administrators.getClass(),
                        administrators.getAdministratorsPK());
                serviceProviders.setAdministrators(administrators);
                storage.storeReference(ProviderStorageKeys.UPDATED_ADMIN, ReferenceMode.NEW_REFERENCE,
                        administrators);
            }
            Administrators administrators1 = serviceProviders.getAdministrators1();
            if (administrators1 != null) {
                administrators1 = entityManager.getReference(administrators1.getClass(),
                        administrators1.getAdministratorsPK());
                serviceProviders.setAdministrators1(administrators1);
                storage.storeReference(ProviderStorageKeys.AUTHORIZED_BY, ReferenceMode.NEW_REFERENCE,
                        administrators1);
            }

            Collection<ServiceProviders> selves = new ArrayList<ServiceProviders>();
            for (ServiceProviders self : serviceProviders.getServiceProvidersCollection()) {
                self = entityManager.getReference(self.getClass(), self.getServiceProvidersPK());
                selves.add(self);
            }
            serviceProviders.setServiceProvidersCollection(selves);
            storage.storeReference(ProviderStorageKeys.SELVES, ReferenceMode.NEW_REFERENCE, selves);
        } catch (Exception ex) {
            if (findByComplexId(entityManager, serviceProviders.getServiceProvidersPK(),
                    ServiceProviders.class) != null) {
                throw new AlreadyOnSourceException("ServiceProviders " + serviceProviders + " already exists.");
            }
            throw ex;
        }
    }

    /**
     * Post construct.
     * 
     * @param serviceProviders
     *            the service providers
     * @since 2/09/2014, 09:31:49 PM
     */
    @Override
    public void postConstruct(ServiceProviders serviceProviders) {
        AppUsers appUsers;
        ProviderTypes providerRole;
        Administrators administrators, administrators1;
        ServiceProviders serviceProvidersRel;

        if (storage.validateReference(ProviderStorageKeys.SUPER_TYPE, ReferenceMode.NEW_REFERENCE)) {
            appUsers = (AppUsers) storage.extractReference(ProviderStorageKeys.SUPER_TYPE,
                    ReferenceMode.NEW_REFERENCE);
            appUsers.getServiceProvidersCollection().add(serviceProviders);
            appUsers = entityManager.merge(appUsers);
        }
        if (storage.validateReference(ProviderStorageKeys.ROLE, ReferenceMode.NEW_REFERENCE)) {
            providerRole = (ProviderTypes) storage.extractReference(ProviderStorageKeys.ROLE,
                    ReferenceMode.NEW_REFERENCE);
            providerRole.getServiceProvidersCollection().add(serviceProviders);
            providerRole = entityManager.merge(providerRole);
        }

        if (storage.validateReference(ProviderStorageKeys.UPDATED, ReferenceMode.NEW_REFERENCE)) {
            serviceProvidersRel = (ServiceProviders) storage.extractReference(ProviderStorageKeys.UPDATED,
                    ReferenceMode.NEW_REFERENCE);
            serviceProvidersRel.getServiceProvidersCollection().add(serviceProviders);
            serviceProvidersRel = entityManager.merge(serviceProvidersRel);
        }
        if (storage.validateReference(ProviderStorageKeys.UPDATED_ADMIN, ReferenceMode.NEW_REFERENCE)) {
            administrators = (Administrators) storage.extractReference(ProviderStorageKeys.UPDATED_ADMIN,
                    ReferenceMode.NEW_REFERENCE);
            administrators.getServiceProvidersCollection().add(serviceProviders);
            administrators = entityManager.merge(administrators);
        }
        if (storage.validateReference(ProviderStorageKeys.AUTHORIZED_BY, ReferenceMode.NEW_REFERENCE)) {
            administrators1 = (Administrators) storage.extractReference(ProviderStorageKeys.AUTHORIZED_BY,
                    ReferenceMode.NEW_REFERENCE);
            administrators1.getServiceProvidersCollection().add(serviceProviders);
            administrators1 = entityManager.merge(administrators1);
        }

        for (ServiceProviders serviceProvidersCollectionServiceProviders : serviceProviders
                .getServiceProvidersCollection()) {
            ServiceProviders oldServiceProvidersOfServiceProvidersCollectionServiceProviders = serviceProvidersCollectionServiceProviders
                    .getServiceProviders();
            serviceProvidersCollectionServiceProviders.setServiceProviders(serviceProviders);
            serviceProvidersCollectionServiceProviders = entityManager
                    .merge(serviceProvidersCollectionServiceProviders);
            if (oldServiceProvidersOfServiceProvidersCollectionServiceProviders != null) {
                oldServiceProvidersOfServiceProvidersCollectionServiceProviders.getServiceProvidersCollection()
                        .remove(serviceProvidersCollectionServiceProviders);
                oldServiceProvidersOfServiceProvidersCollectionServiceProviders = entityManager
                        .merge(oldServiceProvidersOfServiceProvidersCollectionServiceProviders);
            }
        }

    }

    /**
     * Pre delete.
     * 
     * @param toDelete
     *            the to delete
     * @return the service providers
     * @throws NonexistentEntityException
     *             the nonexistent entity exception
     * @throws IllegalOrphanException
     *             the illegal orphan exception
     * @since 2/09/2014, 09:31:49 PM
     */
    @Override
    public ServiceProviders preDelete(Number toDelete) throws NonexistentEntityException, IllegalOrphanException {
        throw new NotImplementedException();
    }

    /**
     * Pre edit.
     * 
     * @param serviceProviders
     *            the service providers
     * @throws NonexistentEntityException
     *             the nonexistent entity exception
     * @throws IllegalOrphanException
     *             the illegal orphan exception
     * @since 2/09/2014, 09:31:49 PM
     */
    @Override
    public void preEdit(ServiceProviders serviceProviders)
            throws NonexistentEntityException, IllegalOrphanException {
        serviceProviders.getServiceProvidersPK()
                .setIdServiceProvider(serviceProviders.getAppUsers().getIdApplicationUsers());
        try {
            ServiceProviders persistentServiceProviders = entityManager.find(ServiceProviders.class,
                    serviceProviders.getServiceProvidersPK());
            AppUsers appUsersOld = persistentServiceProviders.getAppUsers();
            storage.storeReference(ProviderStorageKeys.SUPER_TYPE, ReferenceMode.OLD_REFERENCE, appUsersOld);
            AppUsers appUsersNew = serviceProviders.getAppUsers();

            ProviderTypes providerRoleOld = persistentServiceProviders.getProviderRole();
            storage.storeReference(ProviderStorageKeys.ROLE, ReferenceMode.OLD_REFERENCE, providerRoleOld);
            ProviderTypes providerRoleNew = serviceProviders.getProviderRole();

            ServiceProviders serviceProvidersRelOld = persistentServiceProviders.getServiceProviders();
            storage.storeReference(ProviderStorageKeys.UPDATED, ReferenceMode.OLD_REFERENCE,
                    serviceProvidersRelOld);
            ServiceProviders serviceProvidersRelNew = serviceProviders.getServiceProviders();

            Administrators administratorsOld = persistentServiceProviders.getAdministrators();
            storage.storeReference(ProviderStorageKeys.UPDATED_ADMIN, ReferenceMode.OLD_REFERENCE,
                    administratorsOld);
            Administrators administratorsNew = serviceProviders.getAdministrators();

            Administrators administrators1Old = persistentServiceProviders.getAdministrators1();
            storage.storeReference(ProviderStorageKeys.AUTHORIZED_BY, ReferenceMode.OLD_REFERENCE,
                    administrators1Old);
            Administrators administrators1New = serviceProviders.getAdministrators1();

            Collection<ServiceProviders> serviceProvidersCollectionOld = persistentServiceProviders
                    .getServiceProvidersCollection();
            storage.storeReference(ProviderStorageKeys.SELVES, ReferenceMode.OLD_REFERENCE,
                    serviceProvidersCollectionOld);
            Collection<ServiceProviders> serviceProvidersCollectionNew = serviceProviders
                    .getServiceProvidersCollection();

            if (appUsersNew != null) {
                appUsersNew = entityManager.getReference(appUsersNew.getClass(),
                        appUsersNew.getIdApplicationUsers());
                serviceProviders.setAppUsers(appUsersNew);
                storage.storeReference(ProviderStorageKeys.SUPER_TYPE, ReferenceMode.NEW_REFERENCE, appUsersNew);
            }
            if (providerRoleNew != null) {
                providerRoleNew = entityManager.getReference(providerRoleNew.getClass(),
                        providerRoleNew.getIdProviderType());
                serviceProviders.setProviderRole(providerRoleNew);
                storage.storeReference(ProviderStorageKeys.ROLE, ReferenceMode.NEW_REFERENCE, providerRoleNew);
            }
            if (serviceProvidersRelNew != null) {
                serviceProvidersRelNew = entityManager.getReference(serviceProvidersRelNew.getClass(),
                        serviceProvidersRelNew.getServiceProvidersPK());
                serviceProviders.setServiceProviders(serviceProvidersRelNew);
                storage.storeReference(ProviderStorageKeys.UPDATED, ReferenceMode.NEW_REFERENCE,
                        serviceProvidersRelNew);
            }
            if (administratorsNew != null) {
                administratorsNew = entityManager.getReference(administratorsNew.getClass(),
                        administratorsNew.getAdministratorsPK());
                serviceProviders.setAdministrators(administratorsNew);
                storage.storeReference(ProviderStorageKeys.UPDATED_ADMIN, ReferenceMode.NEW_REFERENCE,
                        administratorsNew);
            }
            if (administrators1New != null) {
                administrators1New = entityManager.getReference(administrators1New.getClass(),
                        administrators1New.getAdministratorsPK());
                serviceProviders.setAdministrators1(administrators1New);
                storage.storeReference(ProviderStorageKeys.AUTHORIZED_BY, ReferenceMode.NEW_REFERENCE,
                        administrators1New);
            }
            Collection<ServiceProviders> attachedServiceProvidersCollectionNew = new ArrayList<ServiceProviders>();
            for (ServiceProviders serviceProvidersCollectionNewServiceProvidersToAttach : serviceProvidersCollectionNew) {
                serviceProvidersCollectionNewServiceProvidersToAttach = entityManager.getReference(
                        serviceProvidersCollectionNewServiceProvidersToAttach.getClass(),
                        serviceProvidersCollectionNewServiceProvidersToAttach.getServiceProvidersPK());
                attachedServiceProvidersCollectionNew.add(serviceProvidersCollectionNewServiceProvidersToAttach);
            }
            serviceProvidersCollectionNew = attachedServiceProvidersCollectionNew;
            serviceProviders.setServiceProvidersCollection(serviceProvidersCollectionNew);
            storage.storeReference(ProviderStorageKeys.SELVES, ReferenceMode.NEW_REFERENCE,
                    serviceProvidersCollectionNew);
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                ServiceProvidersPK id = serviceProviders.getServiceProvidersPK();
                if (findByComplexId(entityManager, id, ServiceProviders.class) == null) {
                    throw new NonexistentEntityException(
                            "The serviceProviders with id " + id.resume() + " no longer exists.");
                }
            }
            throw ex;
        }
    }

    /**
     * Post edit.
     * 
     * @param serviceProviders
     *            the service providers
     * @since 2/09/2014, 09:31:49 PM
     */
    @SuppressWarnings("unchecked")
    @Override
    public void postEdit(ServiceProviders serviceProviders) {

        AppUsers appUsersOld, appUsersNew;
        Administrators administratorsOld, administratorsNew, administrators1Old, administrators1New;
        ProviderTypes providerRoleOld, providerRoleNew;
        ServiceProviders serviceProvidersRelOld, serviceProvidersRelNew;

        if (storage.compareReferences(ProviderStorageKeys.SUPER_TYPE, ReferenceMode.OLD_REFERENCE)) {
            appUsersOld = (AppUsers) storage.extractReference(ProviderStorageKeys.SUPER_TYPE,
                    ReferenceMode.OLD_REFERENCE);
            appUsersOld.getServiceProvidersCollection().remove(serviceProviders);
            appUsersOld = entityManager.merge(appUsersOld);
        }
        if (storage.compareReferences(ProviderStorageKeys.SUPER_TYPE, ReferenceMode.NEW_REFERENCE)) {
            appUsersNew = (AppUsers) storage.extractReference(ProviderStorageKeys.SUPER_TYPE,
                    ReferenceMode.NEW_REFERENCE);
            appUsersNew.getServiceProvidersCollection().add(serviceProviders);
            appUsersNew = entityManager.merge(appUsersNew);
        }
        if (storage.compareReferences(ProviderStorageKeys.ROLE, ReferenceMode.OLD_REFERENCE)) {
            providerRoleOld = (ProviderTypes) storage.extractReference(ProviderStorageKeys.ROLE,
                    ReferenceMode.OLD_REFERENCE);
            providerRoleOld.getServiceProvidersCollection().remove(serviceProviders);
            providerRoleOld = entityManager.merge(providerRoleOld);
        }
        if (storage.compareReferences(ProviderStorageKeys.ROLE, ReferenceMode.NEW_REFERENCE)) {
            providerRoleNew = (ProviderTypes) storage.extractReference(ProviderStorageKeys.ROLE,
                    ReferenceMode.NEW_REFERENCE);
            providerRoleNew.getServiceProvidersCollection().add(serviceProviders);
            providerRoleNew = entityManager.merge(providerRoleNew);
        }
        if (storage.compareReferences(ProviderStorageKeys.UPDATED, ReferenceMode.OLD_REFERENCE)) {
            serviceProvidersRelOld = (ServiceProviders) storage.extractReference(ProviderStorageKeys.UPDATED,
                    ReferenceMode.OLD_REFERENCE);
            serviceProvidersRelOld.getServiceProvidersCollection().remove(serviceProviders);
            serviceProvidersRelOld = entityManager.merge(serviceProvidersRelOld);
        }
        if (storage.compareReferences(ProviderStorageKeys.UPDATED, ReferenceMode.NEW_REFERENCE)) {
            serviceProvidersRelNew = (ServiceProviders) storage.extractReference(ProviderStorageKeys.UPDATED,
                    ReferenceMode.NEW_REFERENCE);
            serviceProvidersRelNew.getServiceProvidersCollection().add(serviceProviders);
            serviceProvidersRelNew = entityManager.merge(serviceProvidersRelNew);
        }
        if (storage.compareReferences(ProviderStorageKeys.UPDATED_ADMIN, ReferenceMode.OLD_REFERENCE)) {
            administratorsOld = (Administrators) storage.extractReference(ProviderStorageKeys.UPDATED_ADMIN,
                    ReferenceMode.OLD_REFERENCE);
            administratorsOld.getServiceProvidersCollection().remove(serviceProviders);
            administratorsOld = entityManager.merge(administratorsOld);
        }
        if (storage.compareReferences(ProviderStorageKeys.UPDATED_ADMIN, ReferenceMode.NEW_REFERENCE)) {
            administratorsNew = (Administrators) storage.extractReference(ProviderStorageKeys.UPDATED_ADMIN,
                    ReferenceMode.NEW_REFERENCE);
            administratorsNew.getServiceProvidersCollection().add(serviceProviders);
            administratorsNew = entityManager.merge(administratorsNew);
        }
        if (storage.compareReferences(ProviderStorageKeys.AUTHORIZED_BY, ReferenceMode.OLD_REFERENCE)) {
            administrators1Old = (Administrators) storage.extractReference(ProviderStorageKeys.AUTHORIZED_BY,
                    ReferenceMode.OLD_REFERENCE);
            administrators1Old.getServiceProvidersCollection().remove(serviceProviders);
            administrators1Old = entityManager.merge(administrators1Old);
        }
        if (storage.compareReferences(ProviderStorageKeys.AUTHORIZED_BY, ReferenceMode.NEW_REFERENCE)) {
            administrators1New = (Administrators) storage.extractReference(ProviderStorageKeys.AUTHORIZED_BY,
                    ReferenceMode.NEW_REFERENCE);
            administrators1New.getServiceProvidersCollection().add(serviceProviders);
            administrators1New = entityManager.merge(administrators1New);
        }

        Collection<ServiceProviders> serviceProvidersCollectionOld = (Collection<ServiceProviders>) storage
                .extractReferences(ProviderStorageKeys.SELVES, ReferenceMode.OLD_REFERENCE);
        Collection<ServiceProviders> serviceProvidersCollectionNew = (Collection<ServiceProviders>) storage
                .extractReferences(ProviderStorageKeys.SELVES, ReferenceMode.NEW_REFERENCE);
        for (ServiceProviders serviceProvidersCollectionOldServiceProviders : serviceProvidersCollectionOld) {
            if (!serviceProvidersCollectionNew.contains(serviceProvidersCollectionOldServiceProviders)) {
                serviceProvidersCollectionOldServiceProviders.setServiceProviders(null);
                serviceProvidersCollectionOldServiceProviders = entityManager
                        .merge(serviceProvidersCollectionOldServiceProviders);
            }
        }
        for (ServiceProviders serviceProvidersCollectionNewServiceProviders : serviceProvidersCollectionNew) {
            if (!serviceProvidersCollectionOld.contains(serviceProvidersCollectionNewServiceProviders)) {
                ServiceProviders oldServiceProvidersOfServiceProvidersCollectionNewServiceProviders = serviceProvidersCollectionNewServiceProviders
                        .getServiceProviders();
                serviceProvidersCollectionNewServiceProviders.setServiceProviders(serviceProviders);
                serviceProvidersCollectionNewServiceProviders = entityManager
                        .merge(serviceProvidersCollectionNewServiceProviders);
                if (oldServiceProvidersOfServiceProvidersCollectionNewServiceProviders != null
                        && !oldServiceProvidersOfServiceProvidersCollectionNewServiceProviders
                                .equals(serviceProviders)) {
                    oldServiceProvidersOfServiceProvidersCollectionNewServiceProviders
                            .getServiceProvidersCollection().remove(serviceProvidersCollectionNewServiceProviders);
                    oldServiceProvidersOfServiceProvidersCollectionNewServiceProviders = entityManager
                            .merge(oldServiceProvidersOfServiceProvidersCollectionNewServiceProviders);
                }
            }
        }
    }

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

    /**
     * Gets the by logical id.
     * 
     * @param idEntity
     *            the id entity
     * @return the by logical id
     * @since 2/09/2014, 09:31:49 PM
     */
    @Override
    public ServiceProviders getByLogicalId(Serializable idEntity) {
        TypedQuery<ServiceProviders> query = entityManager
                .createNamedQuery("ServiceProviders." + NormalizedEntity.NAMED_QUERY_ID, ServiceProviders.class);
        query.setParameter("appUsername", ((ServiceProvidersPK) idEntity).getAppUsername());
        return query.getSingleResult();
    }

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

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