de.fiz.ddb.aas.auxiliaryoperations.ThreadOrganisationSetApprove.java Source code

Java tutorial

Introduction

Here is the source code for de.fiz.ddb.aas.auxiliaryoperations.ThreadOrganisationSetApprove.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.fiz.ddb.aas.auxiliaryoperations;

import de.fiz.ddb.aas.administration.AasPrincipal;
import de.fiz.ddb.aas.administration.ConstEnumOrgStatus;
import de.fiz.ddb.aas.administration.Organisation;
import de.fiz.ddb.aas.authorization.PrivilegeEnum;
import de.fiz.ddb.aas.authorization.Scope;
import de.fiz.ddb.aas.auxiliaryobjects.Email;
import de.fiz.ddb.aas.auxiliaryobjects.OIDs;
import de.fiz.ddb.aas.exceptions.AASUnauthorizedException;
import de.fiz.ddb.aas.security.Authorizable;
import de.fiz.ddb.aas.security.PreAuthorize;
import de.fiz.ddb.aas.utils.LDAPEngineUtilityOrganisation;
import de.fiz.ddb.ldap.connector.LDAPConnector;

import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.NameAlreadyBoundException;
import javax.naming.NameNotFoundException;
import javax.naming.directory.AttributeModificationException;
import javax.naming.directory.SearchControls;

import org.apache.commons.lang.StringUtils;

/**
 *
 * @author bkl
 */
public class ThreadOrganisationSetApprove extends LDAPEngineUtilityOrganisation
        implements Callable<Organisation>, Authorizable {

    private CountDownLatch _ready = null;

    private AasPrincipal _performer = null;

    private Organisation _organisation = null;

    private ConstEnumOrgStatus _oldStatus = null;

    // ***************************************************************************
    private ThreadOrganisationSetApprove(AasPrincipal performer) throws AASUnauthorizedException {
        this._performer = performer;
        if (performer == null) {
            throw new IllegalArgumentException("The contractor is unknown: Performer == null");
        }
    }

    public ThreadOrganisationSetApprove(OIDs pOIDs, AasPrincipal performer)
            throws IllegalArgumentException, ExecutionException, AASUnauthorizedException {
        this(performer);
        //check mandatory fields
        if (pOIDs == null) {
            throw new IllegalArgumentException("Illegal argument: OIDs = " + pOIDs);
        }
        ThreadOrganisationRead threadOrgOnWorkDirRead = new ThreadOrganisationRead(pOIDs, performer);
        try {
            this._organisation = threadOrgOnWorkDirRead.call();
            this._oldStatus = this._organisation.getStatus();
        } catch (NameNotFoundException ex) {
            throw new ExecutionException(new NameNotFoundException(
                    "OriganOrganization with ID '" + pOIDs + "' was not found. Message: " + ex.getMessage()));
        }
    }

    public ThreadOrganisationSetApprove(CountDownLatch pReadyLatch, OIDs pOIDs, AasPrincipal performer)
            throws AssertionError, IllegalArgumentException, ExecutionException, AASUnauthorizedException {
        this(pOIDs, performer);
        this._ready = pReadyLatch;
    }

    // ***************************************************************************
    @PreAuthorize(privileges = { PrivilegeEnum.ADMIN }, scope = Scope.ORGANIZATION, cacheUpdate = true)
    public Organisation call() throws AASUnauthorizedException, ExecutionException, IllegalAccessException {
        if (ConstEnumOrgStatus.approved.equals(this._organisation.getStatus())) {
            throw new ExecutionException("Die Institution ist bereits in der Status 'approved'.", null);
        }
        Future<Organisation> submitOrgOnWorkDir = null;
        Future<Organisation> submitOrgOnLicencedDir = null;

        Future<Organisation> submitOrgParentOnLicencedDir = null;
        Future<Organisation> submitOrgParentOnWorkDir = null;

        Organisation vOrgParentOnLicenceDir = null;
        Organisation vOrgParentOnWorkDir = null;

        try {
            // -- set a new status:
            this._organisation.setStatus(ConstEnumOrgStatus.approved);
            // -- save status:
            ThreadOrganisationUpdate threadOrganisationUpdate = new ThreadOrganisationUpdate(_ready, _organisation,
                    false, _performer);
            threadOrganisationUpdate.setChangeOfStatus(true);
            submitOrgOnWorkDir = LDAPConnector.getSingletonInstance().getExecutorServiceOne()
                    .submit(threadOrganisationUpdate);

            // -- Ist diese Organisation unter Licensed schon vorhanden?
            // -- Read organization on the license directory:
            ThreadOrganisationRead threadOrgOnLicencedDirRead = new ThreadOrganisationRead(
                    new OIDs(this._organisation.getOIDs().getOrgName(), false), this.getPerformer());
            // -- the request goes to the branch with licensed organizations:
            threadOrgOnLicencedDirRead.setLicensedOrgs(true);
            submitOrgOnLicencedDir = LDAPConnector.getSingletonInstance().getExecutorServiceOne()
                    .submit(threadOrgOnLicencedDirRead);

            // -- Operations in the licensed area...
            Boolean vIsOrgParentLicense = null;
            if (this._organisation.getOrgParent() != null) {

                // -- Parent on the license directory:
                ThreadOrganisationRead threadOrgParentOnLicencedDirRead = new ThreadOrganisationRead(
                        new OIDs(this._organisation.getOrgParent(), false), this.getPerformer());
                // -- the request goes to the branch with licensed organizations:
                threadOrgParentOnLicencedDirRead.setLicensedOrgs(true);
                submitOrgParentOnLicencedDir = LDAPConnector.getSingletonInstance().getExecutorServiceOne()
                        .submit(threadOrgParentOnLicencedDirRead);

                // -- Parent on the work directory:
                ThreadOrganisationRead threadOrgParentOnWorkDirRead = new ThreadOrganisationRead(
                        new OIDs(this._organisation.getOrgParent(), false), this.getPerformer());
                // -- the request goes to the branch with licensed organizations:
                submitOrgParentOnWorkDir = LDAPConnector.getSingletonInstance().getExecutorServiceOne()
                        .submit(threadOrgParentOnWorkDirRead);

                // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                // -- Parent on the license directory:
                try {
                    //vIsOrgParentLicense = (threadOrgParentOnLicencedDirRead.call() != null);
                    vIsOrgParentLicense = ((vOrgParentOnLicenceDir = submitOrgParentOnLicencedDir.get(3,
                            TimeUnit.SECONDS)) != null);
                }
                /*
                catch (NameNotFoundException ex) {
                // hier gibt es keinen Grund zur Panik! ;-)
                vIsOrgParentLicense = Boolean.FALSE;
                }
                 */
                catch (ExecutionException ex) {
                    if ((ex.getCause() != null)
                            && (ex.getCause().getClass().isAssignableFrom(NameNotFoundException.class))) {
                        // hier gibt es keinen Grund zur Panik! ;-)
                        vIsOrgParentLicense = Boolean.FALSE;
                    } else {
                        throw ex;
                    }
                } catch (InterruptedException ex) {
                    throw new ExecutionException(ex);
                } catch (TimeoutException ex) {
                    throw new ExecutionException(ex);
                }
            }

            try {

                // -- Update abwarten
                this._organisation = submitOrgOnWorkDir.get(3, TimeUnit.SECONDS);

                // -- die Organisation wenn mglich in der lizenzierte Verzeichnis schreiben:
                if ((vIsOrgParentLicense == null) || (vIsOrgParentLicense.booleanValue())) {
                    // -- ! This institution is classified to the licensed organizations:
                    Organisation vOrgOnLicensedDir = null;
                    try {
                        vOrgOnLicensedDir = submitOrgOnLicencedDir.get(3, TimeUnit.SECONDS);
                        if (!vOrgOnLicensedDir.getOrgRDN().equalsIgnoreCase(this._organisation.getOrgRDN())) {
                            /*
                             * The shift operation works beautifully but may cause to error, because there are 
                             * potential changes from the sub-organizations in the Work Directory will not be included.
                             * ...therefore the Orgnanisation is first deleted and then will be re-copied
                            ThreadOrganisationMove threadOrganisationMove =
                            new ThreadOrganisationMove(vOrgOnLicensedDir.getOIDs().getOrgName(), this._organisation
                                .getOrgParent(), true, _performer);
                            vOrgOnLicensedDir = threadOrganisationMove.call();
                             */
                            this.deletingFromLicensedOrgsDir(vOrgOnLicensedDir);
                            // -- !!! very important for further processing:
                            vOrgOnLicensedDir = null;
                        }
                    }
                    /*
                    catch (NameNotFoundException ex) {
                    // es gibt keinen Grund zur Panik! ;-)
                    }
                     */
                    catch (ExecutionException ex) {
                        if ((ex.getCause() != null)
                                && (ex.getCause().getClass().isAssignableFrom(NameNotFoundException.class))) {
                            // hier gibt es keinen Grund zur Panik...
                        } else {
                            // hier aber schon...
                            throw ex;
                        }
                    } catch (InterruptedException ex) {
                        throw new ExecutionException(ex);
                    } catch (TimeoutException ex) {
                        throw new ExecutionException(ex);
                    }

                    if (vOrgOnLicensedDir != null) {
                        if (!ConstEnumOrgStatus.revised.equals(this._oldStatus)) {
                            // -- This should be never happen:
                            LOG.log(Level.WARNING,
                                    "The old status is not ''revised'' but this organization is between the Licensed: this should never be happen! Old status: ''{0}''",
                                    this._oldStatus.name());
                        }
                        // -- !!! The organization could not be moved:
                        if (vOrgOnLicensedDir.getOrgRDN().equals(this._organisation.getOrgRDN())) {
                            // -- Update licensed organization:
                            try {
                                threadOrganisationUpdate = new ThreadOrganisationUpdate(_ready, _organisation,
                                        false, _performer);
                                threadOrganisationUpdate.setUpdatingOfLicensedOrgs(true);
                                threadOrganisationUpdate.call();
                            } catch (NameNotFoundException ex) {
                                throw new ExecutionException(ex);
                            } catch (AttributeModificationException ex) {
                                throw new ExecutionException(ex);
                            }
                        } else {
                            LOG.log(Level.WARNING, "The licensed (RDN='" + vOrgOnLicensedDir.getOrgRDN()
                                    + "') organization can not be updated because it has been postponed to new RDN='"
                                    + this._organisation.getOrgRDN() + "'");
                        }
                    } else {
                        // -- Der Knoten sollte kopiert werden aber nur unter einem Bedingung...
                        if (submitOrgParentOnWorkDir != null) {
                            // -- Parent on the work directory:
                            try {
                                vOrgParentOnWorkDir = submitOrgParentOnWorkDir.get(3, TimeUnit.SECONDS);
                            } catch (ExecutionException ex) {
                                if ((ex.getCause() != null) && (ex.getCause().getClass()
                                        .isAssignableFrom(NameNotFoundException.class))) {
                                    // hier gibt es keinen Grund zur Panik! ;-)
                                } else {
                                    throw ex;
                                }
                            } catch (InterruptedException ex) {
                                throw new ExecutionException(ex);
                            } catch (TimeoutException ex) {
                                throw new ExecutionException(ex);
                            }
                        }
                        // ...dass die RDN des Parnts- Knoten stimmt, das heit, dass die nicht verschoben wurde:
                        if (((vOrgParentOnWorkDir != null) && (vOrgParentOnLicenceDir != null)
                                && (vOrgParentOnWorkDir.getOrgRDN().equals(vOrgParentOnLicenceDir.getOrgRDN())))
                                || ((vOrgParentOnWorkDir == null) && (vOrgParentOnLicenceDir == null))) {
                            this.copyingToLicensedOrgs(_organisation);
                        }
                    }

                }
            } catch (InterruptedException ex) {
                throw new ExecutionException(ex);
            } catch (TimeoutException ex) {
                throw new ExecutionException(ex);
            }
        } finally {
            if ((submitOrgOnWorkDir != null) && (!submitOrgOnWorkDir.isDone())
                    && (!submitOrgOnWorkDir.isCancelled())) {
                submitOrgOnWorkDir.cancel(true);
            }
            if ((submitOrgOnLicencedDir != null) && (!submitOrgOnLicencedDir.isDone())
                    && (!submitOrgOnLicencedDir.isCancelled())) {
                submitOrgOnLicencedDir.cancel(true);
            }
            if ((submitOrgParentOnWorkDir != null) && (!submitOrgParentOnWorkDir.isDone())
                    && (!submitOrgParentOnWorkDir.isCancelled())) {
                submitOrgParentOnWorkDir.cancel(true);
            }
            if ((submitOrgParentOnLicencedDir != null) && (!submitOrgParentOnLicencedDir.isDone())
                    && (!submitOrgParentOnLicencedDir.isCancelled())) {
                submitOrgParentOnLicencedDir.cancel(true);
            }
        }
        return this._organisation;
    }

    // ***************************************************************************
    // --- Aufpassen: Es ist eine rekursive Routine!
    // --- copying to licensed organisations 
    private void copyingToLicensedOrgs(Organisation pOrganisation)
            throws AASUnauthorizedException, ExecutionException {
        LOG.log(Level.INFO, "copyingToLicensedOrgs.Organisation.getOIDs(): "
                + (pOrganisation != null ? pOrganisation.getOIDs() : "NULL"));
        if ((pOrganisation == null) || !ConstEnumOrgStatus.approved.equals(pOrganisation.getStatus())) {
            LOG.log(Level.INFO,
                    "Return: not in Status: " + (pOrganisation != null ? pOrganisation.getStatus() : "NULL"));
            return;
        }

        ThreadOrganisationCreate threadOrganisationCreate;
        ThreadOrganisationRead threadOrgOnWorkDirRead;

        try {
            threadOrganisationCreate = new ThreadOrganisationCreate(pOrganisation, _performer);
            threadOrganisationCreate.setAddToLicensedOrgs(true);
            threadOrganisationCreate.call();
            // -- find all sub-organizations
            Set<OIDs> vAllSubOrgIdsOnWorkDir = this.getAllSubOrgIds(false, pOrganisation.getOIDs(),
                    SearchControls.ONELEVEL_SCOPE, this._performer);
            // -- all sub-organizations must be deleted:
            for (OIDs vOIDs : vAllSubOrgIdsOnWorkDir) {
                try {
                    threadOrgOnWorkDirRead = new ThreadOrganisationRead(vOIDs, this._performer);
                    this.copyingToLicensedOrgs(threadOrgOnWorkDirRead.call());
                } catch (NameNotFoundException ex) {
                    LOG.log(Level.WARNING,
                            "copyingToLicensedOrgs.NameNotFoundException: ex '" + ex.getMessage() + "'");
                } catch (AASUnauthorizedException ex) {
                    LOG.log(Level.WARNING, "copyingToLicensedOrgs.AASUnauthorizedException: ex: '" + ex.getMessage()
                            + "', Performer" + _performer);
                } catch (ExecutionException ex) {
                    LOG.log(Level.WARNING,
                            "copyingToLicensedOrgs.ExecutionException: oid = '" + pOrganisation.getOIDs() + "'");
                }
            }
        } catch (NameAlreadyBoundException ex) {
            LOG.log(Level.WARNING,
                    "copyingToLicensedOrgs.NameAlreadyBoundException: oid = '" + pOrganisation.getOIDs() + "'");
        } catch (AASUnauthorizedException ex) {
            LOG.log(Level.WARNING, "copyingToLicensedOrgs.AASUnauthorizedException: oid = '"
                    + pOrganisation.getOIDs() + "', Performer" + _performer);
        } catch (ExecutionException ex) {
            LOG.log(Level.WARNING,
                    "copyingToLicensedOrgs.ExecutionException: oid = '" + pOrganisation.getOIDs() + "'");
        }
    }

    // ***************************************************************************
    private void deletingFromLicensedOrgsDir(Organisation pOrgOnLicensedDir)
            throws IllegalArgumentException, ExecutionException {
        if (pOrgOnLicensedDir == null) {
            return;
        }
        LOG.log(Level.INFO, "deletingOfLicensedOrgs: pOrgOnLicensedDir.getOIDs(): " + pOrgOnLicensedDir.getOIDs());
        // -- ...must be deleted
        ThreadOrganisationDelete threadOrganisationDelete = new ThreadOrganisationDelete(_ready,
                pOrgOnLicensedDir.getOIDs(), _performer);
        threadOrganisationDelete.setLicensedOrgs(true);
        try {
            threadOrganisationDelete.call();
        } catch (ExecutionException ex) {
            LOG.log(Level.WARNING,
                    "deletingOfLicensedOrgs.ExecutionException: oid = '" + this._organisation.getOIDs() + "'");
        } catch (AASUnauthorizedException ex) {
            LOG.log(Level.WARNING, "deletingOfLicensedOrgs.AASUnauthorizedException: oid = '"
                    + this._organisation.getOIDs() + "', Performer" + _performer);
        } catch (NameNotFoundException ex) {
            LOG.log(Level.WARNING,
                    "deletingOfLicensedOrgs: NameNotFoundException: oid = '" + this._organisation.getOIDs() + "'");
        }
    }

    // ***************************************************************************
    public AasPrincipal getPerformer() {
        return _performer;
    }

    // ***************************************************************************
    public String getResourceId() {
        if (_organisation != null) {
            return _organisation.getId();
        }
        return null;
    }

    // ***************************************************************************
}