Example usage for java.util HashMap remove

List of usage examples for java.util HashMap remove

Introduction

In this page you can find the example usage for java.util HashMap remove.

Prototype

public V remove(Object key) 

Source Link

Document

Removes the mapping for the specified key from this map if present.

Usage

From source file:ffx.potential.parsers.PDBFilter.java

/**
 * Assign force field atoms types to common chemistries using "biotype"
 * records./*from w w w .  j a  va 2  s .  co m*/
 */
private void assignAtomTypes() {
    /**
     * Create a list to store bonds defined by PDB atom names.
     */
    bondList = new ArrayList<>();

    /**
     * To Do: Look for cyclic peptides and disulfides.
     */
    Polymer[] polymers = activeMolecularAssembly.getChains();

    /**
     * Loop over chains.
     */
    if (polymers != null) {
        logger.info(format("\n Assigning atom types for %d chains.", polymers.length));
        for (Polymer polymer : polymers) {
            List<Residue> residues = polymer.getResidues();
            int numberOfResidues = residues.size();
            /**
             * Check if all residues are known amino acids.
             */
            boolean isProtein = true;
            for (int residueNumber = 0; residueNumber < numberOfResidues; residueNumber++) {
                Residue residue = residues.get(residueNumber);
                String name = residue.getName().toUpperCase();
                boolean aa = false;
                for (AminoAcid3 amino : aminoAcidList) {
                    if (amino.toString().equalsIgnoreCase(name)) {
                        aa = true;
                        checkHydrogenAtomNames(residue);
                        break;
                    }
                }
                // Check for a patch.
                if (!aa) {
                    logger.info(" Checking for non-standard amino acid patch " + name);
                    HashMap<String, AtomType> types = forceField.getAtomTypes(name);
                    if (types.isEmpty()) {
                        isProtein = false;
                        break;
                    } else {
                        logger.info(" Patch found for non-standard amino acid " + name);
                    }
                }
            }

            /**
             * If all the residues in this chain have known amino acids
             * names, then attempt to assign atom types.
             */
            if (isProtein) {
                try {
                    logger.info(format(" Amino acid chain %s", polymer.getName()));
                    double dist = properties.getDouble("chainbreak", 3.0);
                    // Detect main chain breaks!
                    List<List<Residue>> subChains = findChainBreaks(residues, dist);
                    for (List<Residue> subChain : subChains) {
                        assignAminoAcidAtomTypes(subChain);
                    }
                } catch (MissingHeavyAtomException missingHeavyAtomException) {
                    logger.severe(missingHeavyAtomException.toString());
                } catch (MissingAtomTypeException missingAtomTypeException) {
                    logger.severe(missingAtomTypeException.toString());
                }
                continue;
            }

            /**
             * Check if all residues have known nucleic acids names.
             */
            boolean isNucleicAcid = true;
            for (int residueNumber = 0; residueNumber < numberOfResidues; residueNumber++) {
                Residue residue = residues.get(residueNumber);
                String name = residue.getName().toUpperCase();
                /**
                 * Convert 1 and 2-character nucleic acid names to
                 * 3-character names.
                 */
                if (name.length() == 1) {
                    if (name.equals("A")) {
                        name = NucleicAcid3.ADE.toString();
                    } else if (name.equals("C")) {
                        name = NucleicAcid3.CYT.toString();
                    } else if (name.equals("G")) {
                        name = NucleicAcid3.GUA.toString();
                    } else if (name.equals("T")) {
                        name = NucleicAcid3.THY.toString();
                    } else if (name.equals("U")) {
                        name = NucleicAcid3.URI.toString();
                    }
                } else if (name.length() == 2) {
                    if (name.equals("YG")) {
                        name = NucleicAcid3.YYG.toString();
                    }
                }
                residue.setName(name);
                NucleicAcid3 nucleicAcid = null;
                for (NucleicAcid3 nucleic : nucleicAcidList) {
                    String nuc3 = nucleic.toString();
                    nuc3 = nuc3.substring(nuc3.length() - 3);
                    if (nuc3.equalsIgnoreCase(name)) {
                        nucleicAcid = nucleic;
                        break;
                    }
                }
                if (nucleicAcid == null) {
                    logger.info(format("Nucleic acid was not recognized %s.", name));
                    isNucleicAcid = false;
                    break;
                }
            }

            /**
             * If all the residues in this chain have known nucleic acids
             * names, then attempt to assign atom types.
             */
            if (isNucleicAcid) {
                try {
                    logger.info(format(" Nucleic acid chain %s", polymer.getName()));
                    assignNucleicAcidAtomTypes(residues, forceField, bondList);
                } catch (MissingHeavyAtomException | MissingAtomTypeException e) {
                    logger.severe(e.toString());
                }
            }
        }
    }

    // Assign ion atom types.
    ArrayList<MSNode> ions = activeMolecularAssembly.getIons();
    if (ions != null && ions.size() > 0) {
        logger.info(format(" Assigning atom types for %d ions.", ions.size()));
        for (MSNode m : ions) {
            Molecule ion = (Molecule) m;
            String name = ion.getResidueName().toUpperCase();
            HetAtoms hetatm = HetAtoms.valueOf(name);
            Atom atom = ion.getAtomList().get(0);
            if (ion.getAtomList().size() != 1) {
                logger.severe(format(" Check residue %s of chain %s.", ion.toString(), ion.getChainID()));
            }
            try {
                switch (hetatm) {
                case NA:
                    atom.setAtomType(findAtomType(2003));
                    break;
                case K:
                    atom.setAtomType(findAtomType(2004));
                    break;
                case MG:
                case MG2:
                    atom.setAtomType(findAtomType(2005));
                    break;
                case CA:
                case CA2:
                    atom.setAtomType(findAtomType(2006));
                    break;
                case CL:
                    atom.setAtomType(findAtomType(2007));
                    break;
                case ZN:
                case ZN2:
                    atom.setAtomType(findAtomType(2008));
                    break;
                case BR:
                    atom.setAtomType(findAtomType(2009));
                    break;
                default:
                    logger.severe(format(" Check residue %s of chain %s.", ion.toString(), ion.getChainID()));
                }
            } catch (Exception e) {
                String message = "Error assigning atom types.";
                logger.log(Level.SEVERE, message, e);
            }
        }
    }
    // Assign water atom types.
    ArrayList<MSNode> water = activeMolecularAssembly.getWaters();
    if (water != null && water.size() > 0) {
        logger.info(format(" Assigning atom types for %d waters.", water.size()));
        for (MSNode m : water) {
            Molecule wat = (Molecule) m;
            try {
                Atom O = buildHeavy(wat, "O", null, 2001);
                Atom H1 = buildHydrogen(wat, "H1", O, 0.96e0, null, 109.5e0, null, 120.0e0, 0, 2002);
                H1.setHetero(true);
                Atom H2 = buildHydrogen(wat, "H2", O, 0.96e0, H1, 109.5e0, null, 120.0e0, 0, 2002);
                H2.setHetero(true);
            } catch (Exception e) {
                String message = "Error assigning atom types to a water.";
                logger.log(Level.SEVERE, message, e);
            }
        }
    }

    // Assign small molecule atom types.
    ArrayList<Molecule> molecules = activeMolecularAssembly.getMolecules();
    for (MSNode m : molecules) {
        Molecule molecule = (Molecule) m;
        String moleculeName = molecule.getResidueName();
        logger.info(" Attempting to patch " + moleculeName);
        ArrayList<Atom> moleculeAtoms = molecule.getAtomList();
        boolean patched = true;
        HashMap<String, AtomType> types = forceField.getAtomTypes(moleculeName);
        /**
         * Assign atom types for all known atoms.
         */
        for (Atom atom : moleculeAtoms) {
            String atomName = atom.getName().toUpperCase();
            AtomType atomType = types.get(atomName);
            if (atomType == null) {
                logger.info(" No atom type was found for " + atomName + " of " + moleculeName + ".");
                patched = false;
                break;
            } else {
                logger.fine(" " + atom.toString() + " -> " + atomType.toString());
                atom.setAtomType(atomType);
                types.remove(atomName);
            }
        }
        /**
         * Create missing hydrogen atoms. Check for missing heavy atoms.
         */
        if (patched && !types.isEmpty()) {
            for (AtomType type : types.values()) {
                if (type.atomicNumber != 1) {
                    logger.info(" Missing heavy atom " + type.name);
                    patched = false;
                    break;
                }
            }
        }
        // Create bonds between known atoms.
        if (patched) {
            for (Atom atom : moleculeAtoms) {
                String atomName = atom.getName();
                String bonds[] = forceField.getBonds(moleculeName, atomName);
                if (bonds != null) {
                    for (String name : bonds) {
                        Atom atom2 = molecule.getAtom(name);
                        if (atom2 != null && !atom.isBonded(atom2)) {
                            buildBond(atom, atom2);
                        }
                    }
                }
            }
        }
        // Create missing hydrogen atoms.
        if (patched && !types.isEmpty()) {
            // Create a hashmap of the molecule's atoms
            HashMap<String, Atom> atomMap = new HashMap<String, Atom>();
            for (Atom atom : moleculeAtoms) {
                atomMap.put(atom.getName().toUpperCase(), atom);
            }
            for (String atomName : types.keySet()) {
                AtomType type = types.get(atomName);
                String bonds[] = forceField.getBonds(moleculeName, atomName.toUpperCase());
                if (bonds == null || bonds.length != 1) {
                    patched = false;
                    logger.info(" Check biotype for hydrogen " + type.name + ".");
                    break;
                }
                // Get the heavy atom the hydrogen is bonded to.
                Atom ia = atomMap.get(bonds[0].toUpperCase());
                Atom hydrogen = new Atom(0, atomName, ia.getAltLoc(), new double[3], ia.getResidueName(),
                        ia.getResidueNumber(), ia.getChainID(), ia.getOccupancy(), ia.getTempFactor(),
                        ia.getSegID());
                logger.fine(" Created hydrogen " + atomName + ".");
                hydrogen.setAtomType(type);
                hydrogen.setHetero(true);
                molecule.addMSNode(hydrogen);
                int valence = ia.getAtomType().valence;
                List<Bond> aBonds = ia.getBonds();
                int numBonds = aBonds.size();
                /**
                 * Try to find the following configuration: ib-ia-ic
                 */
                Atom ib = null;
                Atom ic = null;
                Atom id = null;
                if (numBonds > 0) {
                    Bond bond = aBonds.get(0);
                    ib = bond.get1_2(ia);
                }
                if (numBonds > 1) {
                    Bond bond = aBonds.get(1);
                    ic = bond.get1_2(ia);
                }
                if (numBonds > 2) {
                    Bond bond = aBonds.get(2);
                    id = bond.get1_2(ia);
                }

                /**
                 * Building the hydrogens depends on hybridization and the
                 * locations of other bonded atoms.
                 */
                logger.fine(" Bonding " + atomName + " to " + ia.getName() + " (" + numBonds + " of " + valence
                        + ").");
                switch (valence) {
                case 4:
                    switch (numBonds) {
                    case 3:
                        // Find the average coordinates of atoms ib, ic and id.
                        double b[] = ib.getXYZ(null);
                        double c[] = ib.getXYZ(null);
                        double d[] = ib.getXYZ(null);
                        double a[] = new double[3];
                        a[0] = (b[0] + c[0] + d[0]) / 3.0;
                        a[1] = (b[1] + c[1] + d[1]) / 3.0;
                        a[2] = (b[2] + c[2] + d[2]) / 3.0;
                        // Place the hydrogen at chiral position #1.
                        intxyz(hydrogen, ia, 1.0, ib, 109.5, ic, 109.5, 1);
                        double e1[] = new double[3];
                        hydrogen.getXYZ(e1);
                        double ret[] = new double[3];
                        diff(a, e1, ret);
                        double l1 = r(ret);
                        // Place the hydrogen at chiral position #2.
                        intxyz(hydrogen, ia, 1.0, ib, 109.5, ic, 109.5, -1);
                        double e2[] = new double[3];
                        hydrogen.getXYZ(e2);
                        diff(a, e2, ret);
                        double l2 = r(ret);
                        // Revert to #1 if it is farther from the average.
                        if (l1 > l2) {
                            hydrogen.setXYZ(e1);
                        }
                        break;
                    case 2:
                        intxyz(hydrogen, ia, 1.0, ib, 109.5, ic, 109.5, 0);
                        break;
                    case 1:
                        intxyz(hydrogen, ia, 1.0, ib, 109.5, null, 0.0, 0);
                        break;
                    case 0:
                        intxyz(hydrogen, ia, 1.0, null, 0.0, null, 0.0, 0);
                        break;
                    default:
                        logger.info(" Check biotype for hydrogen " + atomName + ".");
                        patched = false;
                    }
                    break;
                case 3:
                    switch (numBonds) {
                    case 2:
                        intxyz(hydrogen, ia, 1.0, ib, 120.0, ic, 0.0, 0);
                        break;
                    case 1:
                        intxyz(hydrogen, ia, 1.0, ib, 120.0, null, 0.0, 0);
                        break;
                    case 0:
                        intxyz(hydrogen, ia, 1.0, null, 0.0, null, 0.0, 0);
                        break;
                    default:
                        logger.info(" Check biotype for hydrogen " + atomName + ".");
                        patched = false;
                    }
                    break;
                case 2:
                    switch (numBonds) {
                    case 1:
                        intxyz(hydrogen, ia, 1.0, ib, 120.0, null, 0.0, 0);
                        break;
                    case 0:
                        intxyz(hydrogen, ia, 1.0, null, 0.0, null, 0.0, 0);
                        break;
                    default:
                        logger.info(" Check biotype for hydrogen " + atomName + ".");
                        patched = false;
                    }
                    break;
                case 1:
                    switch (numBonds) {
                    case 0:
                        intxyz(hydrogen, ia, 1.0, null, 0.0, null, 0.0, 0);
                        break;
                    default:
                        logger.info(" Check biotype for hydrogen " + atomName + ".");
                        patched = false;
                    }
                    break;
                default:
                    logger.info(" Check biotype for hydrogen " + atomName + ".");
                    patched = false;
                }
                if (!patched) {
                    break;
                } else {
                    buildBond(ia, hydrogen);
                }
            }
        }
        if (!patched) {
            logger.log(Level.WARNING, format(" Deleting unrecognized molecule %s.", m.toString()));
            activeMolecularAssembly.deleteMolecule((Molecule) m);
        } else {
            logger.info(" Patch for " + moleculeName + " succeeded.");
        }
    }
}

From source file:org.opendatakit.aggregate.parser.FormParserForJavaRosa.java

private void guardedInitHelper(MultiPartFormData uploadedFormItems, MultiPartFormItem xformXmlData,
        String incomingFormXml, String persistenceStoreFormId, StringBuilder warnings, CallingContext cc)
        throws ODKDatastoreException, ODKFormAlreadyExistsException, ODKParseException,
        ODKIncompleteSubmissionData {/*from   w  w  w  . ja v  a2  s  .co  m*/
    // ///////////////
    // Step 1: create or fetch the Form (FormInfo) submission
    //
    // This allows us to delete the form if upload goes bad...
    // form downloads are immediately enabled unless the upload specifies
    // that they shouldn't be.
    String isIncompleteFlag = uploadedFormItems.getSimpleFormField(ServletConsts.TRANSFER_IS_INCOMPLETE);
    boolean isDownloadEnabled = (isIncompleteFlag == null || isIncompleteFlag.trim().length() == 0);

    /* true if newly created */
    boolean newlyCreatedXForm = false;
    /* true if we are modifying this form definition. */
    boolean updateForm;
    /* true if the form definition changes, but is compatible */
    @SuppressWarnings("unused")
    boolean differentForm = false;
    IForm formInfo = null;
    /*
     * originationGraceTime: if a previously loaded form was last updated prior
     * to the originationGraceTime, then require a version change if the new
     * form is not identical (is changed).
     */
    Date originationGraceTime = new Date(System.currentTimeMillis() - FIFTEEN_MINUTES_IN_MILLISECONDS);
    /*
     * originationTime: the time of the form's first upload to the system
     */
    Date originationTime;
    try {
        formInfo = FormFactory.retrieveFormByFormId(rootElementDefn.formId, cc);

        // formId matches...
        Boolean thisIsEncryptedForm = formInfo.isEncryptedForm();
        if (thisIsEncryptedForm == null) {
            thisIsEncryptedForm = false;
        }

        if (isFileEncryptedForm != thisIsEncryptedForm) {
            // they either both need to be encrypted, or both need to not be
            // encrypted...
            throw new ODKFormAlreadyExistsException(
                    "Form encryption status cannot be altered. Form Id must be changed.");
        }
        // isEncryptedForm matches...

        XFormParameters thisRootElementDefn = formInfo.getRootElementDefn();
        String thisTitle = formInfo.getViewableName();
        String thisMd5Hash = formInfo.getMd5HashFormXml(cc);
        String md5Hash = CommonFieldsBase.newMD5HashUri(incomingFormXml);

        boolean same = thisRootElementDefn.equals(rootElementDefn)
                && (thisMd5Hash == null || md5Hash.equals(thisMd5Hash));

        if (same) {
            // version matches
            if (thisMd5Hash == null) {
                // IForm record does not have any attached form definition XML
                // attach it, set the title, and flag the form as updating
                // NOTE: this is an error path and not a normal flow
                updateFormXmlVersion(formInfo, incomingFormXml, rootElementDefn.modelVersion, cc);
                formInfo.setViewableName(title);
                updateForm = true;
                originationTime = new Date();
            } else {
                // The md5Hash of the form file being uploaded matches that
                // of a fully populated IForm record.
                // Do not allow changing the title...
                if (!title.equals(thisTitle)) {
                    throw new ODKFormAlreadyExistsException(
                            "Form title cannot be changed without updating the form version");
                }
                updateForm = false;
                String existingFormXml = formInfo.getFormXml(cc);
                // get the upload time of the existing form definition
                originationTime = FormParserForJavaRosa.xmlTimestamp(existingFormXml);
            }
        } else {
            String existingFormXml = formInfo.getFormXml(cc);
            // get the upload time of the existing form definition
            originationTime = FormParserForJavaRosa.xmlTimestamp(existingFormXml);

            if (FormParserForJavaRosa.xmlWithoutTimestampComment(incomingFormXml)
                    .equals(FormParserForJavaRosa.xmlWithoutTimestampComment(existingFormXml))) {
                // (version and file match).
                // The text of the form file being uploaded matches that of a
                // fully-populated IForm record once the ODK Aggregate
                // TimestampComment is removed.

                // Do not allow changing the title...
                if (!title.equals(thisTitle)) {
                    throw new ODKFormAlreadyExistsException(
                            "Form title cannot be changed without updating the form version.");
                }
                updateForm = false;

            } else {
                // file is different...

                // determine if the form is storage-equivalent and if version is
                // increasing...
                DifferenceResult diffresult = FormParserForJavaRosa.compareXml(this, existingFormXml,
                        formInfo.getViewableName(), originationTime.after(originationGraceTime));
                if (diffresult == DifferenceResult.XFORMS_DIFFERENT) {
                    // form is not storage-compatible
                    throw new ODKFormAlreadyExistsException();
                }
                if (diffresult == DifferenceResult.XFORMS_MISSING_VERSION) {
                    throw new ODKFormAlreadyExistsException(
                            "Form definition file has changed but does not specify a form version.  Update the form version and resubmit.");
                }
                if (diffresult == DifferenceResult.XFORMS_EARLIER_VERSION) {
                    throw new ODKFormAlreadyExistsException(
                            "Form version is not lexically greater than existing form version.  Update the form version and resubmit.");
                }

                // update the title and form definition file as needed...
                if (!thisTitle.equals(title)) {
                    formInfo.setViewableName(title);
                }

                updateFormXmlVersion(formInfo, incomingFormXml, rootElementDefn.modelVersion, cc);

                // mark this as a different form...
                differentForm = true;
                updateForm = true;
                originationTime = new Date();
            }
        }
    } catch (ODKFormNotFoundException e) {
        // form is not found -- create it
        formInfo = FormFactory.createFormId(incomingFormXml, rootElementDefn, isFileEncryptedForm,
                isDownloadEnabled, title, cc);
        updateForm = false;
        newlyCreatedXForm = true;
        originationTime = new Date();
    }

    // and upload all the media files associated with the form.
    // Allow updates if the form version has changed (updateForm is true)
    // or if the originationTime is after the originationGraceTime
    // e.g., the form version was changed within the last 15 minutes.

    boolean allowUpdates = updateForm || originationTime.after(originationGraceTime);

    // If an update is attempted and we don't allow updates,
    // throw an ODKFormAlreadyExistsException
    // NOTE: we store new files during this process, in the
    // expectation that the user simply forgot to update the
    // version and will do so shortly and upload that revised
    // form.
    Set<Map.Entry<String, MultiPartFormItem>> fileSet = uploadedFormItems.getFileNameEntrySet();
    for (Map.Entry<String, MultiPartFormItem> itm : fileSet) {
        if (itm.getValue() == xformXmlData)
            continue;// ignore the xform -- stored above.

        // update the images if the form version changed, otherwise throw an
        // error.

        if (itm.getValue().getFilename().contains("settings")) {
            log.info("Adding settings: " + itm.getValue().getFilename());
            if (formInfo.setSettingsFile(itm.getValue(), allowUpdates, cc)) {
                // needed update
                if (!allowUpdates) {
                    // but we didn't update the form...
                    throw new ODKFormAlreadyExistsException(
                            "Form settings file(s) have changed.  Please update the form version and resubmit.");
                }
            }
            continue;
        }

        if (formInfo.setXFormMediaFile(itm.getValue(), allowUpdates, cc)) {
            // needed update
            if (!allowUpdates) {
                // but we didn't update the form...
                throw new ODKFormAlreadyExistsException(
                        "Form media file(s) have changed.  Please update the form version and resubmit.");
            }
        }
    }
    // NOTE: because of caching, we only update the form definition file at
    // intervals of no more than every 3 seconds. So if you upload a
    // media file, then immediately upload an altered version, we don't
    // necessarily increment the uiVersion.

    // Determine the information about the submission...
    formInfo.setIsComplete(true);
    formInfo.persist(cc);
    formInfo.setAccessEntry(cc);

    Datastore ds = cc.getDatastore();
    User user = cc.getCurrentUser();

    FormDefinition fdDefined = null;
    try {
        fdDefined = FormDefinition.getFormDefinition(submissionElementDefn.formId, cc);
    } catch (IllegalStateException e) {
        e.printStackTrace();
        throw new ODKFormAlreadyExistsException(
                "Internal error: the form already exists but has a bad form definition.  Delete it.");
    }
    if (fdDefined != null) {
        // get most recent form-deletion statuses
        if (newlyCreatedXForm) {
            throw new ODKFormAlreadyExistsException(
                    "Internal error: Completely new file has pre-existing form definition");
        }
        // we're done -- updated the file and media; form definition doesn't need
        // updating.
        return;
    }

    // we don't have an existing form definition
    // -- create a submission association table entry mapping to what will
    // be
    // the model.
    // -- then create the model and iterate on manifesting it in the
    // database.
    SubmissionAssociationTable sa = SubmissionAssociationTable
            .assertSubmissionAssociation(formInfo.getKey().getKey(), submissionElementDefn.formId, cc);
    fdmSubmissionUri = sa.getUriSubmissionDataModel();

    // so we have the formInfo record, but no data model backing it.
    // Find the submission associated with this form...

    final List<FormDataModel> fdmList = new ArrayList<FormDataModel>();

    // List of successfully asserted relations.
    // Use a HashMap<String,CommonFieldsBase>(). This allows us to
    // use the tableKey(CommonFieldsBase) (schema name + table name)
    // to identify the successfully asserted relations, rather than
    // the object identity of the CommonFieldsBase objects, which is
    // inappropriate for our use.
    final HashMap<String, CommonFieldsBase> assertedRelations = new HashMap<String, CommonFieldsBase>();

    try {
        // ////////////////////////////////////////////////
        // Step 2: Now build up the parse tree for the form...
        //
        final FormDataModel fdm = FormDataModel.assertRelation(cc);

        // we haven't actually constructed the fdm record yet, so use the
        // relation when creating the entity key...
        final EntityKey k = new EntityKey(fdm, fdmSubmissionUri);

        NamingSet opaque = new NamingSet();

        // construct the data model with table and column placeholders.
        // assumes that the root is a non-repeating group element.
        final String tableNamePlaceholder = opaque.getTableName(fdm.getSchemaName(), persistenceStoreFormId, "",
                "CORE");

        constructDataModel(opaque, k, fdmList, fdm, k.getKey(), 1, persistenceStoreFormId, "",
                tableNamePlaceholder, submissionElement, warnings, cc);

        // find a good set of names...
        // this also ensures that the table names don't overlap existing
        // tables
        // in the datastore.
        opaque.resolveNames(ds, user);

        // debug output
        // for ( FormDataModel m : fdmList ) {
        // m.print(System.out);
        // }

        // and revise the data model with those names...
        for (FormDataModel m : fdmList) {
            String tablePlaceholder = m.getPersistAsTable();
            if (tablePlaceholder == null)
                continue;

            String columnPlaceholder = m.getPersistAsColumn();

            String tableName = opaque.resolveTablePlaceholder(tablePlaceholder);
            String columnName = opaque.resolveColumnPlaceholder(tablePlaceholder, columnPlaceholder);

            m.setPersistAsColumn(columnName);
            m.setPersistAsTable(tableName);
        }

        // ///////////////////////////////////////////
        // Step 3: create the backing tables...
        //
        // OK. At this point, the construction gets a bit ugly.
        // We need to handle the possibility that the table
        // needs to be split into phantom tables.
        // That happens if the table exceeds the maximum row
        // size for the persistence layer.

        // we do this by constructing the form definition from the fdmList
        // and then testing for successful creation of each table it
        // defines.
        // If that table cannot be created, we subdivide it, rearranging
        // the structure of the fdmList. Repeat until no errors.
        // Very error prone!!!
        //
        FormDefinition fd = null;
        try {
            int nAttempts = 0;
            for (;;) {
                // place a limit on this process
                if (++nAttempts > MAX_FORM_CREATION_ATTEMPTS) {
                    log.error("Aborting form-creation due to fail-safe limit (" + MAX_FORM_CREATION_ATTEMPTS
                            + " attempts)!");
                    throw new ODKParseException("Unable to create form data tables after "
                            + MAX_FORM_CREATION_ATTEMPTS + " attempts.");
                }

                fd = new FormDefinition(sa, submissionElementDefn.formId, fdmList, cc);

                List<CommonFieldsBase> badTables = new ArrayList<CommonFieldsBase>();

                for (CommonFieldsBase tbl : fd.getBackingTableSet()) {
                    try {
                        // patch up tbl with desired lengths of string
                        // fields...
                        for (FormDataModel m : fdmList) {
                            if (m.getElementType().equals(ElementType.STRING)) {
                                DataField f = m.getBackingKey();
                                Integer i = fieldLengths.get(m);
                                if (f != null && i != null) {
                                    f.setMaxCharLen(new Long(i));
                                }
                            }
                        }

                        // CommonFieldsBase objects are re-constructed with each
                        // call to new FormDefinition(...). We need to ensure the
                        // datastore contains the table that each of these objects
                        // refers to.
                        //
                        // Optimization:
                        //
                        // If assertedRelations contains a hit for tableKey(tbl),
                        // then we can assume that the table exists in the datastore
                        // and just update the CommonFieldsBase object in the
                        // assertedRelations map.
                        //
                        // Otherwise, we need to see if we can create it.
                        //
                        // Later on, before we do any more work, we need to
                        // sweep through and call ds.assertRelation() to ensure
                        // that all the CommonFieldsBase objects are fully
                        // initialized (because we don't really know what is
                        // done in the persistence layers during the
                        // ds.assertRelation() call).
                        //
                        if (assertedRelations.containsKey(tableKey(tbl))) {
                            assertedRelations.put(tableKey(tbl), tbl);
                        } else {
                            ds.assertRelation(tbl, user);
                            assertedRelations.put(tableKey(tbl), tbl);
                        }
                    } catch (Exception e1) {
                        // assume it is because the table is too wide...
                        log.warn("Create failed -- assuming phantom table required " + tableKey(tbl)
                                + " Exception: " + e1.toString());
                        // we expect the following dropRelation to fail,
                        // as the most likely state of the system is
                        // that the table was unable to be created.
                        try {
                            ds.dropRelation(tbl, user);
                        } catch (Exception e2) {
                            // no-op
                        }
                        if ((tbl instanceof DynamicBase) || (tbl instanceof TopLevelDynamicBase)) {
                            /* we know how to subdivide these -- we can recover from this */
                            badTables.add(tbl);
                        } else {
                            /* there must be something amiss with the database... */
                            throw e1;
                        }
                    }
                }

                for (CommonFieldsBase tbl : badTables) {
                    // dang. We need to create phantom tables...
                    orderlyDivideTable(fdmList, FormDataModel.assertRelation(cc), tbl, opaque, cc);
                }

                if (badTables.isEmpty()) {
                    // OK. We created everything and have no re-work.
                    //
                    // Since this might be the N'th time through this
                    // loop, we may have incompletely initialized the
                    // CommonFieldsBase entries in the assertedRelations map.
                    //
                    // Go through that now, asserting each relation.
                    // This ensures that all those entries are
                    // properly initialized.
                    //
                    // Since this was once successful, it should still be.
                    // If it isn't then any database error thrown is
                    // not recoverable.
                    for (CommonFieldsBase tbl : assertedRelations.values()) {
                        ds.assertRelation(tbl, user);
                    }
                    break;
                }

                /*
                 * reset the derived fields so that the FormDefinition construction
                 * will work.
                 */
                for (FormDataModel m : fdmList) {
                    m.resetDerivedFields();
                }
            }
        } catch (Exception e) {
            /*
             * either something is amiss in the database or there was some sort of
             * internal error. Try to drop all the successfully created database
             * tables.
             */
            try {
                log.warn("Aborting form-creation do to exception: " + e.toString()
                        + ". Datastore exceptions are expected in the following stack trace; other exceptions may indicate a problem:");
                e.printStackTrace();

                /* if everything were OK, assertedRelations should be empty... */
                if (!assertedRelations.isEmpty()) {
                    log.error("assertedRelations not fully unwound!");
                    Iterator<Entry<String, CommonFieldsBase>> iter = assertedRelations.entrySet().iterator();
                    while (iter.hasNext()) {
                        Entry<String, CommonFieldsBase> entry = iter.next();
                        CommonFieldsBase tbl = entry.getValue();
                        try {
                            log.error("--dropping " + entry.getKey());
                            ds.dropRelation(tbl, user);
                        } catch (Exception e3) {
                            log.error("--Exception while dropping " + entry.getKey() + " exception: "
                                    + e3.toString());
                            // do nothing...
                            e3.printStackTrace();
                        }
                        // we tried our best... twice.
                        // Remove the definition whether
                        // or not we were successful.
                        // No point in ever trying again.
                        iter.remove();
                    }
                }

                // scorched earth -- get all the tables and try to drop them all...
                if (fd != null) {
                    for (CommonFieldsBase tbl : fd.getBackingTableSet()) {
                        try {
                            ds.dropRelation(tbl, user);
                            assertedRelations.remove(tableKey(tbl));
                        } catch (Exception e3) {
                            // the above may fail because the table was never created...
                            // do nothing...
                            log.warn(
                                    "If the following stack trace is not a complaint about a table not existing, it is likely a problem!");
                            e3.printStackTrace();
                        }
                    }
                }
            } catch (Exception e4) {
                // just log error... popping out to original exception
                log.error("dropping of relations unexpectedly failed with exception: " + e4.toString());
                e4.printStackTrace();
            }
            throw new ODKParseException("Error processing new form: " + e.toString());
        }
        // TODO: if the above gets killed, how do we clean up?
    } catch (ODKParseException e) {
        formInfo.deleteForm(cc);
        throw e;
    } catch (ODKDatastoreException e) {
        formInfo.deleteForm(cc);
        throw e;
    }

    // ////////////////////////////////////////////
    // Step 4: record the data model...
    //
    // if we get here, we were able to create the tables -- record the
    // form description....
    ds.putEntities(fdmList, user);

    // TODO: if above write fails, how do we clean this up?

    // and update the complete flag to indicate that upload was fully
    // successful.
    sa.setIsPersistenceModelComplete(true);
    ds.putEntity(sa, user);
    // And wait until the data is propagated across all server instances.
    //
    // Rather than relying on MemCache, we insert this delay here so that
    // any caller that is creating a form can know that the form definition
    // has been propagated across the front-ends (subject to fast/slow
    // clocks).
    // This assumes that server clock rates never cause drifts of more than
    // the
    // network transmission latency between the requester and the server
    // over
    // the PersistConsts.MAX_SETTLE_MILLISECONDS time period.
    //
    // After this delay interval, the caller can be confident that the form
    // is visible by whatever server receives the caller's next request
    // (and this is also true during unit tests).
    try {
        Thread.sleep(PersistConsts.MAX_SETTLE_MILLISECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:structuredPredictionNLG.SFX.java

/**
 * Populates the predicate, attribute, attribute/value pair, and value alignment collections
 * @param dataFile The dataset file./*w w  w. j av a 2 s  .c o m*/
 */
public void createLists(File dataFile) {
    try {
        // Initialize the collections
        setPredicates(new ArrayList<>());
        setAttributes(new HashMap<>());
        setAttributeValuePairs(new HashMap<>());
        setValueAlignments(new HashMap<>());

        // Obtain the dataset portion of the file
        String dataPart = new String();
        boolean begin = false;
        try (BufferedReader br = new BufferedReader(new FileReader(dataFile))) {
            String s;
            while ((s = br.readLine()) != null) {
                if (s.startsWith("[")) {
                    begin = true;
                }
                if (begin) {
                    dataPart += s;
                }
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Bagel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Bagel.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Parse the dataset with JSON
        JSONArray overArray = new JSONArray(dataPart);
        for (int o = 0; o < overArray.length(); o++) {
            // "dial" notes each seperate dialog
            JSONArray arr = overArray.getJSONObject(o).getJSONArray("dial");
            for (int i = 0; i < arr.length(); i++) {
                String MRstr;
                String ref;
                // "dact" notes every meaning representation
                MRstr = arr.getJSONObject(i).getJSONObject("S").getString("dact");
                // "ref" notes every corresponding reference
                ref = arr.getJSONObject(i).getJSONObject("S").getString("ref").replaceAll("-s", "s");

                //We split some composite words (based on Wen et al's (2016) code)
                ref = (" " + ref + " ").replaceAll(" it's ", " it is ").replaceAll(" don't ", " do not ")
                        .replaceAll(" doesn't ", " does not ").replaceAll(" didn't ", " did not ")
                        .replaceAll(" you'd ", " you would ").replaceAll(" you're ", " you are ")
                        .replaceAll(" you'll ", " you will ").replaceAll(" i'm ", " i am ")
                        .replaceAll(" they're ", " they are ").replaceAll(" that's ", " that is ")
                        .replaceAll(" what's ", " what is ").replaceAll(" couldn't ", " could not ")
                        .replaceAll(" i've ", " i have ").replaceAll(" we've ", " we have ")
                        .replaceAll(" can't ", " cannot ").replaceAll(" i'd ", " i would ")
                        .replaceAll(" i'd ", " i would ").replaceAll(" aren't ", " are not ")
                        .replaceAll(" isn't ", " is not ").replaceAll(" wasn't ", " was not ")
                        .replaceAll(" weren't ", " were not ").replaceAll(" won't ", " will not ")
                        .replaceAll(" there's ", " there is ").replaceAll(" there're ", " there are ")
                        .replaceAll(" \\. \\. ", " \\. ").replaceAll(" restaurants ", " restaurant -s ")
                        .replaceAll(" hotels ", " hotel -s ").replaceAll(" laptops ", " laptop -s ")
                        .replaceAll(" cheaper ", " cheap -er ").replaceAll(" dinners ", " dinner -s ")
                        .replaceAll(" lunches ", " lunch -s ").replaceAll(" breakfasts ", " breakfast -s ")
                        .replaceAll(" expensively ", " expensive -ly ")
                        .replaceAll(" moderately ", " moderate -ly ").replaceAll(" cheaply ", " cheap -ly ")
                        .replaceAll(" prices ", " price -s ").replaceAll(" places ", " place -s ")
                        .replaceAll(" venues ", " venue -s ").replaceAll(" ranges ", " range -s ")
                        .replaceAll(" meals ", " meal -s ").replaceAll(" locations ", " location -s ")
                        .replaceAll(" areas ", " area -s ").replaceAll(" policies ", " policy -s ")
                        .replaceAll(" children ", " child -s ").replaceAll(" kids ", " kid -s ")
                        .replaceAll(" kidfriendly ", " kid friendly ").replaceAll(" cards ", " card -s ")
                        .replaceAll(" st ", " street ").replaceAll(" ave ", " avenue ")
                        .replaceAll(" upmarket ", " expensive ").replaceAll(" inpricey ", " cheap ")
                        .replaceAll(" inches ", " inch -s ").replaceAll(" uses ", " use -s ")
                        .replaceAll(" dimensions ", " dimension -s ")
                        .replaceAll(" driverange ", " drive range ").replaceAll(" includes ", " include -s ")
                        .replaceAll(" computers ", " computer -s ").replaceAll(" machines ", " machine -s ")
                        .replaceAll(" ecorating ", " eco rating ").replaceAll(" families ", " family -s ")
                        .replaceAll(" ratings ", " rating -s ").replaceAll(" constraints ", " constraint -s ")
                        .replaceAll(" pricerange ", " price range ")
                        .replaceAll(" batteryrating ", " battery rating ")
                        .replaceAll(" requirements ", " requirement -s ").replaceAll(" drives ", " drive -s ")
                        .replaceAll(" specifications ", " specification -s ")
                        .replaceAll(" weightrange ", " weight range ").replaceAll(" harddrive ", " hard drive ")
                        .replaceAll(" batterylife ", " battery life ")
                        .replaceAll(" businesses ", " business -s ").replaceAll(" hours ", " hour -s ")
                        .replaceAll(" accessories ", " accessory -s ").replaceAll(" ports ", " port -s ")
                        .replaceAll(" televisions ", " television -s ")
                        .replaceAll(" restrictions ", " restriction -s ")
                        .replaceAll(" extremely ", " extreme -ly ").replaceAll(" actually ", " actual -ly ")
                        .replaceAll(" typically ", " typical -ly ").replaceAll(" drivers ", " driver -s ")
                        .replaceAll(" teh ", " the ").replaceAll(" definitely ", " definite -ly ")
                        .replaceAll(" factors ", " factor -s ").replaceAll(" truly ", " true -ly ")
                        .replaceAll(" mostly ", " most -ly ").replaceAll(" nicely ", " nice -ly ")
                        .replaceAll(" surely ", " sure -ly ").replaceAll(" certainly ", " certain -ly ")
                        .replaceAll(" totally ", " total -ly ").replaceAll(" \\# ", " number ")
                        .replaceAll(" \\& ", " and ").replaceAll(" avenue ", " ave ").replaceAll(" -s ", " s ")
                        .trim();

                // If the MR concerns one of the following predicates, and a ref is available
                if ((MRstr.startsWith("inform(") || MRstr.startsWith("inform_only")
                        || MRstr.startsWith("inform_no_match(") || MRstr.startsWith("?confirm(")
                        || MRstr.startsWith("?select(") || MRstr.startsWith("?request(")
                        || MRstr.startsWith("?reqmore(") || MRstr.startsWith("goodbye(")) && !ref.isEmpty()) {
                    // Obtain the predicate
                    String predicate = MRstr.substring(0, MRstr.indexOf('('));
                    if (!getPredicates().contains(predicate) && predicate != null) {
                        getPredicates().add(predicate);

                        if (!getAttributes().containsKey(predicate)) {
                            getAttributes().put(predicate, new HashSet<String>());
                        }
                        if (!getDatasetInstances().containsKey(predicate)) {
                            getDatasetInstances().put(predicate, new ArrayList<DatasetInstance>());
                        }
                    }

                    // Obtain the attributes
                    String attributesStr = MRstr.substring(MRstr.indexOf('(') + 1, MRstr.length() - 1);
                    HashMap<String, HashSet<String>> attributeValues = new HashMap<>();
                    // Track the indexes used for variables identifiers (seperately for each attribute)
                    HashMap<String, Integer> attrXIndeces = new HashMap<>();
                    if (!attributesStr.isEmpty()) {
                        // Parse the attributes and their values
                        String[] args = attributesStr.split(";");
                        for (String arg : args) {
                            String attr;
                            String value = "";
                            // If the attribute has corresponding values
                            if (arg.contains("=")) {
                                String[] subAttr = arg.split("=");
                                value = subAttr[1].toLowerCase();
                                attr = subAttr[0].toLowerCase().replaceAll("_", "");

                                if (value.startsWith("\'")) {
                                    value = value.substring(1, value.length() - 1);
                                }
                                // Normalize some closed set values
                                if (value.equals("true")) {
                                    value = "yes";
                                }
                                if (value.equals("false")) {
                                    value = "no";
                                }
                                if (value.equals("dontcare")) {
                                    value = "dont_care";
                                }
                                if ((" " + value + " ").contains(" avenue ")) {
                                    value = (" " + value + " ").replace(" avenue ", " ave ").trim();
                                }
                                // Treat these values as seperate attributes since they are expressed quite differently
                                if (value.equals("no") || value.equals("yes") || value.equals("yes or no")
                                        || value.equals("none") || value.equals("empty")) {
                                    attr += "_" + value.replaceAll(" ", "_");
                                    value = attr;
                                }
                                // Treat "dont_care" instances, as if "dont_care" is the attribute, and the original attribute is the value
                                // We do this because the phrasing is very similar between different "dont_care" realizations
                                if (value.equals("dont_care")) {
                                    String v = value;
                                    value = attr;
                                    attr = v;
                                }
                            } else {
                                attr = arg.replaceAll("_", "");
                            }
                            if (!getAttributes().get(predicate).contains(attr)) {
                                getAttributes().get(predicate).add(attr);
                            }
                            if (!attributeValues.containsKey(attr)) {
                                attributeValues.put(attr, new HashSet<String>());
                            }
                            // If the attribute has no corresponding value, we encode it by using the attibute identifier as the value
                            if (value.isEmpty()) {
                                value = attr;
                            }

                            // If the value is a variable, we name it as {@X@ + attribute identifier + variable index (for this attribute)}
                            // This occurs when values are already set as variables in the MR, before any delexicalization happens
                            if (value.toLowerCase().startsWith("x")) {
                                int index = 0;
                                if (!attrXIndeces.containsKey(attr)) {
                                    attrXIndeces.put(attr, 1);
                                } else {
                                    index = attrXIndeces.get(attr);
                                    attrXIndeces.put(attr, index + 1);
                                }
                                value = "x" + index;
                            }
                            attributeValues.get(attr).add(value.trim().toLowerCase());
                        }
                    }

                    // Delexicalizing the attribute/value pairs
                    HashMap<String, HashSet<String>> delexicalizedAttributeValues = new HashMap<>();
                    HashMap<String, HashMap<String, Integer>> attrValuePriorities = new HashMap<>();
                    int maximumPriority = 0;
                    /* Delixalization of values needs to happen incrementally with priority given to the values of greater lenth, to avoid overlap of values in the reference
                     * e.g. for the MR: inform{name="inn on castro", near="castro"}, with the reference "inn on castro is a nice restaurant",
                     *      we need to first align and delexicalize the "inn on castro" value, before the "castro" value 
                     *      (in this case because "castro" doesn't appear in the reference, but even if it appeared later the priorities would help align it with the correct one)
                    */
                    // We begin by determining which values may require delexicalization, and which not
                    for (String attr : attributeValues.keySet()) {
                        if (!attr.isEmpty()) {
                            delexicalizedAttributeValues.put(attr, new HashSet<String>());
                            attrValuePriorities.put(attr, new HashMap<String, Integer>());
                            for (String value : attributeValues.get(attr)) {
                                if (!value.equals("none") && !value.equals("empty") && !value.equals("yes")
                                        && !value.equals("yes or no") && !value.equals("no")
                                        && !value.equals(attr)) {
                                    // Initially priorities are given according to value order
                                    attrValuePriorities.get(attr).put(value, maximumPriority);
                                    maximumPriority++;
                                } else {
                                    // No delexicalization is needed here
                                    delexicalizedAttributeValues.get(attr).add(value);
                                }
                            }
                        }
                    }
                    // We shift the priorities of different values, according to their perspective lengths (i.e. longer values have higher priority)
                    boolean change = true;
                    while (change) {
                        change = false;
                        for (String attr1 : attrValuePriorities.keySet()) {
                            for (String value1 : attrValuePriorities.get(attr1).keySet()) {
                                for (String attr2 : attrValuePriorities.keySet()) {
                                    for (String value2 : attrValuePriorities.get(attr2).keySet()) {
                                        if (!value1.equals(value2) && value1.contains(value2)
                                                && attrValuePriorities.get(attr1).get(
                                                        value1) > attrValuePriorities.get(attr2).get(value2)) {
                                            int prio1 = attrValuePriorities.get(attr1).get(value1);
                                            int prio2 = attrValuePriorities.get(attr2).get(value2);
                                            attrValuePriorities.get(attr1).put(value1, prio2);
                                            attrValuePriorities.get(attr2).put(value2, prio1);
                                            change = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // Map between variables and their lexicalized values, required for relexicalization during postprocessing after the sentence generation
                    HashMap<String, String> delexicalizationMap = new HashMap<>();
                    ref = " " + ref + " ";
                    // Delexicalization occurs, in order of priority
                    for (int priority = 0; priority < maximumPriority; priority++) {
                        for (String attr : attrValuePriorities.keySet()) {
                            if (!attrXIndeces.containsKey(attr)) {
                                attrXIndeces.put(attr, 0);
                            }
                            for (String value : attrValuePriorities.get(attr).keySet()) {
                                if (attrValuePriorities.get(attr).get(value) == priority) {
                                    // If the value doesn't appear verbatim in the reference, and the value is not composed of multiple subvalues (i.e. doesn't contain connectives)
                                    if (!ref.contains(" " + value + " ") && !value.contains(" and ")
                                            && !value.contains(" or ")) {
                                        if (value.equals("restaurant") && ref.contains(" place ")) {
                                            ref = ref.replace(" place ", " " + Action.TOKEN_X + attr + "_"
                                                    + attrXIndeces.get(attr) + " ");
                                            ref = ref.replaceAll("  ", " ");
                                            delexicalizedAttributeValues.get(attr)
                                                    .add(Action.TOKEN_X + attr + "_" + attrXIndeces.get(attr));
                                            delexicalizationMap.put(
                                                    Action.TOKEN_X + attr + "_" + attrXIndeces.get(attr),
                                                    "place");
                                            attrXIndeces.put(attr, attrXIndeces.get(attr) + 1);
                                        } else {
                                            delexicalizedAttributeValues.get(attr).add(value);
                                        }
                                        // If the value doesn't appear verbatim in the reference, but the value is composed of multiple sub-values
                                    } else if (!ref.contains(" " + value + " ")
                                            && (value.contains(" and ") || value.contains(" or "))) {
                                        // We first check if the value appears verbatim when we switch "and" with "or" and vice versa
                                        // We do this due to some inconsistencies in the dataset on how conjuctions are treated
                                        String tempValue = value;
                                        if (value.contains(" and ")) {
                                            tempValue = value.replace(" and ", " or ");
                                        } else if (value.contains(" or ")) {
                                            tempValue = value.replace(" or ", " and ");
                                        }

                                        if (ref.contains(" " + tempValue + " ")) {
                                            ref = ref.replace(" " + tempValue + " ", " " + Action.TOKEN_X + attr
                                                    + "_" + attrXIndeces.get(attr) + " ");
                                            ref = ref.replaceAll("  ", " ");
                                            delexicalizedAttributeValues.get(attr)
                                                    .add(Action.TOKEN_X + attr + "_" + attrXIndeces.get(attr));
                                            delexicalizationMap.put(
                                                    Action.TOKEN_X + attr + "_" + attrXIndeces.get(attr),
                                                    value);
                                            attrXIndeces.put(attr, attrXIndeces.get(attr) + 1);
                                        } else {
                                            // We split the conjunction into the seperate values; so far the code supports only 2 sub-values
                                            String[] values = new String[2];
                                            if (value.contains(" and ")) {
                                                values = value.split(" and ");
                                            } else if (value.contains(" or ")) {
                                                values = value.split(" or ");
                                            }
                                            // And check if the conjunction appears verbatim when we switch the position of the sub-values
                                            String newValue1 = values[1] + " and " + values[0];
                                            String newValue2 = values[1] + " or " + values[0];
                                            if (ref.contains(" " + newValue1 + " ")) {
                                                ref = ref.replace(" " + newValue1 + " ", " " + Action.TOKEN_X
                                                        + attr + "_" + attrXIndeces.get(attr) + " ");
                                                ref = ref.replaceAll("  ", " ");
                                                delexicalizedAttributeValues.get(attr).add(
                                                        Action.TOKEN_X + attr + "_" + attrXIndeces.get(attr));
                                                delexicalizationMap.put(
                                                        Action.TOKEN_X + attr + "_" + attrXIndeces.get(attr),
                                                        value);
                                                attrXIndeces.put(attr, attrXIndeces.get(attr) + 1);
                                            } else if (ref.contains(" " + newValue2 + " ")) {
                                                ref = ref.replace(" " + newValue2 + " ", " " + Action.TOKEN_X
                                                        + attr + "_" + attrXIndeces.get(attr) + " ");
                                                ref = ref.replaceAll("  ", " ");
                                                delexicalizedAttributeValues.get(attr).add(
                                                        Action.TOKEN_X + attr + "_" + attrXIndeces.get(attr));
                                                delexicalizationMap.put(
                                                        Action.TOKEN_X + attr + "_" + attrXIndeces.get(attr),
                                                        value);
                                                attrXIndeces.put(attr, attrXIndeces.get(attr) + 1);
                                            }
                                        }
                                        // If the value appears verbatim in the reference, delexicalize it
                                    } else {
                                        ref = ref.replace(" " + value + " ", " " + Action.TOKEN_X + attr + "_"
                                                + attrXIndeces.get(attr) + " ");
                                        ref = ref.replaceAll("  ", " ");
                                        delexicalizedAttributeValues.get(attr)
                                                .add(Action.TOKEN_X + attr + "_" + attrXIndeces.get(attr));
                                        delexicalizationMap.put(
                                                Action.TOKEN_X + attr + "_" + attrXIndeces.get(attr), value);
                                        attrXIndeces.put(attr, attrXIndeces.get(attr) + 1);
                                    }
                                }
                            }
                        }
                    }
                    ref = ref.trim();

                    // We construct the MeaningRepresentation
                    MeaningRepresentation MR = new MeaningRepresentation(predicate,
                            delexicalizedAttributeValues, MRstr, delexicalizationMap);

                    // Sequences of attribute/values pairs and words in the order we observe this in the reference
                    ArrayList<String> observedAttrValueSequence = new ArrayList<>();
                    ArrayList<String> observedWordSequence = new ArrayList<>();

                    // The observed word sequence does not include punctuation
                    String[] words = ref.replaceAll("([,.?!;:'])", " $1").split(" ");

                    // We construct the observed word sequence (and fix some orthographical errors along the way)
                    for (int w = 0; w < words.length; w++) {
                        if (!words[w].trim().isEmpty()) {
                            if (!words[w].trim().isEmpty()
                                    && (observedWordSequence.isEmpty() || !words[w].trim().equals(
                                            observedWordSequence.get(observedWordSequence.size() - 1)))) {
                                if (words[w].trim().equals("s") && (observedWordSequence
                                        .get(observedWordSequence.size() - 1).equals("child"))) {
                                    observedWordSequence.set(observedWordSequence.size() - 1, "children");
                                } else if (words[w].trim().equals("addres")
                                        || words[w].trim().equals("adress")) {
                                    observedWordSequence.add("address");
                                } else if (words[w].trim().equals("mathch")) {
                                    observedWordSequence.add("match");
                                } else if (words[w].trim().equals("prefered")) {
                                    observedWordSequence.add("preferred");
                                } else if (words[w].trim().equals("relevent")) {
                                    observedWordSequence.add("relevant");
                                } else if (words[w].trim().equals("alloed")) {
                                    observedWordSequence.add("allowed");
                                } else if (words[w].trim().equals("avalible")
                                        || words[w].trim().equals("avalable")) {
                                    observedWordSequence.add("available");
                                } else if (words[w].trim().equals("tha") || words[w].trim().equals("te")) {
                                    observedWordSequence.add("the");
                                } else if (words[w].trim().equals("internect")) {
                                    observedWordSequence.add("internet");
                                } else if (words[w].trim().equals("wether")) {
                                    observedWordSequence.add("whether");
                                } else if (words[w].trim().equals("aplogize")) {
                                    observedWordSequence.add("apologize");
                                } else if (words[w].trim().equals("accomodations")) {
                                    observedWordSequence.add("accommodations");
                                } else if (words[w].trim().equals("whould")) {
                                    observedWordSequence.add("would");
                                } else if (words[w].trim().equals("aceepted")) {
                                    observedWordSequence.add("accepted");
                                } else if (words[w].trim().equals("postode")) {
                                    observedWordSequence.add("postcode");
                                } else if (words[w].trim().equals("ive")) {
                                    observedWordSequence.add("i");
                                    observedWordSequence.add("have");
                                } else if (words[w].trim().equals("waht")) {
                                    observedWordSequence.add("what");
                                } else if (words[w].trim().equals("neighborhood")) {
                                    observedWordSequence.add("neighbourhood");
                                } else if (words[w].trim().equals("prefernce")) {
                                    observedWordSequence.add("preference");
                                } else if (words[w].trim().equals("dont")) {
                                    observedWordSequence.add("don't");
                                } else if (words[w].trim().equals("isnt")) {
                                    observedWordSequence.add("isn't");
                                } else if (words[w].trim().equals("intenet")
                                        || words[w].trim().equals("internetn")) {
                                    observedWordSequence.add("internet");
                                } else if (words[w].trim().equals("cannote")) {
                                    observedWordSequence.add("cannot");
                                } else if (words[w].trim().equals("notels")) {
                                    observedWordSequence.add("hotels");
                                } else if (words[w].trim().equals("phne")) {
                                    observedWordSequence.add("phone");
                                } else if (words[w].trim().equals("taht")) {
                                    observedWordSequence.add("that");
                                } else if (words[w].trim().equals("postdocde")) {
                                    observedWordSequence.add("postcode");
                                } else if (words[w].trim().equals("accpects")) {
                                    observedWordSequence.add("accepts");
                                } else if (words[w].trim().equals("doesn") || words[w].trim().equals("doesnt")
                                        || words[w].trim().equals("doesn")) {
                                    observedWordSequence.add("doesn't");
                                } else if (words[w].trim().equals("restaurnats")) {
                                    observedWordSequence.add("restarnauts");
                                } else if (words[w].trim().equals("ther") || words[w].trim().equals("thers")) {
                                    observedWordSequence.add("there");

                                    // The dataset treats the suffixes "s" and "-ly" as separate words
                                    // We combine these suffixes with their preceding words but keep a cache with these changes to revert them before evaluation (we have to do this so that the token-based evaluation metrics are calculated in a consistent manner with Wen et al.'s)
                                } else if (words[w].trim().equals("s")) {
                                    if (observedWordSequence.isEmpty()) {
                                        observedWordSequence.add(words[w].trim().toLowerCase());
                                    } else if (observedWordSequence.get(observedWordSequence.size() - 1)
                                            .startsWith(Action.TOKEN_X)) {
                                        observedWordSequence.add(words[w].trim().toLowerCase());
                                    } else {
                                        getCompositeSuffixesInData().put(
                                                observedWordSequence.get(observedWordSequence.size() - 1) + "s",
                                                observedWordSequence.get(observedWordSequence.size() - 1)
                                                        + " s");
                                        observedWordSequence.set(observedWordSequence.size() - 1,
                                                observedWordSequence.get(observedWordSequence.size() - 1)
                                                        + "s");
                                    }
                                } else if (words[w].trim().equals("-ly")) {
                                    if (observedWordSequence.isEmpty()) {
                                        observedWordSequence.add(words[w].trim().toLowerCase());
                                    } else if (observedWordSequence.get(observedWordSequence.size() - 1)
                                            .startsWith(Action.TOKEN_X)) {
                                        observedWordSequence.add(words[w].trim().toLowerCase());
                                    } else {
                                        getCompositeSuffixesInData().put(
                                                observedWordSequence.get(observedWordSequence.size() - 1)
                                                        + "ly",
                                                observedWordSequence.get(observedWordSequence.size() - 1)
                                                        + " -ly");
                                        observedWordSequence.set(observedWordSequence.size() - 1,
                                                observedWordSequence.get(observedWordSequence.size() - 1)
                                                        + "ly");
                                    }
                                } else {
                                    observedWordSequence.add(words[w].trim().toLowerCase());
                                }
                            }
                        }
                    }

                    //Probably deprecated, need to do some more tests
                    MR.getAttributeValues().keySet().forEach((attr) -> {
                        MR.getAttributeValues().get(attr).stream()
                                .filter((value) -> (attr.equals("name") && value.equals("none")))
                                .forEachOrdered((value) -> {
                                    observedAttrValueSequence.add(0,
                                            attr.toLowerCase() + "=" + value.toLowerCase());
                                });
                    });
                    observedAttrValueSequence.add(Action.TOKEN_END);

                    // We store the maximum observed word sequence length, to use as a limit during generation
                    if (observedWordSequence.size() > getMaxWordSequenceLength()) {
                        setMaxWordSequenceLength(observedWordSequence.size());
                    }

                    // We initialize the alignments between words and attribute/value pairs
                    ArrayList<String> wordToAttrValueAlignment = new ArrayList<>();
                    // And populate them with "unaligned" tokens (i.e. "[]") and punctuation alignments; we do the latter so we know to filter out punctuation when estimating the alignments in later stages
                    observedWordSequence.forEach((word) -> {
                        if (word.trim().matches("[,.?!;:']")) {
                            wordToAttrValueAlignment.add(Action.TOKEN_PUNCT);
                        } else {
                            wordToAttrValueAlignment.add("[]");
                        }
                    });
                    // And using both word sequence and initial alignments, we construct a draft sequence of word actions corresponding to the reference
                    ArrayList<Action> directReferenceSequence = new ArrayList<>();
                    for (int r = 0; r < observedWordSequence.size(); r++) {
                        directReferenceSequence
                                .add(new Action(observedWordSequence.get(r), wordToAttrValueAlignment.get(r)));
                    }
                    // Finally, we construct the DatasetInstance
                    DatasetInstance DI = new DatasetInstance(MR, directReferenceSequence,
                            postProcessRef(MR, directReferenceSequence));
                    // We add the evaluation references of all previously constructed DatasetInstances (that are identical to this one) as available evaluation references 
                    getDatasetInstances().get(predicate).stream()
                            .filter((existingDI) -> (existingDI.getMeaningRepresentation().getAbstractMR()
                                    .equals(DI.getMeaningRepresentation().getAbstractMR())))
                            .map((existingDI) -> {
                                existingDI.getEvaluationReferences().addAll(DI.getEvaluationReferences());
                                return existingDI;
                            }).forEachOrdered((existingDI) -> {
                                // We add the direct reference of this DatasetInstance as an available evaluation reference to all previously constructed DatasetInstance that are identical to this one
                                DI.getEvaluationReferences().addAll(existingDI.getEvaluationReferences());
                            });
                    getDatasetInstances().get(predicate).add(DI);

                    // Calculate the possible alignments between (non-delexicalized) attribute values and reference subphrases
                    // We do this by comparing the values with n-gram subphrases of the reference, using character-level Levenshtein distance
                    // These are used during the estimation of naive alignments, but also for tracking which values have possibly been expressed during generation
                    HashMap<String, HashMap<String, Double>> observedValueAlignments = new HashMap<>();
                    MR.getAttributeValues().keySet().forEach((attr) -> {
                        MR.getAttributeValues().get(attr).stream()
                                .filter((value) -> (!value.equals("name=none")
                                        && !value.startsWith(Action.TOKEN_X)
                                        && !(value.matches("\"[xX][0-9]+\"") || value.matches("[xX][0-9]+"))))
                                .forEachOrdered((value) -> {
                                    String valueToCompare = value;
                                    if (value.equals("no") || value.equals("yes") || value.equals("yes or no")
                                            || value.equals("none") || value.equals("empty")) {
                                        // If the value is boolean or non-existant, we also compare using the attribute name
                                        valueToCompare = attr;
                                        observedValueAlignments.put(valueToCompare + ":" + value,
                                                new HashMap<String, Double>());
                                    } else {
                                        observedValueAlignments.put(valueToCompare,
                                                new HashMap<String, Double>());
                                    }
                                    //For all n-grams in the referenec
                                    for (int n = 1; n < observedWordSequence.size(); n++) {
                                        //Calculate the similaritie between them and valueToCompare
                                        for (int r = 0; r <= observedWordSequence.size() - n; r++) {
                                            boolean compareAgainstNGram = true;
                                            for (int j = 0; j < n; j++) {
                                                if (observedWordSequence.get(r + j).startsWith(Action.TOKEN_X)
                                                        || wordToAttrValueAlignment.get(r + j)
                                                                .equals(Action.TOKEN_PUNCT)
                                                        || StringNLPUtilities
                                                                .isArticle(observedWordSequence.get(r + j))
                                                        || observedWordSequence.get(r + j)
                                                                .equalsIgnoreCase("and")
                                                        || observedWordSequence.get(r + j)
                                                                .equalsIgnoreCase("or")) {
                                                    // We ignore n-grams that contain variables, punctuation, articles, or conjuctions
                                                    // In other words, we do not allow values to align with such n-grams
                                                    compareAgainstNGram = false;
                                                }
                                            }
                                            if (compareAgainstNGram) {
                                                String align = "";
                                                String compare = "";
                                                String backwardCompare = "";
                                                for (int j = 0; j < n; j++) {
                                                    // The coordinates of the alignment
                                                    align += (r + j) + " ";
                                                    compare += observedWordSequence.get(r + j);
                                                    backwardCompare = observedWordSequence.get(r + j)
                                                            + backwardCompare;
                                                }
                                                align = align.trim();

                                                // Calculate the character-level distance between the value and the nGram (in its original and reversed order)
                                                Double distance = Levenshtein.getSimilarity(
                                                        valueToCompare.toLowerCase(), compare.toLowerCase(),
                                                        true);
                                                Double backwardDistance = Levenshtein.getSimilarity(
                                                        valueToCompare.toLowerCase(),
                                                        backwardCompare.toLowerCase(), true);

                                                // We keep the best distance score; note that the Levenshtein distance is normalized so that greater is better 
                                                if (backwardDistance > distance) {
                                                    distance = backwardDistance;
                                                }
                                                // We ignore all nGrams that are less similar than a threshold
                                                if (distance > 0.3) {
                                                    if (value.equals("no") || value.equals("yes")
                                                            || value.equals("yes or no") || value.equals("none")
                                                            || value.equals("empty")) {
                                                        observedValueAlignments
                                                                .get(valueToCompare + ":" + value)
                                                                .put(align, distance);
                                                    } else {
                                                        observedValueAlignments.get(valueToCompare).put(align,
                                                                distance);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                });
                    });

                    // We filter out any values that haven't been aligned
                    HashSet<String> toRemove = new HashSet<>();
                    for (String value : observedValueAlignments.keySet()) {
                        if (observedValueAlignments.get(value).isEmpty()) {
                            toRemove.add(value);
                        }
                    }
                    for (String value : toRemove) {
                        observedValueAlignments.remove(value);
                    }

                    // We keep the best aligned nGrams; since we do not want the aligned nGrams to be overlapping, we remove any overlapping alignments after we pick each one
                    while (!observedValueAlignments.keySet().isEmpty()) {
                        // Find the best aligned nGram
                        Double max = Double.NEGATIVE_INFINITY;
                        String[] bestAlignment = new String[2];
                        for (String value : observedValueAlignments.keySet()) {
                            for (String alignment : observedValueAlignments.get(value).keySet()) {
                                if (observedValueAlignments.get(value).get(alignment) > max) {
                                    max = observedValueAlignments.get(value).get(alignment);
                                    bestAlignment[0] = value;
                                    bestAlignment[1] = alignment;
                                }
                            }
                        }

                        // Find the subphrase that corresponds to the best aligned nGram, according to the coordinates
                        ArrayList<String> alignedStr = new ArrayList<>();
                        String[] coords = bestAlignment[1].split(" ");
                        if (coords.length == 1) {
                            alignedStr.add(observedWordSequence.get(Integer.parseInt(coords[0].trim())));
                        } else {
                            for (int a = Integer.parseInt(coords[0].trim()); a <= Integer
                                    .parseInt(coords[coords.length - 1].trim()); a++) {
                                alignedStr.add(observedWordSequence.get(a));
                            }
                        }

                        // Store the best aligned nGram
                        if (!getValueAlignments().containsKey(bestAlignment[0])) {
                            getValueAlignments().put(bestAlignment[0],
                                    new HashMap<ArrayList<String>, Double>());
                        }
                        getValueAlignments().get(bestAlignment[0]).put(alignedStr, max);

                        // And remove it from the observed ones for this instance
                        observedValueAlignments.remove(bestAlignment[0]);
                        // And also remove any other aligned nGrams that are overlapping with the best aligned nGram
                        observedValueAlignments.keySet().forEach((value) -> {
                            HashSet<String> alignmentsToBeRemoved = new HashSet<>();
                            observedValueAlignments.get(value).keySet().forEach((alignment) -> {
                                String[] othCoords = alignment.split(" ");
                                if (Integer.parseInt(coords[0].trim()) <= Integer.parseInt(othCoords[0].trim())
                                        && (Integer.parseInt(coords[coords.length - 1].trim()) >= Integer
                                                .parseInt(othCoords[0].trim()))
                                        || (Integer.parseInt(othCoords[0].trim()) <= Integer
                                                .parseInt(coords[0].trim())
                                                && Integer.parseInt(
                                                        othCoords[othCoords.length - 1].trim()) >= Integer
                                                                .parseInt(coords[0].trim()))) {
                                    alignmentsToBeRemoved.add(alignment);
                                }
                            });
                            alignmentsToBeRemoved.forEach((alignment) -> {
                                observedValueAlignments.get(value).remove(alignment);
                            });
                        });
                        // We filter out any values that are no logner aligned (due to overlapping conflicts)
                        toRemove = new HashSet<>();
                        for (String value : observedValueAlignments.keySet()) {
                            if (observedValueAlignments.get(value).isEmpty()) {
                                toRemove.add(value);
                            }
                        }
                        for (String value : toRemove) {
                            observedValueAlignments.remove(value);
                        }
                    }
                    getObservedAttrValueSequences().add(observedAttrValueSequence);
                }
            }
        }
    } catch (JSONException ex) {
    }
}

From source file:cz.cas.lib.proarc.common.export.mets.structure.MetsElementVisitor.java

/**
 * Generates technical metadata using JHOVE
 *
 * @param metsElement//www  .  j a v  a  2s  .  c  o  m
 * @param fileNames
 * @param seq
 * @param fileTypes
 * @param mimeTypes
 * @param pageDiv
 * @throws MetsExportException
 */
private void generateTechMetadata(IMetsElement metsElement, HashMap<String, Object> fileNames, int seq,
        HashMap<String, FileGrp> fileGrpPage, HashMap<String, String> mimeTypes, DivType pageDiv,
        HashMap<String, String> outputFileNames, HashMap<String, FileMD5Info> md5InfosMap)
        throws MetsExportException {
    if (fileNames.get("TECHMDGRP") == null) {
        LOG.log(Level.FINE, "Generating tech");
        Mets amdSecMets = new Mets();
        amdSecMets.setLabel1(mets.getLabel1());
        amdSecMets.setTYPE(mets.getTYPE());
        StructMapType mapType = new StructMapType();
        mapType.setTYPE(Const.DIV_PHYSICAL_ID);
        amdSecMets.getStructMap().add(mapType);
        AmdSecType amdSec = new AmdSecType();
        amdSec.setID(metsElement.getElementID());
        amdSecMets.getAmdSec().add(amdSec);
        DivType divType = new DivType();
        if (Const.PERIODICAL_TITLE
                .equalsIgnoreCase(metsElement.getMetsContext().getRootElement().getElementType())) {
            divType.setTYPE("PERIODICAL_PAGE");
        } else {
            divType.setTYPE("MONOGRAPH_PAGE");
        }

        FileSec fileSec = new FileSec();
        amdSecMets.setFileSec(fileSec);
        HashMap<String, FileGrp> amdSecFileGrpMap = new HashMap<String, MetsType.FileSec.FileGrp>();
        for (String fileMap : fileGrpPage.keySet()) {
            FileGrp fileGrp = fileGrpPage.get(fileMap);
            if (fileGrp.getFile().size() > 0) {
                FileGrp fileGrpAmd = new FileGrp();
                amdSecFileGrpMap.put(fileMap, fileGrpAmd);
                fileGrpAmd.setID(fileGrp.getID());
                fileGrpAmd.setUSE(fileGrp.getUSE());
                fileSec.getFileGrp().add(fileGrpAmd);
                for (FileType fileTypePage : fileGrp.getFile()) {
                    FileType fileTypeAmdSec = new FileType();
                    fileTypeAmdSec.setCHECKSUM(fileTypePage.getCHECKSUM());
                    fileTypeAmdSec.setCHECKSUMTYPE(fileTypePage.getCHECKSUMTYPE());
                    fileTypeAmdSec.setCREATED(fileTypePage.getCREATED());
                    fileTypeAmdSec.setID(fileTypePage.getID());
                    fileTypeAmdSec.setMIMETYPE(fileTypePage.getMIMETYPE());
                    fileTypeAmdSec.setSEQ(fileTypePage.getSEQ());
                    fileTypeAmdSec.setSIZE(fileTypePage.getSIZE());
                    fileGrpAmd.getFile().add(fileTypeAmdSec);
                    if (fileTypePage.getFLocat().get(0) != null) {
                        FLocat flocatAmd = new FLocat();
                        FLocat pageFlocat = fileTypePage.getFLocat().get(0);
                        if (pageFlocat.getHref() != null) {
                            flocatAmd.setHref(".." + pageFlocat.getHref().substring(1));
                        }
                        flocatAmd.setLOCTYPE(pageFlocat.getLOCTYPE());
                        fileTypeAmdSec.getFLocat().add(flocatAmd);
                    }
                    Fptr fptr = new Fptr();
                    fptr.setFILEID(fileTypeAmdSec);
                    divType.getFptr().add(fptr);
                }
            }
        }

        HashMap<String, String> toGenerate = new HashMap<String, String>();
        File rawFile = null;
        XMLGregorianCalendar rawCreated = null;
        Mix mixDevice = getScannerMix(metsElement);
        // RAW datastream for MIX_001 - only for Fedora
        PhotometricInterpretation photometricInterpretation = null;
        JHoveOutput jHoveOutputRaw = null;
        JHoveOutput jHoveOutputMC = null;
        if (metsElement.getMetsContext().getFedoraClient() != null) {
            try {
                DatastreamType rawDS = FoxmlUtils.findDatastream(metsElement.getSourceObject(), "RAW");
                if (rawDS != null) {
                    GetDatastreamDissemination dsRaw = FedoraClient
                            .getDatastreamDissemination(metsElement.getOriginalPid(), "RAW");
                    try {
                        rawCreated = rawDS.getDatastreamVersion().get(0).getCREATED();
                        InputStream is = dsRaw.execute(metsElement.getMetsContext().getFedoraClient())
                                .getEntityInputStream();
                        String rawExtendsion = MimeType
                                .getExtension(rawDS.getDatastreamVersion().get(0).getMIMETYPE());
                        rawFile = new File(metsElement.getMetsContext().getOutputPath() + File.separator
                                + metsElement.getMetsContext().getPackageID() + File.separator + "raw" + "."
                                + rawExtendsion);
                        FileMD5Info rawinfo;
                        try {
                            rawinfo = MetsUtils.getDigestAndCopy(is, new FileOutputStream(rawFile));
                        } catch (NoSuchAlgorithmException e) {
                            throw new MetsExportException(metsElement.getOriginalPid(),
                                    "Unable to copy RAW image and get digest", false, e);
                        }
                        rawinfo.setMimeType(rawDS.getDatastreamVersion().get(0).getMIMETYPE());
                        rawinfo.setCreated(rawDS.getDatastreamVersion().get(0).getCREATED());
                        md5InfosMap.put("RAW", rawinfo);
                        outputFileNames.put("RAW", rawFile.getAbsolutePath());
                        toGenerate.put("MIX_001", "RAW");

                        // If mix is present in fedora, then use this one
                        if (metsElement.getMetsContext().getFedoraClient() != null) {
                            jHoveOutputRaw = JhoveUtility.getMixFromFedora(metsElement, MixEditor.RAW_ID);
                        }
                        // If not present, then generate new
                        if (jHoveOutputRaw == null) {
                            jHoveOutputRaw = JhoveUtility.getMix(new File(rawFile.getAbsolutePath()),
                                    metsElement.getMetsContext(), mixDevice, rawCreated, null);
                            if (jHoveOutputRaw.getMix() == null) {
                                throw new MetsExportException(metsElement.getOriginalPid(),
                                        "Unable to generate Mix information for RAW image", false, null);
                            }
                        } else {
                            // Merges the information from the device mix
                            JhoveUtility.mergeMix(jHoveOutputRaw.getMix(), mixDevice);
                        }
                        if ((jHoveOutputRaw.getMix() != null)
                                && (jHoveOutputRaw.getMix().getBasicImageInformation() != null)
                                && (jHoveOutputRaw.getMix().getBasicImageInformation()
                                        .getBasicImageCharacteristics() != null)
                                && (jHoveOutputRaw.getMix().getBasicImageInformation()
                                        .getBasicImageCharacteristics()
                                        .getPhotometricInterpretation() != null)) {
                            photometricInterpretation = jHoveOutputRaw.getMix().getBasicImageInformation()
                                    .getBasicImageCharacteristics().getPhotometricInterpretation();
                        }
                        fixPSMix(jHoveOutputRaw, metsElement.getOriginalPid(), rawCreated);
                    } catch (FedoraClientException e) {
                        throw new MetsExportException(metsElement.getOriginalPid(),
                                "Unable to read raw datastream content", false, e);
                    }
                }
            } catch (IOException ex) {
                throw new MetsExportException(metsElement.getOriginalPid(),
                        "Error while getting RAW datastream " + metsElement.getOriginalPid(), false, ex);
            }
        }

        if (fileNames.get("MC_IMGGRP") != null) {
            toGenerate.put("MIX_002", "MC_IMGGRP");
            String outputFileName = outputFileNames.get("MC_IMGGRP");
            if (outputFileName != null) {
                String originalFile = MetsUtils.xPathEvaluateString(metsElement.getRelsExt(),
                        "*[local-name()='RDF']/*[local-name()='Description']/*[local-name()='importFile']");
                if (metsElement.getMetsContext().getFedoraClient() != null) {
                    jHoveOutputMC = JhoveUtility.getMixFromFedora(metsElement, MixEditor.NDK_ARCHIVAL_ID);
                }
                if (jHoveOutputMC == null) {
                    jHoveOutputMC = JhoveUtility.getMix(new File(outputFileName), metsElement.getMetsContext(),
                            null, md5InfosMap.get("MC_IMGGRP").getCreated(), originalFile);
                    if (jHoveOutputMC.getMix() == null) {
                        throw new MetsExportException(metsElement.getOriginalPid(),
                                "Unable to generate Mix information for MC image", false, null);
                    }
                }
                fixMCMix(jHoveOutputMC, metsElement.getOriginalPid(), md5InfosMap.get("MC_IMGGRP").getCreated(),
                        originalFile, photometricInterpretation);
            }
        }

        for (String name : toGenerate.keySet()) {
            String streamName = toGenerate.get(name);

            if (streamName != null) {
                MdSecType mdSec = new MdSecType();
                mdSec.setID(name);
                MdWrap mdWrap = new MdWrap();
                mdWrap.setMIMETYPE("text/xml");
                mdWrap.setMDTYPE("NISOIMG");
                XmlData xmlData = new XmlData();
                Node mixNode = null;

                if ("RAW".equals(streamName)) {
                    if (jHoveOutputRaw != null) {
                        mixNode = jHoveOutputRaw.getMixNode();
                        if (md5InfosMap.get(streamName) != null) {
                            md5InfosMap.get(streamName).setFormatVersion(jHoveOutputRaw.getFormatVersion());
                        }
                    }
                } else if (("MC_IMGGRP".equals(streamName)) && (md5InfosMap.get("MC_IMGGRP") != null)) {
                    if (jHoveOutputMC != null) {
                        mixNode = jHoveOutputMC.getMixNode();
                        if (md5InfosMap.get(streamName) != null) {
                            md5InfosMap.get(streamName).setFormatVersion(jHoveOutputMC.getFormatVersion());
                        }
                        if (mixNode != null) {
                            if ((amdSecFileGrpMap.get("MC_IMGGRP") != null)
                                    && (amdSecFileGrpMap.get("MC_IMGGRP").getFile().get(0) != null)) {
                                amdSecFileGrpMap.get("MC_IMGGRP").getFile().get(0).getADMID().add(mdSec);
                            }
                        }
                    }
                }

                if (mixNode != null) {
                    xmlData.getAny().add(mixNode);
                } else {
                    throw new MetsExportException(metsElement.getOriginalPid(),
                            "Unable to generate image metadata (MIX) for " + streamName, false, null);
                }

                mdWrap.setXmlData(xmlData);
                mdSec.setMdWrap(mdWrap);
                amdSec.getTechMD().add(mdSec);
            }
        }

        if (rawFile != null) {
            outputFileNames.remove("RAW");
            rawFile.delete();
        }

        if (outputFileNames.get("ALTOGRP") != null) {
            File altoFile = new File(outputFileNames.get("ALTOGRP"));
            if (altoFile.exists()) {
                Schema altoSchema;
                try {
                    altoSchema = AltoDatastream.getSchema();
                } catch (SAXException e) {
                    throw new MetsExportException("Unable to get ALTO schema", false);
                }
                try {
                    altoSchema.newValidator().validate(new StreamSource(altoFile));
                } catch (Exception exSax) {
                    throw new MetsExportException(metsElement.getOriginalPid(), "Invalid ALTO", false, exSax);
                }
                md5InfosMap.get("ALTOGRP").setFormatVersion("2.0");
            }
        }

        addPremisToAmdSec(amdSec, md5InfosMap, metsElement, amdSecFileGrpMap);
        mapType.setDiv(divType);
        saveAmdSec(metsElement, amdSecMets, fileNames, mimeTypes);
        FileType fileType = prepareFileType(seq, "TECHMDGRP", fileNames, mimeTypes,
                metsElement.getMetsContext(), outputFileNames, md5InfosMap);
        this.fileGrpMap.get("TECHMDGRP").getFile().add(fileType);
        Fptr fptr = new Fptr();
        fptr.setFILEID(fileType);
        pageDiv.getFptr().add(fptr);
    }
}

From source file:org.sakaiproject.site.tool.SiteAction.java

/**
 * find the tool in the tool list and remove the tool instance
 * @param state/*from w ww .  j  av a  2s.c o  m*/
 * @param toolId
 * @param originalToolId
 */
private void removeTool(SessionState state, String toolId, String originalToolId) {
    List toolList = (List) state.getAttribute(STATE_TOOL_REGISTRATION_LIST);
    // get the map of titles of multiple tool instances
    Map multipleToolIdTitleMap = state.getAttribute(STATE_MULTIPLE_TOOL_ID_TITLE_MAP) != null
            ? (Map) state.getAttribute(STATE_MULTIPLE_TOOL_ID_TITLE_MAP)
            : new HashMap();
    // get the attributes of multiple tool instances
    HashMap<String, HashMap<String, String>> multipleToolConfiguration = state
            .getAttribute(STATE_MULTIPLE_TOOL_CONFIGURATION) != null
                    ? (HashMap<String, HashMap<String, String>>) state
                            .getAttribute(STATE_MULTIPLE_TOOL_CONFIGURATION)
                    : new HashMap<String, HashMap<String, String>>();
    // the selected tool list
    List toolSelected = (List) state.getAttribute(STATE_TOOL_REGISTRATION_SELECTED_LIST);

    // remove the tool from related state variables
    toolSelected.remove(toolId);
    // remove the tool from the title map
    multipleToolIdTitleMap.remove(toolId);
    // remove the tool from the configuration map
    boolean found = false;
    for (ListIterator i = toolList.listIterator(); i.hasNext() && !found;) {
        MyTool tool = (MyTool) i.next();
        if (tool.getId().equals(toolId)) {
            toolList.remove(tool);
            found = true;
        }
    }
    multipleToolConfiguration.remove(toolId);

    state.setAttribute(STATE_MULTIPLE_TOOL_ID_TITLE_MAP, multipleToolIdTitleMap);
    state.setAttribute(STATE_MULTIPLE_TOOL_CONFIGURATION, multipleToolConfiguration);
    state.setAttribute(STATE_TOOL_REGISTRATION_LIST, toolList);
    state.setAttribute(STATE_TOOL_REGISTRATION_SELECTED_LIST, toolSelected);

}

From source file:com.lp.server.fertigung.ejbfac.FertigungFacBean.java

@TransactionTimeout(5000)
public TreeMap<String, Object[]> aktualisiereLoseAusStueckliste(Integer stuecklisteIId,
        TheClientDto theClientDto) {// ww w. ja  v  a 2s  . c  o  m

    TreeMap<String, Object[]> tmAktualisierteLose = new TreeMap<String, Object[]>();

    ParametermandantDto parameter = null;
    try {
        parameter = getParameterFac().getMandantparameter(theClientDto.getSMandantenwaehrung(),
                ParameterFac.KATEGORIE_STUECKLISTE,
                ParameterFac.PARAMETER_BEI_LOS_AKTUALISIERUNG_MATERIAL_NACHBUCHEN);
    } catch (RemoteException e1) {
        throwEJBExceptionLPRespectOld(e1);
    }
    boolean bMaterialNachbuchen = (Boolean) parameter.getCWertAsObject();

    Session session = FLRSessionFactory.getFactory().openSession();

    String queryString = "SELECT l FROM FLRLosReport l WHERE l.status_c_nr IN ('" + LocaleFac.STATUS_ANGELEGT
            + "','" + LocaleFac.STATUS_AUSGEGEBEN + "','" + LocaleFac.STATUS_IN_PRODUKTION
            + "') AND l.mandant_c_nr='" + theClientDto.getMandant() + "' AND l.stueckliste_i_id IS NOT NULL ";

    if (stuecklisteIId != null) {
        queryString += " AND l.stueckliste_i_id=" + stuecklisteIId;
    }

    org.hibernate.Query query = session.createQuery(queryString);
    List<?> results = query.list();
    Iterator<?> resultListIterator = results.iterator();
    while (resultListIterator.hasNext()) {
        FLRLosReport los = (FLRLosReport) resultListIterator.next();

        if (los.getStatus_c_nr().equals(LocaleFac.STATUS_ANGELEGT)) {
            aktualisiereSollArbeitsplanAusStueckliste(los.getI_id(), theClientDto);
            aktualisiereSollMaterialAusStueckliste(los.getI_id(), theClientDto);
        } else {

            LoslagerentnahmeDto[] laeger = loslagerentnahmeFindByLosIId(los.getI_id());

            aktualisiereSollArbeitsplanAusStueckliste(los.getI_id(), theClientDto);

            // Alle stuecklistenpositionen holen + Hilfsstueckliste und dann
            // verdichten
            HashMap<Integer, StuecklistepositionDto> hmStuecklistenposition = getFertigungFac()
                    .holeAlleLossollmaterialFuerStuecklistenAktualisierung(los.getStueckliste_i_id(),
                            los.getN_losgroesse(), 0, null, theClientDto);

            LossollmaterialDto[] sollmatDtos = lossollmaterialFindByLosIId(los.getI_id());

            for (int i = 0; i < sollmatDtos.length; i++) {
                try {
                    if (!Helper.short2boolean(sollmatDtos[i].getBNachtraeglich())) {
                        // War vor SP2000 --> &&
                        // sollmatDtos[i].getNMenge().doubleValue() > 0

                        LosistmaterialDto[] istmaterialDtos = losistmaterialFindByLossollmaterialIId(
                                sollmatDtos[i].getIId());
                        BigDecimal bdAusgegeben = getAusgegebeneMenge(sollmatDtos[i].getIId(), null,
                                theClientDto);
                        ArtikelDto artikelDto = getArtikelFac()
                                .artikelFindByPrimaryKeySmall(sollmatDtos[i].getArtikelIId(), theClientDto);

                        Object[] oZeileReport = new Object[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ANZAHL_SPALTEN];
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_LOSNUMMER] = los
                                .getC_nr();
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ARTIKELNUMMER] = artikelDto
                                .getCNr();
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_EINHEIT] = artikelDto
                                .getEinheitCNr();
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE] = sollmatDtos[i]
                                .getNMenge();
                        if (artikelDto.getArtikelsprDto() != null) {
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_BEZEICHNUNG] = artikelDto
                                    .getArtikelsprDto().getCBez();
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ZUSATZBEZEICHNUNG] = artikelDto
                                    .getArtikelsprDto().getCZbez();
                        }

                        if (hmStuecklistenposition.containsKey(sollmatDtos[i].getArtikelIId())) {

                            // Mengen abziehen
                            StuecklistepositionDto stklPos = hmStuecklistenposition
                                    .get(sollmatDtos[i].getArtikelIId());

                            sollmatDtos[i].setNMenge(stklPos.getNMenge());

                            Lossollmaterial sollmatBean = em.find(Lossollmaterial.class,
                                    sollmatDtos[i].getIId());

                            BigDecimal diffSollmenge = stklPos.getNMenge().subtract(sollmatBean.getNMenge());

                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE] = diffSollmenge;

                            sollmatBean.setNMenge(stklPos.getNMenge());
                            sollmatBean.setIBeginnterminoffset(stklPos.getIBeginnterminoffset());

                            BigDecimal diff = bdAusgegeben.subtract(sollmatDtos[i].getNMenge());

                            if (diff.doubleValue() == 0 && diffSollmenge.doubleValue() != 0) {
                                oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = new BigDecimal(
                                        0);
                                tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                        los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                            } else {

                                if (bMaterialNachbuchen == true || (bMaterialNachbuchen == false && Helper
                                        .short2boolean(artikelDto.getBLagerbewirtschaftet()) == false)) {

                                    if (Helper.short2boolean(artikelDto.getBSeriennrtragend())
                                            || Helper.short2boolean(artikelDto.getBChargennrtragend())) {
                                        sollmatDtos[i] = null;

                                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = new BigDecimal(
                                                0);
                                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_BEMERKUNG] = "Der Artikel ist SNR/CNR behaftet und wurde nicht ber\u00FCcksichtigt.";

                                        tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                                los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                                        continue;
                                    }

                                    if (diff.doubleValue() > 0) {

                                        for (int j = 0; j < istmaterialDtos.length; j++) {
                                            if (diff.doubleValue() > 0) {
                                                BigDecimal istmenge = istmaterialDtos[j].getNMenge();

                                                BigDecimal bdMengeNeu = null;

                                                if (diff.doubleValue() > istmenge.doubleValue()) {
                                                    bdMengeNeu = new BigDecimal(0);
                                                    diff = diff.subtract(istmenge);
                                                } else {
                                                    bdMengeNeu = istmenge.subtract(diff);
                                                    diff = new BigDecimal(0);
                                                }

                                                updateLosistmaterialMenge(istmaterialDtos[j].getIId(),
                                                        bdMengeNeu, theClientDto);
                                            }
                                        }

                                        BigDecimal bdAusgegebenNachher = getAusgegebeneMenge(
                                                sollmatDtos[i].getIId(), null, theClientDto);

                                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = bdAusgegebenNachher
                                                .subtract(bdAusgegeben);

                                        tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                                los.getC_nr() + artikelDto.getCNr(), oZeileReport);
                                    } else {
                                        BigDecimal bdAbzubuchendeMenge = diff.abs();
                                        for (int j = 0; j < laeger.length; j++) {
                                            // wenn noch was abzubuchen ist
                                            // (Menge >
                                            // 0)
                                            if (bdAbzubuchendeMenge.compareTo(new BigDecimal(0)) == 1) {
                                                BigDecimal bdLagerstand = null;
                                                if (Helper
                                                        .short2boolean(artikelDto.getBLagerbewirtschaftet())) {

                                                    bdLagerstand = getLagerFac().getLagerstand(
                                                            artikelDto.getIId(), laeger[j].getLagerIId(),
                                                            theClientDto);

                                                } else {
                                                    bdLagerstand = new BigDecimal(999999999);
                                                }
                                                // wenn ein lagerstand da
                                                // ist
                                                if (bdLagerstand.compareTo(new BigDecimal(0)) == 1) {
                                                    BigDecimal bdMengeVonLager;
                                                    if (bdLagerstand.compareTo(bdAbzubuchendeMenge) == 1) {
                                                        // wenn mehr als
                                                        // ausreichend
                                                        // auf
                                                        // lager
                                                        bdMengeVonLager = bdAbzubuchendeMenge;
                                                    } else {
                                                        // dann nur den
                                                        // lagerstand
                                                        // entnehmen
                                                        bdMengeVonLager = bdLagerstand;
                                                    }
                                                    LosistmaterialDto istmat = new LosistmaterialDto();
                                                    istmat.setLagerIId(laeger[j].getLagerIId());
                                                    istmat.setLossollmaterialIId(sollmatDtos[i].getIId());
                                                    istmat.setNMenge(bdMengeVonLager);

                                                    if (sollmatDtos[i].getNMenge().doubleValue() > 0) {
                                                        istmat.setBAbgang(Helper.boolean2Short(true));
                                                    } else {
                                                        istmat.setBAbgang(Helper.boolean2Short(false));
                                                    }

                                                    // ist-wert anlegen und
                                                    // lagerbuchung
                                                    // durchfuehren
                                                    createLosistmaterial(istmat, null, theClientDto);
                                                    // menge reduzieren
                                                    bdAbzubuchendeMenge = bdAbzubuchendeMenge
                                                            .subtract(bdMengeVonLager);
                                                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = bdMengeVonLager;

                                                    tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                                            los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                                                } else {
                                                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = new BigDecimal(
                                                            0);
                                                    tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                                            los.getC_nr() + artikelDto.getCNr(), oZeileReport);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            hmStuecklistenposition.remove(sollmatDtos[i].getArtikelIId());
                            getFehlmengeFac().aktualisiereFehlmenge(LocaleFac.BELEGART_LOS,
                                    sollmatDtos[i].getIId(), false, theClientDto);
                        } else {

                            // Los-Sollmaterial loeschen

                            verknuepfungZuBestellpositionUndArbeitsplanLoeschen(sollmatDtos[i].getIId());

                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE] = sollmatDtos[i]
                                    .getNMenge().multiply(new BigDecimal(-1));

                            if (bMaterialNachbuchen == true || istmaterialDtos.length == 0
                                    || (bMaterialNachbuchen == false && Helper
                                            .short2boolean(artikelDto.getBLagerbewirtschaftet()) == false)) {
                                for (int j = 0; j < istmaterialDtos.length; j++) {
                                    removeLosistmaterial(istmaterialDtos[j], theClientDto);
                                }

                                removeLossollmaterial(sollmatDtos[i], theClientDto);

                                oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = bdAusgegeben
                                        .multiply(new BigDecimal(-1));
                                tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                        los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                            } else {
                                sollmatDtos[i].setNMenge(new BigDecimal(0));
                                updateLossollmaterial(sollmatDtos[i], theClientDto);
                                oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = new BigDecimal(
                                        0);
                                tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                        los.getC_nr() + artikelDto.getCNr(), oZeileReport);
                            }

                            getFehlmengeFac().aktualisiereFehlmenge(LocaleFac.BELEGART_LOS,
                                    sollmatDtos[i].getIId(), false, theClientDto);
                            sollmatDtos[i] = null;

                        }

                    }
                } catch (RemoteException e) {
                    throwEJBExceptionLPRespectOld(e);
                }

            }

            // Nun alle uebrigen Sollmaterial=0 loeschen
            for (int i = 0; i < sollmatDtos.length; i++) {
                if (sollmatDtos[i] != null && !Helper.short2boolean(sollmatDtos[i].getBNachtraeglich())
                        && sollmatDtos[i].getNMenge().doubleValue() == 0) {

                    // Los-Sollmaterial loeschen
                    LosistmaterialDto[] istmaterialDtos = losistmaterialFindByLossollmaterialIId(
                            sollmatDtos[i].getIId());

                    boolean bAlleIstMaterialSindNull = true;

                    for (int z = 0; z < istmaterialDtos.length; z++) {
                        if (istmaterialDtos[z].getNMenge().doubleValue() != 0) {
                            bAlleIstMaterialSindNull = false;
                        }
                    }
                    ArtikelDto artikelDto = getArtikelFac()
                            .artikelFindByPrimaryKeySmall(sollmatDtos[i].getArtikelIId(), theClientDto);
                    if (bMaterialNachbuchen == true || bAlleIstMaterialSindNull == true
                            || (bMaterialNachbuchen == false
                                    && Helper.short2boolean(artikelDto.getBLagerbewirtschaftet()) == false)) {

                        BigDecimal bdAusgegeben = getAusgegebeneMenge(sollmatDtos[i].getIId(), null,
                                theClientDto);

                        for (int j = 0; j < istmaterialDtos.length; j++) {
                            removeLosistmaterial(istmaterialDtos[j], theClientDto);
                        }

                        verknuepfungZuBestellpositionUndArbeitsplanLoeschen(sollmatDtos[i].getIId());
                        removeLossollmaterial(sollmatDtos[i], theClientDto);

                        if (bdAusgegeben.doubleValue() != 0) {

                            Object[] oZeileReport = new Object[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ANZAHL_SPALTEN];
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_LOSNUMMER] = los
                                    .getC_nr();
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ARTIKELNUMMER] = artikelDto
                                    .getCNr();
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_EINHEIT] = artikelDto
                                    .getEinheitCNr();
                            if (artikelDto.getArtikelsprDto() != null) {
                                oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_BEZEICHNUNG] = artikelDto
                                        .getArtikelsprDto().getCBez();
                                oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ZUSATZBEZEICHNUNG] = artikelDto
                                        .getArtikelsprDto().getCZbez();
                            }
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = bdAusgegeben
                                    .multiply(new BigDecimal(-1));

                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE] = new BigDecimal(
                                    0);

                            tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                    los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                        }
                    }

                }
            }

            // Alle noch nicht verbrauchten neu eintragen

            Iterator it = hmStuecklistenposition.keySet().iterator();
            while (it.hasNext()) {

                try {
                    LagerDto lagerDtoMandant = getLagerFac().getHauptlagerDesMandanten(theClientDto);
                    Integer artikelIId = (Integer) it.next();

                    StuecklistepositionDto stklPos = hmStuecklistenposition.get(artikelIId);
                    BigDecimal bdAbzubuchendeMenge = stklPos.getNMenge();

                    ArtikelDto artikelDto = getArtikelFac()
                            .artikelFindByPrimaryKeySmall(stklPos.getArtikelIId(), theClientDto);

                    Object[] oZeileReport = new Object[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ANZAHL_SPALTEN];
                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_LOSNUMMER] = los
                            .getC_nr();
                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ARTIKELNUMMER] = artikelDto
                            .getCNr();
                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_EINHEIT] = artikelDto
                            .getEinheitCNr();
                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE] = stklPos
                            .getNMenge();

                    if (artikelDto.getArtikelsprDto() != null) {
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_BEZEICHNUNG] = artikelDto
                                .getArtikelsprDto().getCBez();
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ZUSATZBEZEICHNUNG] = artikelDto
                                .getArtikelsprDto().getCZbez();
                    }

                    LossollmaterialDto losMatDto = new LossollmaterialDto();
                    losMatDto.setArtikelIId(stklPos.getArtikelIId());
                    losMatDto.setBNachtraeglich(Helper.boolean2Short(false));
                    losMatDto.setCKommentar(stklPos.getCKommentar());
                    losMatDto.setCPosition(stklPos.getCPosition());
                    losMatDto.setFDimension1(stklPos.getFDimension1());
                    losMatDto.setFDimension2(stklPos.getFDimension2());
                    losMatDto.setFDimension3(stklPos.getFDimension3());
                    losMatDto.setILfdnummer(stklPos.getILfdnummer());
                    losMatDto.setEinheitCNr(stklPos.getEinheitCNr());
                    losMatDto.setLosIId(los.getI_id());
                    losMatDto.setMontageartIId(stklPos.getMontageartIId());
                    losMatDto.setNMenge(stklPos.getNMenge());
                    losMatDto.setISort(new Integer(0));

                    BigDecimal bdSollpreis = getLagerFac().getGemittelterGestehungspreisEinesLagers(
                            stklPos.getArtikelIId(), lagerDtoMandant.getIId(), theClientDto);
                    losMatDto.setNSollpreis(bdSollpreis);

                    Integer sollmatIId = createLossollmaterial(losMatDto, theClientDto).getIId();
                    if (bMaterialNachbuchen == true || (bMaterialNachbuchen == false
                            && Helper.short2boolean(artikelDto.getBLagerbewirtschaftet()) == false)) {

                        if (Helper.short2boolean(artikelDto.getBSeriennrtragend())
                                || Helper.short2boolean(artikelDto.getBChargennrtragend())) {
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_BEMERKUNG] = "Der Artikel ist SNR/CNR behaftet und wurde nicht ber\u00FCcksichtigt.";

                            tmAktualisierteLose.put(los.getC_nr() + artikelDto.getCNr(), oZeileReport);
                            continue;
                        }

                        for (int j = 0; j < laeger.length; j++) {
                            // wenn noch was abzubuchen ist
                            // (Menge >
                            // 0)
                            if (bdAbzubuchendeMenge.compareTo(new BigDecimal(0)) == 1) {
                                BigDecimal bdLagerstand = null;
                                if (Helper.short2boolean(artikelDto.getBLagerbewirtschaftet())) {

                                    bdLagerstand = getLagerFac().getLagerstand(artikelDto.getIId(),
                                            laeger[j].getLagerIId(), theClientDto);

                                } else {
                                    bdLagerstand = new BigDecimal(999999999);
                                }
                                // wenn ein lagerstand da ist
                                if (bdLagerstand.compareTo(new BigDecimal(0)) == 1) {
                                    BigDecimal bdMengeVonLager;
                                    if (bdLagerstand.compareTo(bdAbzubuchendeMenge) == 1) {
                                        // wenn mehr als
                                        // ausreichend
                                        // auf
                                        // lager
                                        bdMengeVonLager = bdAbzubuchendeMenge;
                                    } else {
                                        // dann nur den
                                        // lagerstand
                                        // entnehmen
                                        bdMengeVonLager = bdLagerstand;
                                    }
                                    LosistmaterialDto istmat = new LosistmaterialDto();
                                    istmat.setLagerIId(laeger[j].getLagerIId());
                                    istmat.setLossollmaterialIId(sollmatIId);
                                    istmat.setNMenge(bdMengeVonLager);

                                    if (stklPos.getNMenge().doubleValue() > 0) {
                                        istmat.setBAbgang(Helper.boolean2Short(true));
                                    } else {
                                        istmat.setBAbgang(Helper.boolean2Short(false));
                                    }

                                    // ist-wert anlegen und
                                    // lagerbuchung
                                    // durchfuehren
                                    createLosistmaterial(istmat, null, theClientDto);
                                    // menge reduzieren
                                    bdAbzubuchendeMenge = bdAbzubuchendeMenge.subtract(bdMengeVonLager);

                                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = bdMengeVonLager;
                                    tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                            los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                                }
                            }
                        }
                    } else {
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = new BigDecimal(
                                0);
                        tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                los.getC_nr() + artikelDto.getCNr(), oZeileReport);
                    }

                    getFehlmengeFac().aktualisiereFehlmenge(LocaleFac.BELEGART_LOS, losMatDto.getIId(), false,
                            theClientDto);

                } catch (RemoteException e) {
                    throwEJBExceptionLPRespectOld(e);
                }
            }

            // Los aktualisieren
            Los losUpd = em.find(Los.class, los.getI_id());
            losUpd.setTAktualisierungstueckliste(getTimestamp());

        }

    }

    return tmAktualisierteLose;
}

From source file:com.krawler.spring.importFunctionality.ImportUtil.java

/**
 * @param requestParams/*from w w  w  .j a v  a  2s.  co  m*/
 * @param dataMap
 * @param columnConfig
 * @param column
 * @param customfield
 * @param columnHeaderMap
 * @param dateFormat
 * @param importDao
 * @throws JSONException
 * @throws DataInvalidateException
 * @throws ParseException
 */
private static void validateColumnData(HashMap<String, Object> requestParams, HashMap<String, Object> dataMap,
        JSONObject columnConfig, String column, JSONArray customfield, HashMap<String, Object> columnHeaderMap,
        String dateFormat, ImportDAO importDao, Integer colCsvIndex, HashMap<String, String> autoNoMap)
        throws JSONException, DataInvalidateException, ParseException {
    int maxLength = columnConfig.getInt("maxLength");
    String csvHeader = (String) columnHeaderMap.get(column);
    csvHeader = (csvHeader == null ? csvHeader : csvHeader.replaceAll("\\.", " "));//remove '.' from csv Header
    String columnHeader = columnConfig.getString("columnName");
    String data = dataMap.get(column) == null ? null : String.valueOf(dataMap.get(column));
    Object vDataValue = data;

    if (columnConfig.has("validatetype")) {
        String validatetype = columnConfig.getString("validatetype");
        boolean customflag = false;
        if (columnConfig.has("customflag")) {
            customflag = columnConfig.getBoolean("customflag");
        }
        if (validatetype.equalsIgnoreCase("integer")) {
            try {
                if (!StringUtil.isNullOrEmpty(data)) { // Remove ","(comma) from number
                    data = data.replaceAll(",", "");
                }
                if (maxLength > 0 && data != null && data.length() > maxLength) { // Added null value check for data[Sandeep k]
                    throw new DataInvalidateException(
                            "Data length greater than " + maxLength + " for column " + csvHeader + ".");
                }
                vDataValue = StringUtil.isNullOrEmpty(data) ? "" : Integer.parseInt(data);
            } catch (DataInvalidateException dex) {
                throw dex;
            } catch (Exception ex) {
                throw new DataInvalidateException(
                        "Incorrect numeric value for " + csvHeader + ", Please ensure that value type of "
                                + csvHeader + " matches with the " + columnHeader + ".");
            }
        } else if (validatetype.equalsIgnoreCase("double")) {
            try {
                if (!StringUtil.isNullOrEmpty(data)) { // Remove ","(comma) from number
                    data = data.replaceAll(",", "");
                }
                if (maxLength > 0 && data != null && data.length() > maxLength) {
                    throw new DataInvalidateException(
                            "Data length greater than " + maxLength + " for column " + csvHeader + ".");
                }
                vDataValue = StringUtil.isNullOrEmpty(data) ? "" : Double.parseDouble(data);
            } catch (DataInvalidateException dex) {
                throw dex;
            } catch (Exception ex) {
                throw new DataInvalidateException(
                        "Incorrect numeric value for " + csvHeader + ", Please ensure that value type of "
                                + csvHeader + " matches with the " + columnHeader + ".");
            }
        } else if (validatetype.equalsIgnoreCase("date")) {
            if (!StringUtil.isNullOrEmpty(data)) {
                String ldf = dateFormat != null ? dateFormat : (data.length() > 10 ? df_full : df);
                try {
                    if (maxLength > 0 && data == null) {
                        throw new DataInvalidateException(
                                "Data length greater than " + maxLength + " for column " + csvHeader + ".");
                    }
                    DateFormat sdf = new SimpleDateFormat(ldf);
                    sdf.setLenient(false);
                    sdf.setTimeZone(TimeZone.getTimeZone("GMT" + requestParams.get("tzdiff")));
                    vDataValue = StringUtil.isNullOrEmpty(data) ? null : sdf.parse(data);
                    if (customflag && vDataValue != null) {
                        vDataValue = ((Date) vDataValue).getTime();
                        tempFileData[colCsvIndex] = vDataValue;
                        if (tempFileData != null) {//In case of invalid records date field is showing number(Long value) so replacing by proper format in tempFileData
                            tempFileData[colCsvIndex.intValue()] = new Date((Long) vDataValue);
                        }
                    }
                } catch (DataInvalidateException dex) {
                    throw dex;
                } catch (Exception ex) {
                    try {
                        vDataValue = Long.parseLong(data);
                        if (!customflag && vDataValue != null) {
                            vDataValue = new Date((Long) vDataValue);
                        }

                        if (tempFileData != null) {//In case of invalid records date field is showing number(Long value) so replacing by proper format in tempFileData
                            tempFileData[colCsvIndex.intValue()] = customflag ? new Date((Long) vDataValue)
                                    : vDataValue;
                        }
                    } catch (Exception e) {
                        throw new DataInvalidateException("Incorrect date format for " + csvHeader
                                + ", Please specify values in " + ldf + " format.");
                    }
                }
            } else {
                vDataValue = null;
            }
        } else if (validatetype.equalsIgnoreCase("time")) {
            if (!StringUtil.isNullOrEmpty(data)) {
                //@@@ need to uncomment
                //                    Pattern pattern = Pattern.compile(EmailRegEx);
                //                    if(!pattern.matcher(data).matches()){
                //                        throw new DataInvalidateException("Incorrect time format for "+columnConfig.getString("columnName")+" use HH:MM AM or PM");
                //                    }
                vDataValue = data;
            } else {
                vDataValue = null;
            }
        } else if (validatetype.equalsIgnoreCase("ref")) {
            if (!StringUtil.isNullOrEmpty(data)) {
                try {
                    String pref = (String) requestParams.get("masterPreference"); //0:Skip Record, 1:Skip Column, 2:Add new
                    if (columnConfig.has("refModule") && columnConfig.has("refDataColumn")
                            && columnConfig.has("refFetchColumn") && columnConfig.has("configid")) {
                        requestParams.put("defaultheader", columnConfig.getString("columnName"));
                        List list = getRefData(requestParams, columnConfig.getString("refModule"),
                                columnConfig.getString("refDataColumn"),
                                columnConfig.getString("refFetchColumn"), columnConfig.getString("configid"),
                                data, importDao);
                        if (list.size() == 0) {
                            //                                String configid = columnConfig.getString("configid");
                            //                                // Configid =>> 1:Owner, 2:Product, 3:Account, 4:Contact
                            //                                // then we can't create new entry for such module
                            //                                if(pref.equalsIgnoreCase("2") && (configid.equals("1") || configid.equals("2") || configid.equals("3") || configid.equals("4"))) {
                            //                                    throw new DataInvalidateException(csvHeader+" entry not found in master list for "+ columnHeader +" dropdown."); // Throw ex to skip record.
                            //                                } else
                            if (pref.equalsIgnoreCase("0")) { //Skip Record
                                if (!ImportHandler.isMasterTable(columnConfig.getString("refModule"))) { // Cant't create entry for ref. module
                                    throw new DataInvalidateException(csvHeader + " entry not present in "
                                            + columnHeader + " list, Please create new " + columnHeader
                                            + " entry for '" + (data.replaceAll("\\.", ""))
                                            + "' as it requires some other details.");
                                } else
                                    throw new DataInvalidateException(
                                            csvHeader + " entry not found in master list for " + columnHeader
                                                    + " dropdown."); // Throw ex to skip record.
                            } else if (pref.equalsIgnoreCase("1")) {
                                vDataValue = null; // Put 'null' value to skip column data.
                            } else if (pref.equalsIgnoreCase("2")) {
                                if (!ImportHandler.isMasterTable(columnConfig.getString("refModule"))) { // Cant't create entry for ref. module
                                    throw new DataInvalidateException(csvHeader + " entry not present in "
                                            + columnHeader + " list, Please create new " + columnHeader
                                            + " entry for '" + (data.replaceAll("\\.", ""))
                                            + "' as it requires some other details.");
                                }
                            }
                        } else {
                            vDataValue = list.get(0).toString();
                        }
                    } else {
                        throw new DataInvalidateException(
                                "Incorrect reference mapping(" + columnHeader + ") for " + csvHeader + ".");
                    }
                } catch (ServiceException ex) {
                    throw new DataInvalidateException(
                            "Incorrect reference mapping(" + columnHeader + ") for " + csvHeader + ".");
                } catch (DataInvalidateException ex) {
                    throw ex;
                } catch (Exception ex) {
                    throw new DataInvalidateException(
                            csvHeader + " entry not found in master list for " + columnHeader + " dropdown.");
                }
            } else {
                vDataValue = null;
            }
        } else if (!customflag && validatetype.equalsIgnoreCase("multiselect")) {
            if (!StringUtil.isNullOrEmpty(data)) {
                try {
                    String pref = (String) requestParams.get("masterPreference"); //0:Skip Record, 1:Skip Column, 2:Add new

                    if (columnConfig.has("refModule") && columnConfig.has("refDataColumn")
                            && columnConfig.has("refFetchColumn") && columnConfig.has("configid")) {
                        String[] multiData = data.toString().split(Constants.Custom_Column_Sep);
                        String mdata = "";
                        Boolean isRefData = columnConfig.has("refModule") && columnConfig.has("refDataColumn")
                                && columnConfig.has("refFetchColumn") && columnConfig.has("configid");
                        List list = null;
                        for (int i = 0; isRefData && i < multiData.length; i++) { // Reference module
                            list = getRefData(requestParams, columnConfig.getString("refModule"),
                                    columnConfig.getString("refDataColumn"),
                                    columnConfig.getString("refFetchColumn"),
                                    columnConfig.getString("configid"), multiData[i], importDao);
                            if (list.size() == 0) {
                                if (pref.equalsIgnoreCase("0")) { //Skip Record
                                    if (!ImportHandler.isMasterTable(columnConfig.getString("refModule"))) { // Cant't create entry for ref. module
                                        throw new DataInvalidateException(csvHeader + " entry not present in "
                                                + columnHeader + " list, Please create new " + columnHeader
                                                + " entry for '" + (multiData[i].replaceAll("\\.", ""))
                                                + "' as it requires some other details.");
                                    } else {
                                        throw new DataInvalidateException(
                                                csvHeader + " entry not found in master list for "
                                                        + columnHeader + " dropdown."); // Throw ex to skip record.
                                    }
                                } else if (pref.equalsIgnoreCase("2")) {
                                    if (!ImportHandler.isMasterTable(columnConfig.getString("refModule"))) { // Cant't create entry for ref. module
                                        throw new DataInvalidateException(csvHeader + " entry not present in "
                                                + columnHeader + " list, Please create new " + columnHeader
                                                + " entry for '" + (multiData[i].replaceAll("\\.", ""))
                                                + "' as it requires some other details.");
                                    }
                                }
                            } else {
                                mdata += list.get(0).toString() + Constants.Custom_Column_Sep;
                            }
                        }
                        if (mdata.length() > 0) {
                            vDataValue = mdata.substring(0, mdata.length() - 1);
                        } else {
                            if (pref.equalsIgnoreCase("1")) {
                                vDataValue = null;
                            } else
                                vDataValue = "";
                        }
                    } else {
                        throw new DataInvalidateException(
                                "Incorrect reference mapping(" + columnHeader + ") for " + csvHeader + ".");
                    }

                } catch (ServiceException ex) {
                    throw new DataInvalidateException(
                            "Incorrect reference mapping(" + columnHeader + ") for " + csvHeader + ".");
                } catch (DataInvalidateException ex) {
                    throw ex;
                } catch (Exception ex) {
                    throw new DataInvalidateException(
                            csvHeader + " entry not found in master list for " + columnHeader + " dropdown.");
                }
            } else {
                vDataValue = null;
            }
        } else if (validatetype.equalsIgnoreCase("email")) {
            if (maxLength > 0 && data != null && data.length() > maxLength) {
                throw new DataInvalidateException(
                        "Data length greater than " + maxLength + " for column " + csvHeader + ".");
            }
            if (!StringUtil.isNullOrEmpty(data)) {
                Pattern pattern = Pattern.compile(EmailRegEx);
                if (!pattern.matcher(data).matches()) {
                    throw new DataInvalidateException("Invalid email address for " + csvHeader + ".");
                }
                vDataValue = data;
            } else {
                vDataValue = null;
            }
        } else if (validatetype.equalsIgnoreCase("boolean")) {
            if (data.equalsIgnoreCase("true") || data.equalsIgnoreCase("1") || data.equalsIgnoreCase("T")) {
                vDataValue = true;
            } else if (data.equalsIgnoreCase("false") || data.equalsIgnoreCase("0")
                    || data.equalsIgnoreCase("F")) {
                vDataValue = false;
            } else {
                throw new DataInvalidateException("Incorrect boolean value for " + csvHeader + ".");
            }
        }

        if (vDataValue == null && columnConfig.has("isNotNull") && columnConfig.getBoolean("isNotNull")) {
            throw new DataInvalidateException(
                    "Empty data found in " + csvHeader + ", Can not set empty data for " + columnHeader + ".");
        } else {
            if (customflag) {
                JSONObject jobj = new JSONObject();
                if (columnConfig.getString("xtype").equals("4")
                        || columnConfig.getString("xtype").equals("7")) {//Drop down & Multi Select Drop down
                    try {
                        if (vDataValue != null) {
                            if (!StringUtil.isNullOrEmpty(vDataValue.toString())) {
                                HashMap<String, Object> comborequestParams = new HashMap<String, Object>();
                                comborequestParams = requestParams;
                                String pref = (String) requestParams.get("masterPreference"); //0:Skip Record, 1:Skip Column, 2:Add new
                                KwlReturnObject result = null;
                                if (columnConfig.getString("xtype").equals("4")) {
                                    comborequestParams.put("filtergetColumn_names",
                                            Arrays.asList("fieldid", "value"));
                                    comborequestParams.put("filter_values", Arrays.asList(
                                            columnConfig.getString("pojoHeader"), vDataValue.toString()));

                                    List lst = getCustomComboID(requestParams, vDataValue.toString(),
                                            columnConfig.getString("pojoHeader"), "id", importDao);
                                    //                                        List lst = result.getEntityList();
                                    if (lst.size() == 0) {
                                        if (pref.equalsIgnoreCase("0")) { //Skip Record
                                            throw new DataInvalidateException(
                                                    csvHeader + " entry not found in the list for "
                                                            + columnHeader + " dropdown."); // Throw ex to skip record.
                                        } else if (pref.equalsIgnoreCase("1")) {
                                            vDataValue = null; // Put 'null' value to skip column data.
                                        }
                                    } else {
                                        //                                                FieldComboData tmpcontyp = (FieldComboData) ite.next();
                                        vDataValue = lst.get(0).toString();
                                    }
                                } else if (columnConfig.getString("xtype").equals("7")) {
                                    String[] multiData = vDataValue.toString()
                                            .split(Constants.Custom_Column_Sep);
                                    String mdata = "";
                                    for (int i = 0; i < multiData.length; i++) {

                                        // if for master  multiselect data item and else for custom multiselect
                                        if (columnConfig.has("refModule") && columnConfig.has("refDataColumn")
                                                && columnConfig.has("refFetchColumn")
                                                && columnConfig.has("configid")) {
                                            List list = getRefData(requestParams,
                                                    columnConfig.getString("refModule"),
                                                    columnConfig.getString("refDataColumn"),
                                                    columnConfig.getString("refFetchColumn"),
                                                    columnConfig.getString("configid"), multiData[i],
                                                    importDao);
                                            mdata += list.get(0).toString() + Constants.Custom_Column_Sep;
                                        } else {
                                            comborequestParams.put("filter_names",
                                                    Arrays.asList("fieldid", "value"));
                                            comborequestParams.put("filter_values", Arrays.asList(
                                                    columnConfig.getString("pojoHeader"), multiData[i]));

                                            List lst = getCustomComboID(requestParams, multiData[i],
                                                    columnConfig.getString("pojoHeader"), "id", importDao);
                                            //                                                List lst = result.getEntityList();
                                            if (lst.size() == 0) {
                                                if (pref.equalsIgnoreCase("0")) { //Skip Record
                                                    throw new DataInvalidateException(
                                                            csvHeader + " entry not found in the list for "
                                                                    + columnHeader + " dropdown."); // Throw ex to skip record.
                                                } else if (pref.equalsIgnoreCase("1")) {
                                                    vDataValue = null; // Put 'null' value to skip column data.
                                                }
                                            } else {
                                                //                                                FieldComboData tmpcontyp = (FieldComboData) ite.next();
                                                mdata += lst.get(0).toString() + Constants.Custom_Column_Sep;
                                            }
                                        }
                                    }
                                    if (mdata.length() > 0) {
                                        vDataValue = mdata.substring(0, mdata.length() - 1);
                                    }
                                }
                            }
                        } /* else {
                          throw new DataInvalidateException("Incorrect reference mapping("+columnHeader+") for "+csvHeader+".");
                          }*/
                    } catch (DataInvalidateException ex) {
                        throw ex;
                    } catch (Exception ex) {
                        throw new DataInvalidateException(csvHeader + " entry not found in master list for "
                                + columnHeader + " dropdown.");
                    }
                } else if (columnConfig.getString("xtype").equals("8")) {//Reference Drop down
                    try {
                        if (vDataValue != null) {
                            if (!StringUtil.isNullOrEmpty(vDataValue.toString())) {
                                String pref = (String) requestParams.get("masterPreference"); //0:Skip Record, 1:Skip Column, 2:Add new
                                if (columnConfig.has("refModule") && columnConfig.has("refDataColumn")
                                        && columnConfig.has("refFetchColumn") && columnConfig.has("configid")) {
                                    List list = getRefData(requestParams, columnConfig.getString("refModule"),
                                            columnConfig.getString("refDataColumn"),
                                            columnConfig.getString("refFetchColumn"),
                                            columnConfig.getString("configid"), vDataValue, importDao);
                                    if (list.size() == 0) {
                                        if (pref.equalsIgnoreCase("0")) { //Skip Record
                                            throw new DataInvalidateException(
                                                    csvHeader + " entry not found in the list for "
                                                            + columnHeader + " dropdown."); // Throw ex to skip record.
                                        } else if (pref.equalsIgnoreCase("1")) {
                                            vDataValue = null; // Put 'null' value to skip column data.
                                        } else if (pref.equalsIgnoreCase("2")) { //2:Add new
                                            if (columnConfig.getString("refModule")
                                                    .equalsIgnoreCase(Constants.Crm_users_pojo)) {//For users custom column.
                                                throw new DataInvalidateException(
                                                        csvHeader + " entry not found in the list for "
                                                                + columnHeader + " dropdown."); // Throw ex to skip record.
                                            }
                                        }
                                    } else {
                                        vDataValue = list.get(0).toString();
                                    }
                                } else {
                                    throw new DataInvalidateException("Incorrect reference mapping("
                                            + columnHeader + ") for " + csvHeader + ".");
                                }
                            }
                        } /* else {
                          throw new DataInvalidateException("Incorrect reference mapping("+columnHeader+") for "+csvHeader+".");
                          }*/
                    } catch (ServiceException ex) {
                        throw new DataInvalidateException(
                                "Incorrect reference mapping(" + columnHeader + ") for " + csvHeader + ".");
                    } catch (DataInvalidateException ex) {
                        throw ex;
                    } catch (Exception ex) {
                        throw new DataInvalidateException(csvHeader + " entry not found in master list for "
                                + columnHeader + " dropdown.");
                    }
                } else {
                    if (!validatetype.equalsIgnoreCase("date") && maxLength > 0 && data != null
                            && data.length() > maxLength) {
                        throw new DataInvalidateException(
                                "Data length greater than " + maxLength + " for column " + csvHeader + ".");
                    }
                }
                createCustomColumnJSON(jobj, columnConfig, vDataValue);

                customfield.put(jobj);
                if (dataMap.containsKey(column)) {
                    dataMap.remove(column);
                }
            } else {
                if (validatetype.equalsIgnoreCase("string") && maxLength > 0 && data != null
                        && data.length() > maxLength) {
                    throw new DataInvalidateException(
                            "Data length greater than " + maxLength + " for column " + csvHeader + ".");
                }
                dataMap.put(column, vDataValue);
            }
        }
    } else { // If no validation type then check allow null property[SK]
        if (vDataValue == null && columnConfig.has("isNotNull") && columnConfig.getBoolean("isNotNull")) {
            throw new DataInvalidateException(
                    "Empty data found in " + csvHeader + ". Can not set empty data for " + columnHeader + ".");
        }
        if (data != null && maxLength > 0 && data.length() > maxLength) {
            throw new DataInvalidateException(
                    "Data length greater than " + maxLength + " for column " + csvHeader + ".");
        }
    }
}

From source file:com.lp.webapp.zemecs.CommandZE.java

public synchronized String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {

    super.execute(request, response);

    if (request.getCookies() != null) {
        for (int i = 0; i < request.getCookies().length; i++) {
            Cookie cookie = request.getCookies()[i];
            cookie.setMaxAge(10000000);/*from  w  w  w  . j a va 2s  . c om*/
            response.addCookie(cookie);
        }
    }

    String mandant = request.getParameter("mandant");

    Locale localeLogon = getMandantFac().getLocaleDesHauptmandanten();

    String locale = request.getParameter("locale");

    String localeCookie = getCookieValue("locale", request);

    if (localeCookie != null && localeCookie.length() > 3) {
        locale = localeCookie;
    }

    if (locale != null && locale.length() > 3) {
        localeLogon = new Locale(locale.substring(0, 2), locale.substring(2, 4));
    }

    TheClientDto theclientDto = null;
    synchronized (mutex) {
        theclientDto = getLogonFac().logon(Helper.getFullUsername(sUser),
                Helper.getMD5Hash((sUser + new String("lpwebappzemecs")).toCharArray()), localeLogon, null,
                null, new Timestamp(System.currentTimeMillis()));

        if (mandant != null && mandant.length() > 0) {

            theclientDto = getLogonFac().logon(Helper.getFullUsername(sUser),
                    Helper.getMD5Hash((sUser + "lpwebappzemecs").toCharArray()), localeLogon, mandant,
                    theclientDto, new Timestamp(System.currentTimeMillis()));
        } else {
            BenutzerDto benutzerDto = getBenutzerFac().benutzerFindByCBenutzerkennung("lpwebappzemecs",
                    new String(Helper.getMD5Hash("lpwebappzemecs" + "lpwebappzemecs")));
            mandant = benutzerDto.getMandantCNrDefault();
        }
    }

    getTheClient(request, response).setTheClientDto(theclientDto);

    if (command.equals(TheApp.CMD_ZE_BDESTATION)) {
        String ausweis = request.getParameter("ausweis");
        getTheClient(request, response).setSMsg("");

        if (ausweis != null && ausweis.length() > 1) {
            // Personal suchen
            PersonalDto personalDto = getPersonalFac().personalFindByCAusweis(ausweis.substring(2));
            if (personalDto != null) {
                personalDto.setPartnerDto(
                        getPartnerFac().partnerFindByPrimaryKey(personalDto.getPartnerIId(), theclientDto));

                HashMap<String, Serializable> hmParameter = new HashMap<String, Serializable>();
                ZeitdatenDto zeitdatenDto = new ZeitdatenDto();
                zeitdatenDto.setCWowurdegebucht("BDE-Station " + request.getRemoteHost());
                zeitdatenDto.setPersonalIId(personalDto.getIId());
                hmParameter.put("zeitdaten", zeitdatenDto);
                hmParameter.put("person", personalDto.getPartnerDto().formatFixTitelName1Name2());
                getTheClient(request, response).setData(hmParameter);
                setSJSPNext("bdestation2.jsp");
                return getSJSPNext();
            } else {
                getTheClient(request, response)
                        .setSMsg("Ausweis " + ausweis + " bei diesem Mandanten nicht gefunden! ");
            }
        } else {
            getTheClient(request, response).setSMsg("");
        }
    }
    if (command.equals(TheApp.CMD_ZE_BDESTATION2)) {

        HashMap<String, Serializable> hmParameter = (HashMap<String, Serializable>) getTheClient(request,
                response).getData();
        ZeitdatenDto zeitdatenDto = (ZeitdatenDto) hmParameter.get("zeitdaten");
        zeitdatenDto.setTZeit(new Timestamp(System.currentTimeMillis()));

        String option = request.getParameter("option");
        getTheClient(request, response).setSMsg("");

        ParametermandantDto parameterBdeMitTaetigkeitDto = getParameterFac().getMandantparameter(mandant,
                ParameterFac.KATEGORIE_PERSONAL, ParameterFac.PARAMETER_BDE_MIT_TAETIGKEIT);

        Boolean bBdeMitTaetigkeit = (Boolean) parameterBdeMitTaetigkeitDto.getCWertAsObject();
        com.lp.server.artikel.service.ArtikelDto artikelDtoDefaultArbeiztszeit = null;

        if (bBdeMitTaetigkeit == false) {
            ParametermandantDto parameterDtoDefaultarbeitszeit = getParameterFac().getMandantparameter(mandant,
                    ParameterFac.KATEGORIE_ALLGEMEIN, ParameterFac.PARAMETER_DEFAULT_ARBEITSZEITARTIKEL);

            if (parameterDtoDefaultarbeitszeit != null && parameterDtoDefaultarbeitszeit.getCWert() != null
                    && !parameterDtoDefaultarbeitszeit.getCWert().trim().equals("")) {
                try {
                    artikelDtoDefaultArbeiztszeit = getArtikelFac()
                            .artikelFindByCNr(parameterDtoDefaultarbeitszeit.getCWert(), theclientDto);
                    zeitdatenDto.setArtikelIId(artikelDtoDefaultArbeiztszeit.getIId());

                } catch (RemoteException ex2) {
                    myLogger.error("Default-Arbeitszeitartikel " + parameterDtoDefaultarbeitszeit.getCWert()
                            + " nicht vorhanden.", ex2);
                    setSJSPNext("bdestation.jsp");
                    return getSJSPNext();
                }

            } else {
                myLogger.error("Default-Arbeitszeitartikel " + parameterDtoDefaultarbeitszeit.getCWert()
                        + " nicht definiert.");
                setSJSPNext("bdestation.jsp");
                return getSJSPNext();
            }
        }

        if (option != null && option.length() > 2) {

            // Auftrag
            if (option.substring(0, 2).equals("$A") || option.substring(0, 3).equals("$EA")) {
                try {

                    ParametermandantDto parameterDto = getParameterFac().getMandantparameter(mandant,
                            ParameterFac.KATEGORIE_ALLGEMEIN,
                            ParameterFac.PARAMETER_BELEGNUMMERNFORMAT_STELLEN_GESCHAEFTSJAHR);

                    if (parameterDto != null) {
                        if (parameterDto.getCWert() != null && parameterDto.getCWert().equals("4")) {
                            if (option.charAt(4) == 47) {
                                option = "$A"
                                        + Helper.konvertiereDatum2StelligAuf4Stellig(option.substring(2, 4))
                                        + option.substring(4);
                            }
                        }
                    }
                    AuftragDto auftragDto = null;
                    if (option.substring(0, 2).equals("$A")) {

                        auftragDto = getAuftragFac().auftragFindByMandantCNrCNr(mandant, option.substring(2),
                                theclientDto);
                    } else {
                        auftragDto = getAuftragFac().auftragFindByMandantCNrCNr(mandant, option.substring(3),
                                theclientDto);

                    }

                    AuftragpositionDto[] auftragpositionDtos = getAuftragpositionFac()
                            .auftragpositionFindByAuftrag(auftragDto.getIId());

                    if (auftragDto.getAuftragstatusCNr()
                            .equals(com.lp.server.auftrag.service.AuftragServiceFac.AUFTRAGSTATUS_ERLEDIGT)) {
                        setSJSPNext("bdestation.jsp");
                        getTheClient(request, response).setSMsg("Auf Auftrag " + option.substring(2)
                                + " mit Status " + auftragDto.getAuftragstatusCNr().trim()
                                + " darf nicht gebucht werden! ");
                        return getSJSPNext();
                    } else {
                        if (auftragpositionDtos != null && auftragpositionDtos.length > 0) {
                            zeitdatenDto.setIBelegartpositionid(auftragpositionDtos[0].getIId());
                            zeitdatenDto.setIBelegartid(auftragpositionDtos[0].getBelegIId());
                            zeitdatenDto.setCBelegartnr(LocaleFac.BELEGART_AUFTRAG);
                            hmParameter.put("beleg", "A" + option.substring(2));

                            MaschineDto maschineDto = new MaschineDto();
                            maschineDto.setCBez("");
                            hmParameter.put("maschine", maschineDto);

                            if (option.substring(0, 2).equals("$A")) {

                                if (bBdeMitTaetigkeit == false) {
                                    setSJSPNext("bdestation.jsp");
                                    getTheClient(request, response).setSMsg(
                                            getMeldungGebuchtFuerBDE(getTheClient(request, response).getData(),
                                                    artikelDtoDefaultArbeiztszeit.getCNr(), theclientDto));

                                    getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, false,
                                            theclientDto);
                                    return getSJSPNext();

                                } else {
                                    setSJSPNext("bdestation4.jsp");
                                }
                            } else {
                                hmParameter.put("beleg", "A" + option.substring(3));
                                setSJSPNext("bdestation3gutschlecht.jsp");

                            }

                            return getSJSPNext();
                        } else {
                            getTheClient(request, response)
                                    .setSMsg("Auftrag " + option.substring(2) + " hat keine Positionen! ");
                            setSJSPNext("bdestation.jsp");
                            return getSJSPNext();
                        }
                    }

                } catch (EJBExceptionLP ex) {
                    getTheClient(request, response).setSMsg(
                            "Auftrag '" + option.substring(2) + "' bei diesem Mandanten nicht gefunden! ");
                    setSJSPNext("bdestation.jsp");
                    return getSJSPNext();
                }
            } // Los
            else if (option.substring(0, 2).equals("$L") || option.substring(0, 3).equals("$EL")) {
                try {

                    ParametermandantDto parameterDto = getParameterFac().getMandantparameter(mandant,
                            ParameterFac.KATEGORIE_ALLGEMEIN,
                            ParameterFac.PARAMETER_BELEGNUMMERNFORMAT_STELLEN_GESCHAEFTSJAHR);

                    if (parameterDto != null) {
                        if (parameterDto.getCWert() != null && parameterDto.getCWert().equals("4")) {
                            if (option.charAt(4) == 47) {
                                option = "$L"
                                        + Helper.konvertiereDatum2StelligAuf4Stellig(option.substring(2, 4))
                                        + option.substring(4);
                            }
                        }
                    }

                    com.lp.server.fertigung.service.LosDto losDto = null;

                    if (option.substring(0, 2).equals("$L")) {
                        losDto = getFertigungFac().losFindByCNrMandantCNr(option.substring(2), mandant);

                    } else {
                        losDto = getFertigungFac().losFindByCNrMandantCNr(option.substring(3), mandant);

                    }

                    // WH 18-01-2006: Los benoetigt keine Positionen

                    if (losDto.getStatusCNr()
                            .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_ANGELEGT)
                            || losDto.getStatusCNr()
                                    .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_AUSGEGEBEN)
                            || losDto.getStatusCNr()
                                    .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_GESTOPPT)
                            || losDto.getStatusCNr()
                                    .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_ERLEDIGT)
                            || losDto.getStatusCNr()
                                    .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_STORNIERT)) {
                        getTheClient(request, response)
                                .setSMsg("Auf Los " + option.substring(2) + " mit Status "
                                        + losDto.getStatusCNr().trim() + " darf nicht gebucht werden! ");
                        setSJSPNext("bdestation.jsp");
                        return getSJSPNext();
                    } else {
                        zeitdatenDto.setIBelegartid(losDto.getIId());
                        zeitdatenDto.setCBelegartnr(LocaleFac.BELEGART_LOS);

                        if (option.substring(0, 2).equals("$L")) {

                            hmParameter.put("beleg", "L" + option.substring(2));

                            if (bBdeMitTaetigkeit == false) {
                                setSJSPNext("bdestation.jsp");
                                getTheClient(request, response).setSMsg(
                                        getMeldungGebuchtFuerBDE(getTheClient(request, response).getData(),
                                                artikelDtoDefaultArbeiztszeit.getCNr(), theclientDto));

                                getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, true,
                                        theclientDto);
                                return getSJSPNext();
                            } else {
                                setSJSPNext("bdestation3.jsp");
                            }
                        } else {
                            hmParameter.put("beleg", "L" + option.substring(3));
                            setSJSPNext("bdestation3gutschlecht.jsp");
                        }

                        return getSJSPNext();

                    }
                } catch (EJBExceptionLP ex) {
                    getTheClient(request, response)
                            .setSMsg("Los '" + option.substring(2) + "' bei diesem Mandanten nicht gefunden! ");
                    setSJSPNext("bdestation.jsp");
                    return getSJSPNext();
                }

            } // Kombi-Code
            else if (option.length() > 1 && option.substring(0, 2).equals("$V")) {
                setSJSPNext("bdestation.jsp");

                if (option.length() < 12) {

                    getTheClient(request, response).setSMsg("Kombicode muss 10-Stellig sein ");
                    return getSJSPNext();
                }

                try {

                    ParametermandantDto parameter = getParameterFac().getMandantparameter(
                            theclientDto.getMandant(), ParameterFac.KATEGORIE_FERTIGUNG,
                            ParameterFac.PARAMETER_LOSNUMMER_AUFTRAGSBEZOGEN);
                    int iVerlaengerungLosnummer = 0;
                    if ((Integer) parameter.getCWertAsObject() >= 1) {
                        iVerlaengerungLosnummer = 2;
                    }

                    com.lp.server.fertigung.service.LosDto losDto = getFertigungFac()
                            .losFindByCNrMandantCNr(option.substring(2, 12 + iVerlaengerungLosnummer), mandant);

                    // WH 18-01-2006: Los benoetigt keine Positionen

                    if (losDto.getStatusCNr()
                            .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_ANGELEGT)
                            || losDto.getStatusCNr()
                                    .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_AUSGEGEBEN)
                            || losDto.getStatusCNr()
                                    .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_GESTOPPT)
                            || losDto.getStatusCNr()
                                    .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_ERLEDIGT)
                            || losDto.getStatusCNr()
                                    .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_STORNIERT)) {
                        getTheClient(request, response)
                                .setSMsg("Auf Los " + option.substring(2) + " mit Status "
                                        + losDto.getStatusCNr().trim() + " darf nicht gebucht werden! ");
                    } else {
                        zeitdatenDto.setIBelegartid(losDto.getIId());
                        zeitdatenDto.setCBelegartnr(LocaleFac.BELEGART_LOS);

                        String maschine = option.substring(12 + iVerlaengerungLosnummer,
                                14 + iVerlaengerungLosnummer);

                        String taetigkeit = option.substring(14 + iVerlaengerungLosnummer);

                        MaschineDto maschineDto = new MaschineDto();
                        maschineDto.setCBez(maschine);
                        hmParameter.put("maschine", maschineDto);

                        hmParameter.put("beleg", "L" + option.substring(2, 12 + iVerlaengerungLosnummer));

                        com.lp.server.artikel.service.ArtikelDto artikelDto = null;
                        try {
                            artikelDto = getArtikelFac().artikelFindByCNr(taetigkeit, theclientDto);

                            zeitdatenDto.setArtikelIId(
                                    getArtikelFac().artikelFindByCNr(taetigkeit, theclientDto).getIId());

                        } catch (RemoteException ex2) {
                            getTheClient(request, response)
                                    .setSMsg("T\u00E4tigkeit '" + taetigkeit + "' nicht gefunden! ");
                            return getSJSPNext();
                        }

                        com.lp.server.fertigung.service.LossollarbeitsplanDto[] dtos = getFertigungFac()
                                .lossollarbeitsplanFindByLosIIdArtikelIIdTaetigkeit(losDto.getIId(),
                                        artikelDto.getIId());

                        if (dtos != null && dtos.length > 0) {

                            if (!maschine.trim().equals("") && !maschine.equals("--")) {

                                try {
                                    Integer maschineIId = getZeiterfassungsFac()
                                            .maschineFindByCIdentifikationsnr(maschine).getIId();

                                    com.lp.server.fertigung.service.LossollarbeitsplanDto[] sollaDtos = getFertigungFac()
                                            .lossollarbeitsplanFindByLosIIdArtikelIIdTaetigkeit(
                                                    zeitdatenDto.getIBelegartid(),
                                                    zeitdatenDto.getArtikelIId());

                                    if (sollaDtos != null && sollaDtos.length > 0) {
                                        MaschinenzeitdatenDto maschinenzeitdatenDto = new MaschinenzeitdatenDto();
                                        maschinenzeitdatenDto.setLossollarbeitsplanIId(sollaDtos[0].getIId());
                                        maschinenzeitdatenDto.setMaschineIId(maschineIId);
                                        maschinenzeitdatenDto
                                                .setPersonalIIdGestartet(zeitdatenDto.getPersonalIId());
                                        maschinenzeitdatenDto.setTVon(zeitdatenDto.getTZeit());
                                        getZeiterfassungsFac().createMaschinenzeitdaten(maschinenzeitdatenDto,
                                                theclientDto);
                                    }

                                } catch (RemoteException ex2) {
                                    getTheClient(request, response)
                                            .setSMsg("Maschine '" + maschine + "' nicht gefunden! ");
                                    return getSJSPNext();
                                }
                            }

                            // PJ 15388
                            if (maschine.equals("--")) {
                                hmParameter.put("fertig", "");
                                if (dtos != null && dtos.length > 0) {

                                    LossollarbeitsplanDto dto = dtos[0];
                                    dto.setBFertig(Helper.boolean2Short(true));

                                    try {
                                        getFertigungFac().updateLossollarbeitsplan(dto, theclientDto);
                                        getTheClient(request, response).setSMsg(getMeldungGebuchtFuerBDE(
                                                getTheClient(request, response).getData(),
                                                taetigkeit.substring(2), theclientDto));
                                        return getSJSPNext();
                                    } catch (EJBExceptionLP ex2) {
                                        getTheClient(request, response).setSMsg("Fehler beim Buchen!");
                                        return getSJSPNext();
                                    }
                                } else {
                                    getTheClient(request, response).setSMsg("Das Los "
                                            + option.substring(2, 12 + iVerlaengerungLosnummer)
                                            + " hat keinen entsprechen Arbeitsgang mit der Artikelnummer "
                                            + taetigkeit.substring(2));
                                    return getSJSPNext();
                                }
                            }

                            zeitdatenDto.setIBelegartpositionid(dtos[0].getIId());
                        } else {
                            com.lp.server.fertigung.service.LossollarbeitsplanDto[] dtosErstePosition = getFertigungFac()
                                    .lossollarbeitsplanFindByLosIId(losDto.getIId());
                            if (dtosErstePosition != null && dtosErstePosition.length > 0) {
                                zeitdatenDto.setIBelegartpositionid(dtosErstePosition[0].getIId());
                            } else {
                                // Bemerkung
                                getTheClient(request, response)
                                        .setSMsg("Los " + option.substring(2) + " hat keine Positionen");
                                return getSJSPNext();

                            }
                        }

                        try {
                            getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, true,
                                    theclientDto);
                            getTheClient(request, response).setSMsg(getMeldungGebuchtFuerBDE(
                                    getTheClient(request, response).getData(), taetigkeit, theclientDto));

                        } catch (EJBExceptionLP ex2) {
                            getTheClient(request, response).setSMsg("Fehler beim Buchen!");
                            return getSJSPNext();
                        }
                    }
                } catch (EJBExceptionLP ex) {
                    getTheClient(request, response)
                            .setSMsg("Los '" + option.substring(2) + "' bei diesem Mandanten nicht gefunden! ");
                    return getSJSPNext();

                }
                return getSJSPNext();
            } // Sondertaetigkeit
            else {

                if (option.substring(1).equals("SALDO")) {

                    java.sql.Timestamp ts = new java.sql.Timestamp(System.currentTimeMillis() - 3600000 * 24);
                    ts = com.lp.util.Helper.cutTimestamp(ts);

                    Calendar c = Calendar.getInstance();
                    c.setTimeInMillis(ts.getTime());

                    String saldoMitUrlaub = "";
                    try {
                        saldoMitUrlaub = getZeiterfassungsFac().erstelleMonatsAbrechnungFuerBDE(
                                zeitdatenDto.getPersonalIId(), new Integer(c.get(Calendar.YEAR)),
                                new Integer(c.get(Calendar.MONTH)), false, new java.sql.Date(ts.getTime()),
                                theclientDto, true, false);

                    } catch (EJBExceptionLP ex7) {

                        if (ex7.getCause() instanceof EJBExceptionLP) {
                            EJBExceptionLP e = (EJBExceptionLP) ex7.getCause();
                            if (e != null && e
                                    .getCode() == EJBExceptionLP.FEHLER_PERSONAL_FEHLER_BEI_EINTRITTSDATUM) {
                                getTheClient(request, response)
                                        .setSMsg(new String("FEHLER_PERSONAL_FEHLER_BEI_EINTRITTSDATUM"));
                                return getSJSPNext();

                            }
                        }
                        getTheClient(request, response).setSMsg(new String(ex7.getMessage()));
                        setSJSPNext("bdestation.jsp");

                    }

                    getTheClient(request, response).setSMsg(saldoMitUrlaub);

                    setSJSPNext("bdestation.jsp");
                    return getSJSPNext();

                } else if (option.substring(1).equals("TAGESSALDO")) {

                    java.sql.Timestamp ts = new java.sql.Timestamp(System.currentTimeMillis() - 3600000 * 24);

                    Double d = getZeiterfassungsFac().berechneTagesArbeitszeit(zeitdatenDto.getPersonalIId(),
                            new java.sql.Date(System.currentTimeMillis()), theclientDto);

                    StringBuffer sb = new StringBuffer();
                    sb.append("Tagesarbeitszeit bis jetzt: "
                            + Helper.rundeKaufmaennisch(new BigDecimal(d.doubleValue()), 2).doubleValue()
                            + "h");
                    sb.append("\r\n");

                    getTheClient(request, response).setSMsg(new String(sb));
                    setSJSPNext("bdestation.jsp");
                    return getSJSPNext();

                } else {

                    try {
                        TaetigkeitDto taetigkeitDto = getZeiterfassungsFac()
                                .taetigkeitFindByCNr(option.substring(1), theclientDto);

                        zeitdatenDto.setTaetigkeitIId(taetigkeitDto.getIId());
                        getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, true, theclientDto);
                        getTheClient(request, response).setSMsg(
                                getMeldungGebuchtFuerBDE(hmParameter, option.substring(1), theclientDto));
                    } catch (EJBExceptionLP ex1) {
                        getTheClient(request, response)
                                .setSMsg("Sondert\u00E4tigkeit '" + option.substring(1) + "' nicht gefunden! ");
                    }
                    hmParameter.put("zeitdaten", zeitdatenDto);
                    setSJSPNext("bdestation.jsp");

                }
                getTheClient(request, response).setData(hmParameter);
                return getSJSPNext();
            }
        }

    } else if (command.equals(TheApp.CMD_ZE_BDESTATION3)) {

        HashMap<String, Serializable> hmParameter = (HashMap<String, Serializable>) getTheClient(request,
                response).getData();
        ZeitdatenDto zeitdatenDto = (ZeitdatenDto) hmParameter.get("zeitdaten");
        zeitdatenDto.setTZeit(new Timestamp(System.currentTimeMillis()));

        String option = request.getParameter("option");
        getTheClient(request, response).setSMsg("");

        if (option != null && option.length() > 0) {

            if (option.equals("$PLUS")) {
                // CK: PJ5589
                String beleg = (String) hmParameter.get("beleg");

                if (beleg.substring(0, 1).equals("L")) {
                    com.lp.server.fertigung.service.LosDto losDto = getFertigungFac()
                            .losFindByCNrMandantCNr(beleg.substring(1), mandant);

                    ZeitverteilungDto zeitverteilungDto = new ZeitverteilungDto();
                    zeitverteilungDto.setLosIId(losDto.getIId());
                    zeitverteilungDto.setTZeit(new Timestamp(System.currentTimeMillis()));
                    zeitverteilungDto.setPersonalIId(zeitdatenDto.getPersonalIId());
                    try {
                        getZeiterfassungsFac().createZeitverteilung(zeitverteilungDto, theclientDto);
                    } catch (EJBExceptionLP e) {
                        hmParameter.remove("beleg");
                        getTheClient(request, response).setData(hmParameter);
                        getTheClient(request, response)
                                .setSMsg("Los '" + losDto.getCNr() + "' wurde bereits mit $PLUS gebucht");
                        setSJSPNext("bdestation2.jsp");
                        return getSJSPNext();
                    }

                } else {
                    getTheClient(request, response).setSMsg("$PLUS ist nur f\u00FCr Lose m\u00F6glich.");
                    setSJSPNext("bdestation.jsp");
                    return getSJSPNext();
                }

                hmParameter.remove("beleg");
                getTheClient(request, response).setData(hmParameter);
                setSJSPNext("bdestation2.jsp");
                return getSJSPNext();

            } else if (option.equals("$STORNO")) {
                getZeiterfassungsFac().removeZeitverteilungByPersonalIIdUndTag(zeitdatenDto.getPersonalIId(),
                        new Timestamp(System.currentTimeMillis()));
                hmParameter.remove("beleg");
                getTheClient(request, response).setData(hmParameter);
                setSJSPNext("bdestation2.jsp");
            } else if (option.equals("$SPERREN")) {

                String beleg = (String) hmParameter.get("beleg");
                if (beleg.substring(0, 1).equals("L")) {
                    com.lp.server.fertigung.service.LosDto losDto = getFertigungFac()
                            .losFindByCNrMandantCNr(beleg.substring(1), mandant);

                    if (losDto.getStuecklisteIId() != null) {
                        Integer artikelIId = getStuecklisteFac()
                                .stuecklisteFindByPrimaryKey(losDto.getStuecklisteIId(), theclientDto)
                                .getArtikelIId();

                        SperrenDto sDto = getArtikelFac().sperrenFindBDurchfertigung(theclientDto);

                        if (sDto != null) {

                            ArtikelsperrenDto aspDtoVorhanden = getArtikelFac()
                                    .artikelsperrenFindByArtikelIIdSperrenIIdOhneExc(artikelIId, sDto.getIId());
                            if (aspDtoVorhanden == null) {
                                ArtikelsperrenDto spDto = new ArtikelsperrenDto();
                                spDto.setArtikelIId(artikelIId);
                                spDto.setSperrenIId(sDto.getIId());

                                PersonalDto pDto = getPersonalFac()
                                        .personalFindByPrimaryKey(zeitdatenDto.getPersonalIId(), theclientDto);

                                String grund = beleg + " " + pDto.getPartnerDto().getCName1nachnamefirmazeile1()
                                        + " " + pDto.getPartnerDto().getCName2vornamefirmazeile2();

                                if (grund.length() > 80) {
                                    grund = grund.substring(0, 79);
                                }

                                spDto.setCGrund(grund);
                                getArtikelFac().createArtikelsperren(spDto, theclientDto);
                                getTheClient(request, response)
                                        .setSMsg("Los " + losDto.getCNr() + " durch Fertigung gesperrt.");
                                setSJSPNext("bdestation.jsp");
                                return getSJSPNext();
                            } else {
                                getTheClient(request, response)
                                        .setSMsg("St\u00FCckliste bereits durch Fertigung gesperrt.");
                                setSJSPNext("bdestation.jsp");
                                return getSJSPNext();
                            }
                        } else {
                            getTheClient(request, response)
                                    .setSMsg("Fertigungssperre in Grunddaten nicht definiert.");
                            setSJSPNext("bdestation.jsp");
                            return getSJSPNext();
                        }

                    }

                    setSJSPNext("bdestation.jsp");
                    return getSJSPNext();
                }

            } else if (option.equals("$FERTIG")) {
                hmParameter.put("fertig", "");
                MaschineDto maschineDto = new MaschineDto();
                maschineDto.setCBez("Fertig");
                hmParameter.put("maschine", maschineDto);
                getTheClient(request, response).setData(hmParameter);
                setSJSPNext("bdestation4.jsp");
                return getSJSPNext();
            } else {

                setSJSPNext("bdestation.jsp");
                // MASCHINE
                if (option.substring(0, 2).equals("$M")) {

                    String maschine = option.substring(2);

                    try {
                        MaschineDto maschineDto = getZeiterfassungsFac()
                                .maschineFindByCIdentifikationsnr(maschine);
                        hmParameter.put("zeitdaten", zeitdatenDto);
                        hmParameter.put("maschine", maschineDto);
                        setSJSPNext("bdestation4.jsp");
                        return getSJSPNext();

                    } catch (EJBExceptionLP ex2) {
                        getTheClient(request, response).setSMsg("Maschine '" + maschine + "' nicht gefunden! ");
                        return getSJSPNext();
                    }

                } // TAETIGKEIT
                else {

                    String taetigkeit = option.substring(2);

                    com.lp.server.artikel.service.ArtikelDto artikelDto = null;
                    try {
                        artikelDto = getArtikelFac().artikelFindByCNr(taetigkeit, theclientDto);

                        zeitdatenDto.setArtikelIId(
                                getArtikelFac().artikelFindByCNr(taetigkeit, theclientDto).getIId());

                    } catch (EJBExceptionLP ex2) {
                        getTheClient(request, response)
                                .setSMsg("T\u00E4tigkeit '" + taetigkeit + "' nicht gefunden! ");
                        return getSJSPNext();
                    }

                    String beleg = (String) hmParameter.get("beleg");

                    if (beleg.substring(0, 1).equals("L")) {
                        com.lp.server.fertigung.service.LosDto losDto = getFertigungFac()
                                .losFindByCNrMandantCNr(beleg.substring(1), mandant);

                        ZeitverteilungDto[] zvDtos = getZeiterfassungsFac()
                                .zeitverteilungFindByPersonalIIdUndTag(zeitdatenDto.getPersonalIId(),
                                        new Timestamp(System.currentTimeMillis()));
                        if (zvDtos != null & zvDtos.length > 0) {

                            if (zvDtos[0].getArtikelIId() == null) {
                                // Abschlussbuchung eintragen
                                ZeitverteilungDto zv = zvDtos[0];
                                zv.setIId(null);
                                zv.setLosIId(losDto.getIId());
                                zv.setArtikelIId(artikelDto.getIId());
                                try {
                                    getZeiterfassungsFac().createZeitverteilung(zv, theclientDto);
                                } catch (EJBExceptionLP e) {
                                    hmParameter.remove("beleg");
                                    getTheClient(request, response).setData(hmParameter);
                                    getTheClient(request, response).setSMsg(
                                            "Los '" + losDto.getCNr() + "' wurde bereits mit $PLUS gebucht");
                                    setSJSPNext("bdestation2.jsp");
                                    return getSJSPNext();
                                }

                                hmParameter.remove("beleg");
                                getTheClient(request, response).setData(hmParameter);
                                getTheClient(request, response)
                                        .setSMsg("Beginnbuchungen f\u00FCr 'Zeitverteilung' abgeschlossen");
                                setSJSPNext("bdestation.jsp");
                                return getSJSPNext();
                            }
                        }

                        com.lp.server.fertigung.service.LossollarbeitsplanDto[] dtos = getFertigungFac()
                                .lossollarbeitsplanFindByLosIIdArtikelIIdTaetigkeit(losDto.getIId(),
                                        artikelDto.getIId());

                        if (dtos != null && dtos.length > 0) {
                            zeitdatenDto.setIBelegartpositionid(dtos[0].getIId());
                        } else {
                            com.lp.server.fertigung.service.LossollarbeitsplanDto[] dtosErstePosition = getFertigungFac()
                                    .lossollarbeitsplanFindByLosIId(losDto.getIId());
                            if (dtosErstePosition != null && dtosErstePosition.length > 0) {
                                zeitdatenDto.setIBelegartpositionid(dtosErstePosition[0].getIId());
                            } else {
                                // Bemerkung
                                getTheClient(request, response)
                                        .setSMsg("Los " + beleg.substring(1) + " hat keine Positionen");
                                return getSJSPNext();

                            }
                        }
                    }

                    try {
                        getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, true, theclientDto);
                        getTheClient(request, response).setSMsg(getMeldungGebuchtFuerBDE(
                                getTheClient(request, response).getData(), taetigkeit, theclientDto));

                        return getSJSPNext();
                    } catch (EJBExceptionLP ex2) {
                        getTheClient(request, response).setSMsg("Fehler beim Buchen!");
                        return getSJSPNext();
                    }
                }
            }
        }
    } else if (command.equals(TheApp.CMD_ZE_BDESTATION3GUTSCHLECHT)) {
        HashMap<Object, Object> hmParameter = (HashMap<Object, Object>) getTheClient(request, response)
                .getData();
        ZeitdatenDto zeitdatenDto = (ZeitdatenDto) hmParameter.get("zeitdaten");

        String gutstueck = request.getParameter("gutstueck");
        if (gutstueck.equals("")) {
            gutstueck = "0";
        }
        String schlechtstueck = request.getParameter("schlechtstueck");
        if (schlechtstueck.equals("")) {
            schlechtstueck = "0";
        }

        BigDecimal bdGutstueck = null;
        BigDecimal bdSchlechtstueck = null;
        try {
            bdGutstueck = new BigDecimal(gutstueck);
            bdSchlechtstueck = new BigDecimal(schlechtstueck);
        } catch (NumberFormatException ex9) {
            getTheClient(request, response)
                    .setSMsg("Gut/Schlechtst\u00FCck d\u00FCrfen nur aus Zahlen bestehen.");
            setSJSPNext("bdestation.jsp");
            return getSJSPNext();
        }

        if (bdGutstueck.doubleValue() < 0 || bdSchlechtstueck.doubleValue() < 0) {
            getTheClient(request, response).setSMsg("Gut/Schlechtst\u00FCck m\u00FCssen Positiv sein.");
            setSJSPNext("bdestation.jsp");
            return getSJSPNext();
        }
        Integer taetigkeitIId_Ende = getZeiterfassungsFac()
                .taetigkeitFindByCNr(ZeiterfassungFac.TAETIGKEIT_ENDE, theclientDto).getIId();
        ZeitdatenDto zeitdatenDtoEnde = new ZeitdatenDto();
        zeitdatenDtoEnde.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime() + 1000));
        zeitdatenDtoEnde.setTaetigkeitIId(taetigkeitIId_Ende);
        zeitdatenDtoEnde.setPersonalIId(zeitdatenDto.getPersonalIId());

        // Hole letzten begonnenen Auftrag und hinterlege gut/schlechtstueck
        Session session = FLRSessionFactory.getFactory().openSession();
        org.hibernate.Criteria liste = session.createCriteria(FLRZeitdaten.class);
        liste.add(Expression.eq(ZeiterfassungFac.FLR_ZEITDATEN_PERSONAL_I_ID, zeitdatenDto.getPersonalIId()));
        liste.add(Expression.gt(ZeiterfassungFac.FLR_ZEITDATEN_T_ZEIT,
                Helper.cutTimestamp(zeitdatenDto.getTZeit())));

        liste.addOrder(Order.desc(ZeiterfassungFac.FLR_ZEITDATEN_T_ZEIT));
        List<?> letzerAuftrag = liste.list();

        Iterator<?> it = letzerAuftrag.iterator();
        ZeitdatenDto letzterAuftrag = null;

        while (it.hasNext()) {
            FLRZeitdaten flrLetzerAuftrag = (FLRZeitdaten) it.next();

            if (flrLetzerAuftrag.getC_belegartnr() != null && flrLetzerAuftrag.getI_belegartid() != null) {
                if (flrLetzerAuftrag.getC_belegartnr().equals(zeitdatenDto.getCBelegartnr())
                        && flrLetzerAuftrag.getI_belegartid().equals(zeitdatenDto.getIBelegartid())) {
                    letzterAuftrag = getZeiterfassungsFac()
                            .zeitdatenFindByPrimaryKey(flrLetzerAuftrag.getI_id(), theclientDto);
                    break;
                }
            } else if (flrLetzerAuftrag.getTaetigkeit_i_id() != null
                    && flrLetzerAuftrag.getTaetigkeit_i_id().equals(taetigkeitIId_Ende)) {
                break;
            }

        }

        if (letzterAuftrag != null) {
            // Hier eintragen
            // letzterAuftrag.setNGut(bdGutstueck);
            // letzterAuftrag.setNSchlecht(bdSchlechtstueck);
            getZeiterfassungsFac().updateZeitdaten(letzterAuftrag, theclientDto);
            // und buche ENDE
            getZeiterfassungsFac().createZeitdaten(zeitdatenDtoEnde, false, false, false, theclientDto);

        } else {
            // was nun?
            // Beginn und ende Buchen
            getZeiterfassungsFac().createZeitdaten(zeitdatenDto, false, false, false, theclientDto);
            getZeiterfassungsFac().createZeitdaten(zeitdatenDtoEnde, false, false, false, theclientDto);

        }

        session.close();
        getTheClient(request, response).setSMsg(
                getMeldungGebuchtFuerBDE(getTheClient(request, response).getData(), null, theclientDto));

        setSJSPNext("bdestation.jsp");
        return getSJSPNext();
    }

    else if (command.equals(TheApp.CMD_ZE_BDESTATION4)) {
        HashMap<?, ?> hmParameter = (HashMap<?, ?>) getTheClient(request, response).getData();
        ZeitdatenDto zeitdatenDto = (ZeitdatenDto) hmParameter.get("zeitdaten");
        zeitdatenDto.setTZeit(new Timestamp(System.currentTimeMillis()));

        String taetigkeit = request.getParameter("taetigkeit");
        getTheClient(request, response).setSMsg("");
        if (taetigkeit != null && taetigkeit.length() > 0) {

            setSJSPNext("bdestation.jsp");
            com.lp.server.artikel.service.ArtikelDto artikelDto = null;
            try {
                artikelDto = getArtikelFac().artikelFindByCNr(taetigkeit.substring(2), theclientDto);

                zeitdatenDto.setArtikelIId(artikelDto.getIId());
            } catch (EJBExceptionLP ex2) {
                getTheClient(request, response)
                        .setSMsg("T\u00E4tigkeit '" + taetigkeit.substring(2) + "' nicht gefunden! ");
                return getSJSPNext();
            }

            String beleg = (String) hmParameter.get("beleg");

            if (beleg.substring(0, 1).equals("L")) {
                com.lp.server.fertigung.service.LosDto losDto = getFertigungFac()
                        .losFindByCNrMandantCNr(beleg.substring(1), mandant);

                com.lp.server.fertigung.service.LossollarbeitsplanDto[] dtos = getFertigungFac()
                        .lossollarbeitsplanFindByLosIIdArtikelIIdTaetigkeit(losDto.getIId(),
                                artikelDto.getIId());

                if (hmParameter.containsKey("fertig")) {

                    if (dtos != null && dtos.length > 0) {

                        LossollarbeitsplanDto dto = dtos[0];
                        dto.setBFertig(Helper.boolean2Short(true));

                        ParametermandantDto parameterDtoTriggerTops = getParameterFac().getMandantparameter(
                                mandant, ParameterFac.KATEGORIE_FERTIGUNG,
                                ParameterFac.PARAMETER_TRIGGERT_TRUMPF_TOPS_ABLIEFERUNG);

                        try {
                            getFertigungFac().updateLossollarbeitsplan(dto, theclientDto);

                            // PJ 17916
                            if (parameterDtoTriggerTops.getCWert() != null
                                    && parameterDtoTriggerTops.getCWert().trim().length() > 0) {
                                ArtikelDto aDto = getArtikelFac().artikelFindByCNrMandantCNrOhneExc(
                                        parameterDtoTriggerTops.getCWert().trim(), theclientDto.getMandant());

                                if (aDto == null) {
                                    getTheClient(request, response).setSMsg(
                                            "Der Artikel, der im Parameter TRIGGERT_TRUMPF_TOPS_ABLIEFERUNG hinterlegt ist, exisitiert nicht! "
                                                    + parameterDtoTriggerTops.getCWert());
                                    return getSJSPNext();
                                }
                                if (aDto != null && aDto.getIId().equals(artikelDto.getIId())) {
                                    getFertigungFac().bucheTOPSArtikelAufHauptLager(losDto.getIId(),
                                            theclientDto, null);
                                }
                            }

                            getTheClient(request, response)
                                    .setSMsg(getMeldungGebuchtFuerBDE(getTheClient(request, response).getData(),
                                            taetigkeit.substring(2), theclientDto));
                            return getSJSPNext();
                        } catch (EJBExceptionLP ex2) {
                            getTheClient(request, response).setSMsg("Fehler beim Buchen!");
                            return getSJSPNext();
                        }
                    } else {
                        getTheClient(request, response).setSMsg("Das Los " + beleg.substring(1)
                                + " hat keinen entsprechen Arbeitsgang mit der Artikelnummer "
                                + taetigkeit.substring(2));
                        return getSJSPNext();
                    }

                }

                if (dtos != null && dtos.length > 0) {
                    zeitdatenDto.setIBelegartpositionid(dtos[0].getIId());
                } else {
                    com.lp.server.fertigung.service.LossollarbeitsplanDto[] dtosErstePosition = getFertigungFac()
                            .lossollarbeitsplanFindByLosIId(losDto.getIId());
                    if (dtosErstePosition != null && dtosErstePosition.length > 0) {
                        zeitdatenDto.setIBelegartpositionid(dtosErstePosition[0].getIId());
                    } else {
                        // Bemerkung
                        getTheClient(request, response)
                                .setSMsg("Los " + beleg.substring(1) + " hat keine Positionen");
                        return getSJSPNext();

                    }
                }
            }

            // Maschinenzeitdaten buchen (geht nur auf Los)

            if (hmParameter.containsKey("maschine") && zeitdatenDto.getCBelegartnr() != null
                    && zeitdatenDto.getCBelegartnr().equals(LocaleFac.BELEGART_LOS)
                    && zeitdatenDto.getIBelegartid() != null) {
                MaschineDto maschineDto = (MaschineDto) hmParameter.get("maschine");

                com.lp.server.fertigung.service.LossollarbeitsplanDto[] dtos = getFertigungFac()
                        .lossollarbeitsplanFindByLosIIdArtikelIIdTaetigkeit(zeitdatenDto.getIBelegartid(),
                                zeitdatenDto.getArtikelIId());

                if (dtos != null && dtos.length > 0) {
                    MaschinenzeitdatenDto maschinenzeitdatenDto = new MaschinenzeitdatenDto();
                    maschinenzeitdatenDto.setPersonalIIdGestartet(zeitdatenDto.getPersonalIId());
                    maschinenzeitdatenDto.setTVon(zeitdatenDto.getTZeit());
                    maschinenzeitdatenDto.setLossollarbeitsplanIId(dtos[0].getIId());
                    maschinenzeitdatenDto.setMaschineIId(maschineDto.getIId());
                    getZeiterfassungsFac().createMaschinenzeitdaten(maschinenzeitdatenDto, theclientDto);

                }
            }

            try {
                getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, false, theclientDto);
                getTheClient(request, response).setSMsg(getMeldungGebuchtFuerBDE(
                        getTheClient(request, response).getData(), taetigkeit.substring(2), theclientDto));
                return getSJSPNext();
            } catch (EJBExceptionLP ex2) {
                getTheClient(request, response).setSMsg("Fehler beim Buchen!");
                return getSJSPNext();
            }

        }
    } else if (command.equals(TheApp.CMD_ZE_MECS_ONLCHECK)) {
        String beleg = request.getParameter("beleg");

        if (beleg == null) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'beleg' muss angegeben werden");
            return null;
        }

        beleg = beleg.trim();

        if (beleg.length() < 2) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                    "Parameter 'beleg' muss mindestens 2 Zeichen lang sein");
            return null;
        }

        String status = null;
        BigDecimal offeneMenge = new BigDecimal(0);

        String ueberliefernErlaubt = "1";
        try {

            if (beleg.substring(0, 2).equals("$A")) {

                AuftragDto auftragDto = getAuftragFac().auftragFindByMandantCNrCNr(mandant, beleg.substring(2),
                        theclientDto);
                status = auftragDto.getAuftragstatusCNr();

            } else if (beleg.substring(0, 2).equals("$L")) {
                LosDto losDto = getFertigungFac().losFindByCNrMandantCNr(beleg.substring(2), mandant);
                status = losDto.getStatusCNr();

                BigDecimal erledigteMenge = getFertigungFac().getErledigteMenge(losDto.getIId(), theclientDto);
                offeneMenge = losDto.getNLosgroesse().subtract(erledigteMenge);

                if (losDto.getStuecklisteIId() != null) {
                    StuecklisteDto stkDto = getStuecklisteFac()
                            .stuecklisteFindByPrimaryKey(losDto.getStuecklisteIId(), theclientDto);

                    if (Helper.short2boolean(stkDto.getBUeberlieferbar()) == false) {
                        ueberliefernErlaubt = "0";
                    }

                }

            }

        } catch (EJBExceptionLP ex8) {
            status = "Beleg existiert nicht";
        }

        StringBuffer sb = new StringBuffer();

        sb.append(Helper.fitString2Length(beleg, 40, ' '));
        sb.append(Helper.fitString2Length(status, 40, ' '));

        // Offene Menge 17 stellig

        DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance();
        dfs.setDecimalSeparator('.');
        DecimalFormat dFormat = new DecimalFormat("0.0000", dfs);
        if (offeneMenge.doubleValue() < 0) {
            sb.append("-");
        } else {
            sb.append(" ");
        }
        sb.append(Helper.fitString2LengthAlignRight(dFormat.format(offeneMenge.abs()), 16, ' '));
        sb.append(ueberliefernErlaubt);
        sb.append("\r\n");

        getTheClient(request, response).setSMsg(new String(sb));

    } else if (command.equals(TheApp.CMD_ZE_MECS_ONLINECHECK_ABL)) {
        String beleg = request.getParameter("beleg");
        String menge = request.getParameter("menge");

        if (beleg == null) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'beleg' muss angegeben werden");
            return null;
        }
        if (menge == null) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'menge' muss angegeben werden");
            return null;
        }

        BigDecimal nMenge = new BigDecimal(menge.trim());

        beleg = beleg.trim();

        if (beleg.length() < 2) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                    "Parameter 'beleg' muss mindestens 2 Zeichen lang sein");
            return null;
        }

        String status = null;
        BigDecimal offeneMenge = new BigDecimal(0);
        boolean ueberliefernErlaubt = true;
        try {

            if (beleg.substring(0, 2).equals("$A")) {

                AuftragDto auftragDto = getAuftragFac().auftragFindByMandantCNrCNr(mandant, beleg.substring(2),
                        theclientDto);
                status = auftragDto.getAuftragstatusCNr();

            } else if (beleg.substring(0, 2).equals("$L")) {
                LosDto losDto = getFertigungFac().losFindByCNrMandantCNr(beleg.substring(2), mandant);
                status = losDto.getStatusCNr();

                BigDecimal erledigteMenge = getFertigungFac().getErledigteMenge(losDto.getIId(), theclientDto);
                offeneMenge = losDto.getNLosgroesse().subtract(erledigteMenge);
                if (losDto.getStuecklisteIId() != null) {
                    StuecklisteDto stkDto = getStuecklisteFac()
                            .stuecklisteFindByPrimaryKey(losDto.getStuecklisteIId(), theclientDto);
                    ueberliefernErlaubt = Helper.short2boolean(stkDto.getBUeberlieferbar());
                }

            }

        } catch (EJBExceptionLP ex8) {
            status = "Beleg existiert nicht";
        }

        StringBuffer sb = new StringBuffer();

        // Zeile1
        sb.append(Helper.fitString2Length(beleg, 40, ' '));
        sb.append(Helper.fitString2Length(status, 40, ' '));
        sb.append("\r\n");
        // Offene Menge 17 stellig

        // Zeile2

        // Zuerst 3 Stellen Fehlernummer: 000= Abliefern moeglich 001=
        // Status erlaubt kein Abliefern - 002=
        // Menge der Ablieferung zu gross

        String fehlercode = "";
        String text1 = "";
        String text2 = "";
        if (status.equals(LocaleFac.STATUS_ERLEDIGT) || status.equals(LocaleFac.STATUS_STORNIERT)
                || status.equals(LocaleFac.STATUS_ANGELEGT) || status.equals(LocaleFac.STATUS_GESTOPPT)) {
            fehlercode = "001";
            text1 = "Nicht erlaubt!";
            text2 = "Status: " + status;
        } else {

            if (nMenge.doubleValue() <= offeneMenge.doubleValue()) {
                // Wenn Abliefermenge kleiner als Offene Menge, dann =OK
                fehlercode = "000";
                text1 = "Ablieferung";
                text2 = "erlaubt";
            } else {
                if (ueberliefernErlaubt == false) {
                    fehlercode = "002";
                    text1 = "Nicht erlaubt!";

                    DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance();
                    dfs.setDecimalSeparator('.');
                    DecimalFormat dFormat = new DecimalFormat("#####0", dfs);
                    text2 = "Nur " + Helper.fitString2LengthAlignRight(dFormat.format(offeneMenge), 6, ' ')
                            + " Stk offen";
                } else {
                    fehlercode = "000";
                    text1 = "Ablieferung";
                    text2 = "erlaubt";
                }
            }
        }
        sb.append(fehlercode);

        // 37 Leerstellen
        sb.append(Helper.fitString2Length("", 37, ' '));

        // Text1
        sb.append(Helper.fitString2Length(text1, 20, ' '));
        // Text2
        sb.append(Helper.fitString2Length(text2, 20, ' '));

        sb.append("\r\n");

        getTheClient(request, response).setSMsg(new String(sb));

    } else if (command.equals(TheApp.CMD_ZE_MECS_AUSWEISE)) {
        String fingerprint = request.getParameter("fingerprint");

        if (fingerprint != null) {
            StringBuffer sb = new StringBuffer();
            PersonalfingerDto[] personalfingerDtos = getZutrittscontrollerFac().personalfingerFindAll();
            for (int i = 0; i < personalfingerDtos.length; i++) {
                PersonalfingerDto personalfingerDto = personalfingerDtos[i];
                String id = personalfingerDto.getIId() + "";
                id = Helper.fitString2LengthAlignRight(id, 8, '0');
                StringBuffer tmp = new StringBuffer();
                // unbedingt nach ausweis sortieren
                tmp.setLength(0);
                tmp.append(Helper.fitString2Length(id, 20, ' '));

                PersonalDto personalDto = getPersonalFac()
                        .personalFindByPrimaryKey(personalfingerDto.getPersonalIId(), theclientDto);

                tmp.append(Helper.fitString2LengthAlignRight(personalDto.getCPersonalnr() + "", 5, '0')); // persnr
                tmp.append(Helper.fitString2Length("", 3, ' ')); // zutrkl

                String sVorname = personalDto.getPartnerDto().getCName2vornamefirmazeile2();
                String sNachname = personalDto.getPartnerDto().getCName1nachnamefirmazeile1();

                if (sVorname == null) {
                    sVorname = "";
                }
                tmp.append(Helper.fitString2Length(sVorname + " " + sNachname, 25, ' ')); // name
                sb.append(tmp).append("\r\n");

            }
            getTheClient(request, response).setSMsg(new String(sb));
        } else {

            // Ausweisnummern holen
            StringBuffer sb = new StringBuffer();
            PersonalDto[] personalDtos = getPersonalFac().personalFindByCAusweisSortiertNachCAusweis();

            ParametermandantDto parameterDto = getParameterFac().getMandantparameter(mandant,
                    ParameterFac.KATEGORIE_PERSONAL, ParameterFac.PARAMETER_LEAD_IN_AUSWEISNUMMER_MECS);

            String leadIn = "";
            if (parameterDto.getCWert() != null) {
                leadIn = parameterDto.getCWert().trim();
            }

            for (int i = 0; i < personalDtos.length; i++) {
                PersonalDto personalDto = personalDtos[i];
                personalDto.setPartnerDto(
                        getPartnerFac().partnerFindByPrimaryKey(personalDto.getPartnerIId(), theclientDto));
                StringBuffer tmp = new StringBuffer();
                // unbedingt nach ausweis sortieren
                tmp.setLength(0);
                tmp.append(Helper.fitString2Length(leadIn + personalDto.getCAusweis(), 20, ' ')); // ausweis
                tmp.append(Helper.fitString2LengthAlignRight(personalDto.getCPersonalnr() + "", 5, '0')); // persnr
                tmp.append(Helper.fitString2Length("", 3, ' ')); // zutrkl

                String sVorname = personalDto.getPartnerDto().getCName2vornamefirmazeile2();
                String sNachname = personalDto.getPartnerDto().getCName1nachnamefirmazeile1();

                if (sVorname == null) {
                    sVorname = "";
                }

                tmp.append(Helper.fitString2Length(sVorname + " " + sNachname, 25, ' ')); // name
                sb.append(tmp).append("\r\n");

            }

            getTheClient(request, response).setSMsg(new String(sb));
        }
    } else if (command.equals(TheApp.CMD_ZE_MECS_ERLAUBTETAETIGKEITEN)) {

        Session session = FLRSessionFactory.getFactory().openSession();
        org.hibernate.Criteria liste = session.createCriteria(FLRTaetigkeit.class);
        liste.add(Expression.eq(ZeiterfassungFac.FLR_TAETIGKEIT_B_BDEBUCHBAR, Helper.boolean2Short(true)));
        liste.addOrder(Order.asc("c_nr"));
        List<?> lReisezeiten = liste.list();
        Iterator<?> it = lReisezeiten.iterator();

        StringBuffer sb = new StringBuffer();

        while (it.hasNext()) {
            FLRTaetigkeit flrTaetigkeit = (FLRTaetigkeit) it.next();
            StringBuffer tmp = new StringBuffer();

            tmp.setLength(0);
            tmp.append('$');
            tmp.append(Helper.fitString2LengthAlignRight(flrTaetigkeit.getC_nr(), 14, ' ')); // persnr
            sb.append(tmp).append("\r\n");

        }
        session.close();

        getTheClient(request, response).setSMsg(new String(sb));

    } else if (command.equals(TheApp.CMD_ZE_MECS_PERSSTAMM)) {
        // Personalstamm holen

        StringBuffer sb = new StringBuffer();

        // unbedingt nach personalnummer sortieren
        PersonalDto[] personalDtos = getPersonalFac().personalFindByCAusweisSortiertNachPersonalnr();
        for (int i = 0; i < personalDtos.length; i++) {
            PersonalDto personalDto = personalDtos[i];
            personalDto.setPartnerDto(
                    getPartnerFac().partnerFindByPrimaryKey(personalDto.getPartnerIId(), theclientDto));
            StringBuffer tmp = new StringBuffer();

            tmp.setLength(0);
            tmp.append(Helper.fitString2LengthAlignRight(personalDto.getCPersonalnr() + "", 5, '0')); // persnr
            tmp.append(Helper.fitString2Length("", 3, ' ')); // zutrkl

            String sVorname = personalDto.getPartnerDto().getCName2vornamefirmazeile2();
            String sNachname = personalDto.getPartnerDto().getCName1nachnamefirmazeile1();

            if (sVorname == null) {
                sVorname = "";
            }

            tmp.append(Helper.fitString2Length(sVorname + " " + sNachname, 25, ' ')); // name
            sb.append(tmp).append("\r\n");

        }
        getTheClient(request, response).setSMsg(new String(sb));
    } else if (command.equals(TheApp.CMD_ZE_MECS_SALDO)) {
        String ausweis = "";
        try {
            ausweis = request.getParameter("ausweis");
        } catch (Exception e) {
            getTheClient(request, response).setBResponseIsReady(true);
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'ausweis' nicht angegeben");
            myLogger.error("doPost; Exception aufgetreten", e);
            return null;
        }
        if (ausweis.startsWith("$P")) {
            ausweis = ausweis.substring(2);
        }

        ausweis = ausweis.trim();

        ParametermandantDto parameterDto = getParameterFac().getMandantparameter(mandant,
                ParameterFac.KATEGORIE_PERSONAL, ParameterFac.PARAMETER_LEAD_IN_AUSWEISNUMMER_MECS);

        String leadIn = "";
        if (parameterDto.getCWert() != null) {
            leadIn = parameterDto.getCWert().trim();
            int iLaenge = leadIn.length();
            if (ausweis.length() > iLaenge) {
                ausweis = ausweis.substring(iLaenge);
            }
        }

        PersonalDto personalDto = getPersonalFac().personalFindByCAusweis(ausweis);

        personalDto.setPartnerDto(
                getPartnerFac().partnerFindByPrimaryKey(personalDto.getPartnerIId(), theclientDto));

        java.sql.Timestamp ts = new java.sql.Timestamp(System.currentTimeMillis() - 3600000 * 24);
        ts = com.lp.util.Helper.cutTimestamp(ts);

        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(ts.getTime());

        String urlaub = null;
        try {
            urlaub = getZeiterfassungsFac().erstelleMonatsAbrechnungFuerBDE(personalDto.getIId(),
                    new Integer(c.get(Calendar.YEAR)), new Integer(c.get(Calendar.MONTH)), false,
                    new java.sql.Date(ts.getTime()), theclientDto, true, false);

        } catch (EJBExceptionLP ex7) {

            if (ex7.getCause() instanceof EJBExceptionLP) {
                EJBExceptionLP e = (EJBExceptionLP) ex7.getCause();
                if (e != null && e.getCode() == EJBExceptionLP.FEHLER_PERSONAL_FEHLER_BEI_EINTRITTSDATUM) {
                    getTheClient(request, response)
                            .setSMsg(new String("FEHLER_PERSONAL_FEHLER_BEI_EINTRITTSDATUM"));
                    return getSJSPNext();

                }
            }
            getTheClient(request, response).setSMsg(new String(ex7.getMessage()));
            setSJSPNext("bdestation.jsp");

        }

        getTheClient(request, response).setSMsg(urlaub);

    } else if (command.equals(TheApp.CMD_ZE_MECS_ZEITBUCHEN)
            || command.equals(TheApp.CMD_ZE_MECS_ZEITBUCHENFINGERPRINT)) {
        String record = null;

        if (command.equals(TheApp.CMD_ZE_MECS_ZEITBUCHEN)) {
            record = request.getParameter("record");
        } else {
            record = request.getParameter("recordfingerprint");

        }

        record = Helper.fitString2Length(record, 200, ' ');

        String schluesselNr = record.substring(19, 39).trim();
        String zeit = record.substring(5, 19);
        String taetigkeit = record.substring(3, 5);
        // SP753
        String terminal = record.substring(64, 86);
        // Damit die Sollzeitenueberschreitungspruefeung nicht durchgefuehrt
        // wird:
        terminal = "ZT:" + terminal;
        terminal = terminal.trim();

        boolean bAbliefern = false;

        ArtikelDto artikelDtoTaetigkeit = null;

        if (record.substring(130, 155).trim().equals("$ABLIEFERN")) {
            bAbliefern = true;
        } else {

            artikelDtoTaetigkeit = getArtikelFac().artikelFindByCNrOhneExc(record.substring(132, 155).trim(),
                    theclientDto);
        }

        ZeitdatenDto zeitdatenDto = new ZeitdatenDto();
        ZeitdatenDto zeitdatenDtoEnde = new ZeitdatenDto();
        zeitdatenDto.setCWowurdegebucht(terminal);
        zeitdatenDtoEnde.setCWowurdegebucht(terminal);

        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, new Integer(zeit.substring(0, 4)).intValue());
        c.set(Calendar.MONTH, new Integer(zeit.substring(4, 6)).intValue() - 1);
        c.set(Calendar.DAY_OF_MONTH, new Integer(zeit.substring(6, 8)).intValue());
        c.set(Calendar.HOUR_OF_DAY, new Integer(zeit.substring(8, 10)).intValue());
        c.set(Calendar.MINUTE, new Integer(zeit.substring(10, 12)).intValue());
        c.set(Calendar.SECOND, new Integer(zeit.substring(12, 14)).intValue());

        zeitdatenDto.setTZeit(new java.sql.Timestamp(c.getTime().getTime()));
        zeitdatenDtoEnde.setTZeit(new java.sql.Timestamp(c.getTime().getTime() + 1000));
        zeitdatenDto.setTAendern(zeitdatenDto.getTZeit());
        zeitdatenDtoEnde.setTAendern(zeitdatenDtoEnde.getTZeit());

        // Wenn hier NullPointerException, dann kann kein Personal mit
        // Ausweisnummer gefunden werden
        Integer personalIId = null;

        if (schluesselNr.startsWith("$P")) {
            try {
                personalIId = getPersonalFac().personalFindByCAusweis(schluesselNr.substring(2)).getIId();
            } catch (NullPointerException ex11) {
                String msg = "Person mit Ausweis " + schluesselNr + " nicht vorhanden. ORIGINAL-Request:"
                        + record;
                myLogger.error(msg, ex11);
                response.setStatus(HttpServletResponse.SC_OK);
                return getSJSPNext();

            }

        } else {
            if (command.equals(TheApp.CMD_ZE_MECS_ZEITBUCHEN)) {
                try {
                    personalIId = getPersonalFac().personalFindByCAusweis(schluesselNr).getIId();
                } catch (NullPointerException ex11) {
                    String msg = "Person mit Ausweis " + schluesselNr + " nicht vorhanden. ORIGINAL-Request:"
                            + record;
                    myLogger.error(msg, ex11);

                    response.setStatus(HttpServletResponse.SC_OK);
                    return getSJSPNext();

                }

            } else if (command.equals(TheApp.CMD_ZE_MECS_ZEITBUCHENFINGERPRINT)) {
                Integer i = new Integer(schluesselNr);
                getZutrittscontrollerFac().personalfingerFindByPrimaryKey(i).getPersonalIId();
                personalIId = getZutrittscontrollerFac().personalfingerFindByPrimaryKey(i).getPersonalIId();
            }
        }

        zeitdatenDto.setPersonalIId(personalIId);
        zeitdatenDtoEnde.setPersonalIId(personalIId);
        zeitdatenDtoEnde.setTaetigkeitIId(getZeiterfassungsFac()
                .taetigkeitFindByCNr(ZeiterfassungFac.TAETIGKEIT_ENDE, theclientDto).getIId());

        // Taetigkeiten, die MECS liefert muessen in der Tabelle LP_KEYVALUE
        // uebersetzt werden (als String)
        // Bsp: MECSTERMINAL|B1|KOMMT|java.lang.String
        try {
            String sTaetigkeit = null;

            if (schluesselNr.startsWith("$P")) {
                sTaetigkeit = record.substring(110, 126);
                Integer taetigkeitIId_Ende = getZeiterfassungsFac()
                        .taetigkeitFindByCNr(ZeiterfassungFac.TAETIGKEIT_ENDE, theclientDto).getIId();
                String gutStueck = record.substring(160, 172);
                String schlechtStueck = record.substring(173, 189);

                BigDecimal nGutStueck = new BigDecimal(gutStueck.trim());
                BigDecimal nSchlechtStueck = new BigDecimal(schlechtStueck.trim());
                Integer artikelIId = null;

                if (artikelDtoTaetigkeit == null) {
                    ParametermandantDto parameterDto = getParameterFac().getMandantparameter(mandant,
                            ParameterFac.KATEGORIE_ALLGEMEIN,
                            ParameterFac.PARAMETER_DEFAULT_ARBEITSZEITARTIKEL);

                    if (parameterDto != null && parameterDto.getCWert() != null
                            && !parameterDto.getCWert().trim().equals("")) {
                        try {
                            artikelIId = getArtikelFac().artikelFindByCNr(parameterDto.getCWert(), theclientDto)
                                    .getIId();

                        } catch (RemoteException ex2) {
                            myLogger.error("Default-Arbeitszeitartikel " + parameterDto.getCWert()
                                    + " nicht vorhanden.", ex2);
                            return getSJSPNext();
                        }

                    } else {
                        myLogger.error(
                                "Default-Arbeitszeitartikel " + parameterDto.getCWert() + " nicht definiert.");
                        return getSJSPNext();
                    }
                } else {
                    artikelIId = artikelDtoTaetigkeit.getIId();
                }

                if (sTaetigkeit.startsWith("$A")) {

                    AuftragDto auftragDto = null;
                    try {
                        if (sTaetigkeit.startsWith("$A")) {
                            auftragDto = getAuftragFac().auftragFindByMandantCNrCNr(mandant,
                                    sTaetigkeit.substring(2).trim(), theclientDto);
                        } else {
                            auftragDto = getAuftragFac().auftragFindByMandantCNrCNr(mandant,
                                    sTaetigkeit.substring(3).trim(), theclientDto);

                        }
                    } catch (RemoteException ex8) {
                        zeitdatenDto.setCBemerkungZuBelegart(
                                "Auftrag " + sTaetigkeit.substring(2).trim() + " konnte nicht gefunden werden");
                        zeitdatenDto.setTaetigkeitIId(taetigkeitIId_Ende);
                        getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, false, theclientDto);

                        return getSJSPNext();
                    }
                    // Wenn Auftragsbeginn ->
                    if (sTaetigkeit.startsWith("$A")) {
                        AuftragpositionDto[] auftragpositionDtos = getAuftragpositionFac()
                                .auftragpositionFindByAuftrag(auftragDto.getIId());

                        if (auftragpositionDtos.length > 0) {
                            zeitdatenDto.setCBelegartnr(LocaleFac.BELEGART_AUFTRAG);
                            zeitdatenDto.setArtikelIId(artikelIId);
                            zeitdatenDto.setIBelegartid(auftragDto.getIId());
                            zeitdatenDto.setIBelegartpositionid(auftragpositionDtos[0].getIId());
                        } else {
                            myLogger.error("Buchung von MECS-TERMINAL, Ausweis: " + schluesselNr + ", Auftrag"
                                    + sTaetigkeit + " hat keine Positionen.");
                            return getSJSPNext();
                        }
                    }
                } else if (sTaetigkeit.startsWith("$EL") || sTaetigkeit.startsWith("$L")) {

                    com.lp.server.fertigung.service.LosDto losDto = null;
                    try {
                        if (sTaetigkeit.startsWith("$L")) {

                            losDto = getFertigungFac().losFindByCNrMandantCNr(

                                    sTaetigkeit.substring(2).trim(), mandant);

                        } else {
                            losDto = getFertigungFac().losFindByCNrMandantCNr(

                                    sTaetigkeit.substring(3).trim(), mandant);

                        }

                    } catch (EJBExceptionLP ex10) {
                        zeitdatenDto.setCBemerkungZuBelegart(
                                "Los " + sTaetigkeit.substring(2).trim() + " konnte nicht gefunden werden");
                        zeitdatenDto.setTaetigkeitIId(taetigkeitIId_Ende);
                        getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, false, theclientDto);
                        return getSJSPNext();
                    }

                    if (bAbliefern == true) {
                        LosablieferungDto losablieferungDto = new LosablieferungDto();
                        losablieferungDto.setLosIId(losDto.getIId());
                        String menge = record.substring(155, 170);
                        BigDecimal nMenge = new BigDecimal(menge.trim());
                        losablieferungDto.setNMenge(nMenge);
                        losablieferungDto.setTAendern(zeitdatenDto.getTZeit());

                        if (nMenge.doubleValue() > 0) {
                            // lt. FM
                            BigDecimal bdBisherErledigt = getFertigungFac().getErledigteMenge(losDto.getIId(),
                                    theclientDto);

                            if (bdBisherErledigt.add(nMenge).doubleValue() > losDto.getNLosgroesse()
                                    .doubleValue()) {
                                getFertigungFac().aendereLosgroesse(losDto.getIId(),
                                        bdBisherErledigt.add(nMenge).intValue(), false, theclientDto);

                                // SP933
                                losDto.setNLosgroesse(bdBisherErledigt.add(nMenge));

                            }

                            try {
                                getFertigungFac().bucheMaterialAufLos(losDto, nMenge, false, false, true,
                                        theclientDto, null, false);
                            } catch (Exception e1) {
                                // Terminal darf keinen Fehler bekommen
                            }

                            getFertigungFac().createLosablieferungFuerTerminalOhnePreisberechnung(
                                    losablieferungDto, theclientDto, false);

                            try {
                                getFertigungFac().aktualisiereNachtraeglichPreiseAllerLosablieferungen(
                                        losDto.getIId(), theclientDto, true);
                            } catch (Exception e) {
                                // PREISBERECHNUNG FEHLGESCHLAGEN
                                myLogger.error("Preisberechnung der Ablieferungen f\u00FCr Los "
                                        + losDto.getCNr() + " fehlgeschlagen. Bitte manuell ausfuehren", e);
                            }
                        }

                        // PJ17748

                        ParametermandantDto parameterAblieferungBuchtEndeDto = getParameterFac()
                                .getMandantparameter(mandant, ParameterFac.KATEGORIE_FERTIGUNG,
                                        ParameterFac.PARAMETER_ABLIEFERUNG_BUCHT_ENDE);

                        Boolean bAblieferungBuchtEndeDto = (Boolean) parameterAblieferungBuchtEndeDto
                                .getCWertAsObject();

                        if (bAblieferungBuchtEndeDto == true) {
                            zeitdatenDto.setTaetigkeitIId(taetigkeitIId_Ende);
                            zeitdatenDto.setCBelegartnr(null);
                            zeitdatenDto.setArtikelIId(null);
                            zeitdatenDto.setIBelegartid(null);
                            zeitdatenDto.setIBelegartpositionid(null);
                            Integer zeitdatenIId = getZeiterfassungsFac().createZeitdaten(zeitdatenDto, false,
                                    false, false, theclientDto);

                            // PJ17797
                            if (nMenge.doubleValue() > 0) {
                                if (getMandantFac().darfAnwenderAufZusatzfunktionZugreifen(
                                        MandantFac.ZUSATZFUNKTION_STUECKRUECKMELDUNG, theclientDto)) {

                                    Integer lossollarbeitsplanIId = null;
                                    LossollarbeitsplanDto[] sollDtos = getFertigungFac()
                                            .lossollarbeitsplanFindByLosIId(losDto.getIId());
                                    if (sollDtos.length > 0) {
                                        lossollarbeitsplanIId = sollDtos[sollDtos.length - 1].getIId();
                                    } else {
                                        lossollarbeitsplanIId = getFertigungFac()
                                                .defaultArbeitszeitartikelErstellen(losDto, theclientDto);
                                    }

                                    LosgutschlechtDto losgutschlechtDto = new LosgutschlechtDto();
                                    losgutschlechtDto.setZeitdatenIId(zeitdatenIId);
                                    losgutschlechtDto.setLossollarbeitsplanIId(lossollarbeitsplanIId);
                                    losgutschlechtDto.setNGut(nMenge);
                                    losgutschlechtDto.setNSchlecht(new BigDecimal(0));
                                    losgutschlechtDto.setNInarbeit(new BigDecimal(0));

                                    getFertigungFac().createLosgutschlecht(losgutschlechtDto, theclientDto);
                                }
                            }

                        }

                        return getSJSPNext();
                    }

                    // Wenn Auftragsbeginn ->
                    if (sTaetigkeit.startsWith("$L")) {
                        zeitdatenDto.setCBelegartnr(LocaleFac.BELEGART_LOS);
                        zeitdatenDto.setArtikelIId(artikelIId);
                        zeitdatenDto.setIBelegartid(losDto.getIId());

                        LossollarbeitsplanDto[] sollDtos = getFertigungFac()
                                .lossollarbeitsplanFindByLosIIdArtikelIIdTaetigkeit(losDto.getIId(),
                                        artikelIId);
                        if (sollDtos.length > 0) {
                            zeitdatenDto.setIBelegartpositionid(sollDtos[0].getIId());
                        }
                    } else {
                        // Hole letzten begonnenen Auftrag und hinterlege
                        // gut/schlechtstueck
                        Session session = FLRSessionFactory.getFactory().openSession();
                        org.hibernate.Criteria liste = session.createCriteria(FLRZeitdaten.class);
                        liste.add(Expression.eq(ZeiterfassungFac.FLR_ZEITDATEN_PERSONAL_I_ID, personalIId));
                        /*
                         * liste.add(Expression.eq(ZeiterfassungFac.
                         * FLR_ZEITDATEN_C_BELEGARTNR ,
                         * LocaleFac.BELEGART_LOS)); liste.add(Expression.eq
                         * (ZeiterfassungFac.FLR_ZEITDATEN_I_BELEGARTID,
                         * losDto.getIId()));
                         */
                        liste.add(Expression.gt(ZeiterfassungFac.FLR_ZEITDATEN_T_ZEIT,
                                Helper.cutTimestamp(zeitdatenDto.getTZeit())));

                        liste.addOrder(Order.desc(ZeiterfassungFac.FLR_ZEITDATEN_T_ZEIT));
                        // liste.setMaxResults(1);
                        List<?> lReisezeiten = liste.list();

                        Iterator<?> it = lReisezeiten.iterator();
                        zeitdatenDto.setCBelegartnr(LocaleFac.BELEGART_LOS);
                        zeitdatenDto.setArtikelIId(artikelIId);
                        zeitdatenDto.setIBelegartid(losDto.getIId());

                        ZeitdatenDto letzterAuftrag = null;
                        while (it.hasNext()) {
                            FLRZeitdaten flrLetzerAuftrag = (FLRZeitdaten) it.next();

                            if (flrLetzerAuftrag.getC_belegartnr() != null
                                    && flrLetzerAuftrag.getI_belegartid() != null) {
                                if (flrLetzerAuftrag.getC_belegartnr().equals(zeitdatenDto.getCBelegartnr())
                                        && flrLetzerAuftrag.getI_belegartid()
                                                .equals(zeitdatenDto.getIBelegartid())) {
                                    letzterAuftrag = getZeiterfassungsFac().zeitdatenFindByPrimaryKey(
                                            flrLetzerAuftrag.getI_id(), theclientDto);
                                    break;
                                }
                            } else if (flrLetzerAuftrag.getTaetigkeit_i_id() != null
                                    && flrLetzerAuftrag.getTaetigkeit_i_id().equals(taetigkeitIId_Ende)) {
                                break;
                            }

                        }

                        if (letzterAuftrag != null) {
                            // Hier eintragen
                            ZeitdatenDto auftragsbeginn = getZeiterfassungsFac()
                                    .zeitdatenFindByPrimaryKey(letzterAuftrag.getIId(), theclientDto);

                            // auftragsbeginn.setNGut(nGutStueck);
                            // auftragsbeginn.setNSchlecht(nSchlechtStueck);
                            getZeiterfassungsFac().updateZeitdaten(auftragsbeginn, theclientDto);
                            // und buche ENDE
                            zeitdatenDto = zeitdatenDtoEnde;

                        } else {
                            zeitdatenDto.setCBelegartnr(LocaleFac.BELEGART_LOS);
                            zeitdatenDto.setArtikelIId(artikelIId);
                            zeitdatenDto.setIBelegartid(losDto.getIId());
                            // zeitdatenDto.setNGut(nGutStueck);
                            // zeitdatenDto.setNSchlecht(nSchlechtStueck);

                            getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, false,
                                    theclientDto);
                            zeitdatenDto = zeitdatenDtoEnde;
                        }
                        session.close();

                        /*
                         * if (lReisezeiten.size() > 0) { FLRZeitdaten
                         * flrZeitdaten = (FLRZeitdaten)
                         * lReisezeiten.iterator().next();
                         * 
                         * ZeitdatenDto losbeginn = getZeiterfassungsFac().
                         * zeitdatenFindByPrimaryKey(flrZeitdaten.getI_id(),
                         * cNrUser);
                         * 
                         * losbeginn.setNGut(nGutStueck);
                         * losbeginn.setNSchlecht(nSchlechtStueck);
                         * getZeiterfassungsFac().updateZeitdaten(losbeginn,
                         * cNrUser); //und buche ENDE zeitdatenDto =
                         * zeitdatenDtoEnde; } else {
                         * zeitdatenDto.setCBelegartnr
                         * (LocaleFac.BELEGART_LOS);
                         * zeitdatenDto.setArtikelIId(artikelIId);
                         * zeitdatenDto.setIBelegartid(losDto.getIId());
                         * zeitdatenDto.setNGut(nGutStueck);
                         * zeitdatenDto.setNSchlecht(nSchlechtStueck);
                         * getZeiterfassungsFac
                         * ().createZeitdaten(zeitdatenDto, true, true,
                         * cNrUser); zeitdatenDto = zeitdatenDtoEnde; }
                         * 
                         * session.close();
                         */

                    }

                } else {
                    zeitdatenDto.setTaetigkeitIId(getZeiterfassungsFac().taetigkeitFindByCNr(
                            Helper.fitString2Length(sTaetigkeit.substring(1), 15, ' '), theclientDto).getIId());
                }

            }

            else {
                sTaetigkeit = getSystemServicesFac()
                        .keyvalueFindByPrimaryKey(SystemServicesFac.KEYVALUE_MECSTERMINAL, taetigkeit)
                        .getCValue();
                if (sTaetigkeit != null && !sTaetigkeit.equals(ZeiterfassungFac.TAETIGKEIT_REISE.trim())) {
                    zeitdatenDto.setTaetigkeitIId(getZeiterfassungsFac()
                            .taetigkeitFindByCNr(Helper.fitString2Length(sTaetigkeit, 15, ' '), theclientDto)
                            .getIId());
                }
            }

            // Resezeiten wenn Taetigkeit REISE
            if (sTaetigkeit != null && sTaetigkeit.equals(ZeiterfassungFac.TAETIGKEIT_REISE.trim())) {
                ReiseDto reiseDto = new ReiseDto();
                reiseDto.setPersonalIId(personalIId);

                // Letzte Reise von HEUTE holen
                // Heute 00:00 Uhr
                Calendar cTemp = Calendar.getInstance();
                cTemp.setTimeInMillis(zeitdatenDto.getTZeit().getTime());
                cTemp.set(Calendar.HOUR_OF_DAY, 0);
                cTemp.set(Calendar.MINUTE, 0);
                cTemp.set(Calendar.SECOND, 0);
                cTemp.set(Calendar.MILLISECOND, 0);

                Session sessReise = FLRSessionFactory.getFactory().openSession();
                org.hibernate.Criteria reisezeiten = sessReise.createCriteria(FLRReise.class);
                reisezeiten.add(Expression.eq(ZeiterfassungFac.FLR_REISE_PERSONAL_I_ID, personalIId));
                reisezeiten.add(Expression.ge(ZeiterfassungFac.FLR_REISE_T_ZEIT,
                        new Timestamp(cTemp.getTimeInMillis())));
                reisezeiten.add(Expression.lt(ZeiterfassungFac.FLR_REISE_T_ZEIT, zeitdatenDto.getTZeit()));
                reisezeiten.addOrder(Order.desc(ZeiterfassungFac.FLR_REISE_T_ZEIT));
                reisezeiten.setMaxResults(1);
                List<?> lReisezeiten = reisezeiten.list();

                if (lReisezeiten.size() == 0) {
                    reiseDto.setBBeginn(Helper.boolean2Short(true));
                } else {
                    FLRReise flrReise = (FLRReise) lReisezeiten.get(0);
                    if (Helper.short2boolean(flrReise.getB_beginn()) == true) {
                        reiseDto.setBBeginn(Helper.boolean2Short(false));
                    } else {
                        reiseDto.setBBeginn(Helper.boolean2Short(true));

                    }
                }

                reiseDto.setTZeit(zeitdatenDto.getTZeit());

                Integer partnerMandant = getMandantFac().mandantFindByPrimaryKey(mandant, theclientDto)
                        .getPartnerIId();
                PartnerDto partnerDto = getPartnerFac().partnerFindByPrimaryKey(partnerMandant, theclientDto);

                if (partnerDto.getLandplzortIId() == null) {
                    throw new Exception("Mandant hat kein Land hinterlegt");
                }

                DiaetenDto[] dtos = getZeiterfassungsFac()
                        .diaetenFindByLandIId(partnerDto.getLandplzortDto().getIlandID());

                if (dtos.length == 0) {
                    // Einen anlegen
                    DiaetenDto dto = new DiaetenDto();
                    dto.setCBez(partnerDto.getLandplzortDto().getLandDto().getCName());
                    dto.setLandIId(partnerDto.getLandplzortDto().getIlandID());
                    reiseDto.setDiaetenIId(getZeiterfassungsFac().createDiaeten(dto));
                } else {
                    reiseDto.setDiaetenIId(dtos[0].getIId());
                }

                getZeiterfassungsFac().createReise(reiseDto, theclientDto);
                response.setStatus(HttpServletResponse.SC_OK);
                response.flushBuffer();
                getTheClient(request, response).setBResponseIsReady(true);

                return getSJSPNext();
            }
        } catch (Exception ex3) {
            ex3.printStackTrace();
            // lt. FM darf an das MECS-Terminal nur Status=200
            // zurueckgegeben werden
            response.setStatus(HttpServletResponse.SC_OK);
            return getSJSPNext();
        }

        getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, false, theclientDto);

        response.setStatus(HttpServletResponse.SC_OK);
        response.flushBuffer();
        getTheClient(request, response).setBResponseIsReady(true);
    } else if (command.equals(TheApp.CMD_ZE_QUICKZE)) {

    } else if (command.equals(TheApp.CMD_ZE_RECHNERSTART1)) {
        int i = 0;
    } else if (command.equals(TheApp.CMD_ZE_QUICKZEITERFASSUNG)) {
        if (getTheClient(request, response).getSMsg() == null) {
            getTheClient(request, response).setSMsg("");
        }

        String username = getCookieValue("anmeldename", request);
        String password = getCookieValue("pass", request);

        if (localeCookie != null && localeCookie.length() > 3) {
            localeLogon = new Locale(localeCookie.substring(0, 2), localeCookie.substring(2, 4));
        }

        if (username == null || password == null) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                    "Es wurde kein Benutzername oder Kennwort angegeben. Bitte verwenden Sie http://?????cmd=quickze");

        }
        try {
            theclientDto = getLogonFac().logon(Helper.getFullUsername(username),
                    Helper.getMD5Hash((username + password).toCharArray()), localeLogon, null, null,
                    new Timestamp(System.currentTimeMillis()));
        } catch (EJBExceptionLP ex12) {

            int code = ex12.getCode();
            if (code == EJBExceptionLP.FEHLER_BEI_FINDBYPRIMARYKEY) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        "Benutzername '" + username + "' konnte im System nicht gefunden werden");
            } else if (code == EJBExceptionLP.FEHLER_FALSCHES_KENNWORT) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        "Kennwort f\u00FCr Benutzername '" + username + "' ist falsch.");
            } else if (code == EJBExceptionLP.FEHLER_BENUTZER_IST_GESPERRT) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        "Benutzername '" + username + "' ist gesperrt.");
            } else if (code == EJBExceptionLP.FEHLER_BENUTZER_IST_NICHT_MEHR_GUELTIG) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        "Benutzername '" + username + "' ist nicht mehr g\u00FCltig.");
            } else if (code == EJBExceptionLP.FEHLER_BENUTZER_DARF_SICH_BEI_DIESEM_MANDANTEN_NICHT_ANMELDEN) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        "Benutzername '" + username + "' darf sich bei dem Mandanten nicht anmelden.");
            } else if (code == EJBExceptionLP.FEHLER_BENUTZER_KEIN_EINTRAG_IN_BENUTZERMANDANT) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        "Kein Eintrag in Benutzermandant f\u00FCr Benutzername '" + username + "'.");
            } else if (ex12.getCode() == EJBExceptionLP.FEHLER_MAXIMALE_BENUTZERANZAHL_UEBERSCHRITTEN) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        "Maximale Benutzeranzahl \u00FCberschritten '" + username + "'.");
                return null;
            } else if (code == EJBExceptionLP.FEHLER_BENUTZER_DARF_SICH_IN_DIESER_SPRACHE_NICHT_ANMELDEN) {
                ArrayList<?> al = ((EJBExceptionLP) ex12.getCause()).getAlInfoForTheClient();
                String zusatz = "";
                if (al.size() > 0 && al.get(0) instanceof Locale) {
                    Locale loc = (Locale) al.get(0);
                    zusatz = "(" + loc.getDisplayLanguage() + "|" + loc.getDisplayCountry() + ")";
                }
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        "Benutzer '" + username + "' darf sich in '" + zusatz + "' nicht anmelden.");
            }

            return null;
        }

        PersonalDto personalDto = getPersonalFac().personalFindByPrimaryKey(theclientDto.getIDPersonal(),
                theclientDto);

        personalDto.setPartnerDto(
                getPartnerFac().partnerFindByPrimaryKey(personalDto.getPartnerIId(), theclientDto));

        HashMap<String, Object> hmData = new HashMap<String, Object>();

        TextDto textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.sondertaetigkeit",
                theclientDto.getMandant(), theclientDto.getLocUiAsString());
        if (textDto != null) {
            hmData.put("bezeichnung_sondertaetigkeit", textDto.getCText());
        } else {
            hmData.put("bezeichnung_sondertaetigkeit", "Sondert\u00E4tigkeit");
        }

        // Belegarten holen

        Map<String, String> b = getZeiterfassungsFac().getBebuchbareBelegarten(theclientDto);

        hmData.put("belegarten", b);

        String firstBelegart = (String) b.keySet().iterator().next();
        String belegart = null;
        if (request.getParameter("belegart") == null) {
            belegart = firstBelegart;
        } else {
            belegart = request.getParameter("belegart");
        }

        if (belegart.equals(LocaleFac.BELEGART_AUFTRAG)) {
            textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.offenerauftrag",
                    theclientDto.getMandant(), theclientDto.getLocUiAsString());
        } else if (belegart.equals(LocaleFac.BELEGART_LOS)) {
            textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.offeneslos",
                    theclientDto.getMandant(), theclientDto.getLocUiAsString());
        } else if (belegart.equals(LocaleFac.BELEGART_ANGEBOT)) {
            textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.offenesangebot",
                    theclientDto.getMandant(), theclientDto.getLocUiAsString());
        } else if (belegart.equals(LocaleFac.BELEGART_PROJEKT)) {
            textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.offenesprojekt",
                    theclientDto.getMandant(), theclientDto.getLocUiAsString());
        }
        if (textDto != null) {
            hmData.put("bezeichnung_offenerauftrag", textDto.getCText());
        } else {
            hmData.put("bezeichnung_offenerauftrag", "Offener Beleg");
        }

        textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.taetigkeit",
                theclientDto.getMandant(), theclientDto.getLocUiAsString());
        if (textDto != null) {
            hmData.put("bezeichnung_taetigkeit", textDto.getCText());
        } else {
            hmData.put("bezeichnung_taetigkeit", "T\u00E4tigkeit");
        }

        textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.kunde",
                theclientDto.getMandant(), theclientDto.getLocUiAsString());
        if (textDto != null) {
            hmData.put("bezeichnung_kunde", textDto.getCText());
        } else {
            hmData.put("bezeichnung_kunde", "Kunde");
        }

        textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.belegart",
                theclientDto.getMandant(), theclientDto.getLocUiAsString());
        if (textDto != null) {
            hmData.put("bezeichnung_belegart", textDto.getCText());
        } else {
            hmData.put("bezeichnung_belegart", "Belegart");
        }

        textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.bemerkung",
                theclientDto.getMandant(), theclientDto.getLocUiAsString());
        if (textDto != null) {
            hmData.put("bezeichnung_bemerkung", textDto.getCText());
        } else {
            hmData.put("bezeichnung_bemerkung", "Bemerkung");
        }

        hmData.put("locale", Helper.locale2String(localeLogon).trim());
        hmData.put("mandant", mandant);
        hmData.put("person", personalDto.getPartnerDto().formatAnrede());

        // Kunden mit offenen Auftraegen holen
        Session session = FLRSessionFactory.getFactory().openSession();
        String sQuery = "";
        if (belegart.equals(LocaleFac.BELEGART_AUFTRAG)) {
            sQuery = "SELECT flrkunde.flrpartner.c_name1nachnamefirmazeile1, flrkunde.flrpartner.c_name2vornamefirmazeile2, flrkunde.flrpartner.i_id "
                    + " FROM FLRAuftrag AS auftrag WHERE (auftrag.auftragstatus_c_nr='" + LocaleFac.STATUS_OFFEN
                    + "' OR auftrag.auftragstatus_c_nr='" + LocaleFac.STATUS_TEILERLEDIGT
                    + "') AND auftrag.mandant_c_nr='" + theclientDto.getMandant() + "'"
                    + "  GROUP BY flrkunde.flrpartner.c_name1nachnamefirmazeile1, flrkunde.flrpartner.c_name2vornamefirmazeile2, flrkunde.flrpartner.i_id  ORDER BY flrkunde.flrpartner.c_name1nachnamefirmazeile1 ASC";
        } else if (belegart.equals(LocaleFac.BELEGART_ANGEBOT)) {
            sQuery = "SELECT flrkunde.flrpartner.c_name1nachnamefirmazeile1, flrkunde.flrpartner.c_name2vornamefirmazeile2, flrkunde.flrpartner.i_id "
                    + " FROM FLRAngebot AS angebot WHERE (angebot.angebotstatus_c_nr='" + LocaleFac.STATUS_OFFEN
                    + "') AND angebot.mandant_c_nr='" + theclientDto.getMandant() + "'"
                    + "  GROUP BY flrkunde.flrpartner.c_name1nachnamefirmazeile1, flrkunde.flrpartner.c_name2vornamefirmazeile2, flrkunde.flrpartner.i_id  ORDER BY flrkunde.flrpartner.c_name1nachnamefirmazeile1 ASC";
        } else if (belegart.equals(LocaleFac.BELEGART_PROJEKT)) {
            sQuery = "SELECT flrpartner.c_name1nachnamefirmazeile1, flrpartner.c_name2vornamefirmazeile2, flrpartner.i_id "
                    + " FROM FLRProjekt AS projekt WHERE projekt.status_c_nr<>'"
                    + ProjektServiceFac.PROJEKT_STATUS_STORNIERT
                    + "' AND projekt.t_erledigungsdatum IS NULL AND projekt.mandant_c_nr='"
                    + theclientDto.getMandant() + "'"
                    + "  GROUP BY flrpartner.c_name1nachnamefirmazeile1, flrpartner.c_name2vornamefirmazeile2, flrpartner.i_id  ORDER BY flrpartner.c_name1nachnamefirmazeile1 ASC";

        }
        LinkedHashMap<Object, Object> tmKunden = new LinkedHashMap<Object, Object>();
        Integer firstKunde = null;
        if (!belegart.equals(LocaleFac.BELEGART_LOS)) {
            Query kunden = session.createQuery(sQuery);

            List<?> resultList = kunden.list();

            Iterator<?> resultListIterator = resultList.iterator();

            int row = 0;

            while (resultListIterator.hasNext()) {
                Object o[] = (Object[]) resultListIterator.next();
                if (row == 0) {
                    firstKunde = (Integer) o[2];
                }
                if (o[1] == null) {
                    tmKunden.put(o[2], o[0]);

                } else {
                    tmKunden.put(o[2], o[0] + " " + o[1]);

                }
                row++;
            }
            session.close();
        } else {

            sQuery = "SELECT los " + " FROM FLRLosReport AS los WHERE (los.status_c_nr='"
                    + LocaleFac.STATUS_AUSGEGEBEN + "' OR los.status_c_nr='" + LocaleFac.STATUS_IN_PRODUKTION
                    + "' OR los.status_c_nr='" + LocaleFac.STATUS_TEILERLEDIGT + "')  AND los.mandant_c_nr='"
                    + theclientDto.getMandant()
                    + "' AND ( los.flrauftrag IS NOT NULL OR los.flrkunde IS NOT NULL) ";

            Query kunden = session.createQuery(sQuery);

            List<?> resultList = kunden.list();

            Iterator<?> resultListIterator = resultList.iterator();

            int row = 0;

            while (resultListIterator.hasNext()) {
                FLRLosReport los = (FLRLosReport) resultListIterator.next();

                Integer partnerIId = null;
                String kundenname = "";
                if (los.getFlrauftrag() != null) {
                    partnerIId = los.getFlrauftrag().getFlrkunde().getFlrpartner().getI_id();

                    kundenname += los.getFlrauftrag().getFlrkunde().getFlrpartner()
                            .getC_name1nachnamefirmazeile1();

                    if (los.getFlrauftrag().getFlrkunde().getFlrpartner()
                            .getC_name2vornamefirmazeile2() != null) {
                        kundenname += " " + los.getFlrauftrag().getFlrkunde().getFlrpartner()
                                .getC_name2vornamefirmazeile2();
                    }

                } else {
                    partnerIId = los.getFlrkunde().getFlrpartner().getI_id();
                    kundenname += los.getFlrkunde().getFlrpartner().getC_name1nachnamefirmazeile1();

                    if (los.getFlrkunde().getFlrpartner().getC_name2vornamefirmazeile2() != null) {
                        kundenname += " " + los.getFlrkunde().getFlrpartner().getC_name2vornamefirmazeile2();
                    }
                }

                if (row == 0) {
                    firstKunde = partnerIId;
                }
                if (!tmKunden.containsKey(partnerIId)) {

                    tmKunden.put(partnerIId, kundenname);
                }

                row++;
            }

            tmKunden = (LinkedHashMap) Helper.sortByValue(tmKunden);

            // leeren Kunden einfuegen
            tmKunden.put("", "--KEIN--");

            session.close();
        }
        hmData.put("kunden", tmKunden);

        // Sondertaetigkeiten holen

        Map<Integer, String> m = getZeiterfassungsFac()
                .getAllSprSondertaetigkeitenNurBDEBuchbar(theclientDto.getLocUiAsString());

        hmData.put("taetigkeiten", m);

        // Kunden holen

        Integer kunde = null;
        if (request.getParameter("kunde") == null) {
            kunde = firstKunde;
        } else {
            if (!request.getParameter("kunde").equals("")) {
                if (!request.getParameter("kunde").equals(" ")) {
                    if (!request.getParameter("kunde").trim().equals("null")) {
                        kunde = new Integer(request.getParameter("kunde").trim());
                    }
                }

            }
        }

        hmData.put("selectedbelegart", belegart);

        session = FLRSessionFactory.getFactory().openSession();

        if (belegart.equals(LocaleFac.BELEGART_AUFTRAG)) {

            sQuery = "SELECT auftrag.i_id, auftrag.c_nr, auftrag.c_bez, auftrag.t_liefertermin, auftrag.flrkunde.flrpartner.i_id "
                    + " FROM FLRAuftrag AS auftrag WHERE (auftrag.auftragstatus_c_nr='" + LocaleFac.STATUS_OFFEN
                    + "' OR auftrag.auftragstatus_c_nr='" + LocaleFac.STATUS_TEILERLEDIGT
                    + "') AND auftrag.flrkunde.flrpartner.i_id=" + kunde
                    + " AND auftrag.b_versteckt=0 ORDER BY auftrag.c_nr ASC";
        } else if (belegart.equals(LocaleFac.BELEGART_LOS)) {
            sQuery = "SELECT los.i_id, los.c_nr, los.c_projekt, los.t_produktionsende, coalesce(auftragpartner.i_id,kundepartner.i_id) "
                    + " FROM FLRLosReport AS los LEFT OUTER JOIN los.flrauftrag.flrkunde.flrpartner as auftragpartner LEFT OUTER JOIN los.flrkunde.flrpartner as kundepartner  WHERE (los.status_c_nr='"
                    + LocaleFac.STATUS_AUSGEGEBEN + "' OR los.status_c_nr='" + LocaleFac.STATUS_IN_PRODUKTION
                    + "' OR los.status_c_nr='" + LocaleFac.STATUS_TEILERLEDIGT + "') ";
            if (kunde != null) {
                sQuery += " AND ( auftragpartner.i_id=" + kunde + " OR kundepartner.i_id=" + kunde + ")";

            } else {
                sQuery += " AND ( auftragpartner.i_id IS NULL AND kundepartner.i_id IS NULL)";
            }
            sQuery += " ORDER BY los.c_nr ASC";
        } else if (belegart.equals(LocaleFac.BELEGART_ANGEBOT)) {
            sQuery = "SELECT angebot.i_id, angebot.c_nr, angebot.c_bez, angebot.t_realisierungstermin, angebot.flrkunde.flrpartner.i_id "
                    + " FROM FLRAngebot AS angebot WHERE angebot.angebotstatus_c_nr='" + LocaleFac.STATUS_OFFEN
                    + "' AND angebot.flrkunde.flrpartner.i_id=" + kunde + "  ORDER BY angebot.c_nr ASC";
        } else if (belegart.equals(LocaleFac.BELEGART_PROJEKT)) {
            sQuery = "SELECT projekt.i_id, projekt.c_nr, projekt.c_titel, projekt.t_zielwunschdatum, projekt.partner_i_id "
                    + " FROM FLRProjekt AS projekt WHERE projekt.status_c_nr<>'"
                    + ProjektServiceFac.PROJEKT_STATUS_STORNIERT
                    + "' AND projekt.t_erledigungsdatum IS NULL AND projekt.partner_i_id=" + kunde
                    + " ORDER BY projekt.c_nr ASC";
        }
        Query auftraege = session.createQuery(sQuery);

        List<?> resultList = auftraege.list();

        Iterator resultListIterator = resultList.iterator();
        LinkedHashMap<Object, Object> tmAuftraege = new LinkedHashMap<Object, Object>();
        Object partnerIId = null;
        String selectedAuftragId = null;
        while (resultListIterator.hasNext()) {
            Object o[] = (Object[]) resultListIterator.next();
            partnerIId = (Integer) o[4];

            if (o[2] == null) {
                tmAuftraege.put(o[0], o[1]);

            } else {
                tmAuftraege.put(o[0], o[1] + " " + o[2]);

            }
            if (selectedAuftragId == null) {
                selectedAuftragId = o[0].toString();
            }
        }

        session.close();
        hmData.put("auftraege", tmAuftraege);

        hmData.put("selectedkunde", partnerIId);

        if (request.getParameter("auftrag") != null && request.getParameter("auftrag").length() > 0) {
            selectedAuftragId = request.getParameter("auftrag");

        }

        // Artikel zu Auftrag holen
        session = FLRSessionFactory.getFactory().openSession();

        if (belegart.equals(LocaleFac.BELEGART_AUFTRAG)) {

            sQuery = "SELECT a.i_id, a.flrartikel.i_id FROM FLRAuftragposition AS a WHERE a.flrauftrag.i_id="
                    + selectedAuftragId + " AND a.flrartikel.artikelart_c_nr='"
                    + ArtikelFac.ARTIKELART_ARBEITSZEIT + "'";
        } else if (belegart.equals(LocaleFac.BELEGART_LOS)) {

            sQuery = "SELECT a.i_id, a.flrartikel.i_id FROM FLRLossollarbeitsplan AS a WHERE a.los_i_id="
                    + selectedAuftragId + " AND a.flrartikel.artikelart_c_nr='"
                    + ArtikelFac.ARTIKELART_ARBEITSZEIT + "'";
        } else if (belegart.equals(LocaleFac.BELEGART_ANGEBOT)) {

            sQuery = "SELECT a.i_id, a.flrartikel.i_id FROM FLRAngebotposition AS a WHERE a.flrangebot.i_id="
                    + selectedAuftragId + " AND a.flrartikel.artikelart_c_nr='"
                    + ArtikelFac.ARTIKELART_ARBEITSZEIT + "'";
        }
        LinkedHashMap<Object, Object> tmArtikel = new LinkedHashMap<Object, Object>();
        if (!belegart.equals(LocaleFac.BELEGART_PROJEKT)) {
            Query artikelListe = session.createQuery(sQuery);

            resultList = artikelListe.list();

            resultListIterator = resultList.iterator();

            if (resultList.size() > 0) {
                tmArtikel.put(-1, " - - - - - - Beleg - - - - - -");
            }

            while (resultListIterator.hasNext()) {
                Object[] zeile = (Object[]) resultListIterator.next();

                Integer artikelIId = (Integer) zeile[1];

                String sollIst = "";

                if (belegart.equals(LocaleFac.BELEGART_AUFTRAG)) {

                    BigDecimal bdSoll = getAuftragpositionFac()
                            .auftragpositionFindByPrimaryKey((Integer) zeile[0]).getNMenge();
                    sollIst = "; Soll: " + Helper.formatZahl(bdSoll, 2, theclientDto.getLocUi());

                    Double dIst;
                    try {

                        boolean bZuvieleZeitbuchungen = getZeiterfassungsFac()
                                .sindZuvieleZeitdatenEinesBelegesVorhanden(belegart,
                                        new Integer(selectedAuftragId), theclientDto);
                        if (bZuvieleZeitbuchungen == false) {

                            dIst = getZeiterfassungsFac().getSummeZeitenEinesBeleges(belegart,
                                    new Integer(selectedAuftragId), (Integer) zeile[0], null, null, null,
                                    theclientDto);
                            sollIst += " Ist: " + Helper.formatZahl(dIst, 2, theclientDto.getLocUi());
                        }
                    } catch (Exception e) {
                        sollIst += " Ist: ERR";
                    }

                }

                String artikel = getArtikelFac().artikelFindByPrimaryKey(artikelIId, theclientDto)
                        .formatArtikelbezeichnung() + sollIst;
                if (!tmArtikel.containsKey(artikelIId)) {
                    tmArtikel.put(artikelIId, artikel);

                }
            }
            session.close();
        }
        // Artikel des Auftrags + Artikel aus Personalverfuegbarkeit holen

        if (selectedAuftragId != null) {

            PersonalverfuegbarkeitDto[] personalverfuegbarkeitDtos = getPersonalFac()
                    .personalverfuegbarkeitFindByPersonalIId(personalDto.getIId());
            if (personalverfuegbarkeitDtos.length > 0) {
                tmArtikel.put(-2, " - - - Verf\u00FCgbarkeit - - - ");
            }
            for (int i = 0; i < personalverfuegbarkeitDtos.length; i++) {
                PersonalverfuegbarkeitDto v = personalverfuegbarkeitDtos[i];
                String artikel = getArtikelFac().artikelFindByPrimaryKey(v.getArtikelIId(), theclientDto)
                        .formatArtikelbezeichnung();
                tmArtikel.put(v.getArtikelIId(), artikel);
            }

            hmData.put("selectedauftrag", selectedAuftragId);

        }

        ParametermandantDto parameterDtoDefaultarbeitszeit = getParameterFac().getMandantparameter(mandant,
                ParameterFac.KATEGORIE_ALLGEMEIN, ParameterFac.PARAMETER_DEFAULT_ARBEITSZEITARTIKEL);

        if (parameterDtoDefaultarbeitszeit != null && parameterDtoDefaultarbeitszeit.getCWert() != null
                && !parameterDtoDefaultarbeitszeit.getCWert().trim().equals("")) {

            ArtikelDto artikelDtoDefaultArbeiztszeit = getArtikelFac()
                    .artikelFindByCNr(parameterDtoDefaultarbeitszeit.getCWert(), theclientDto);
            tmArtikel.put(-3, " - - - Default-Arbeitszeitartikel - - -");
            tmArtikel.put(artikelDtoDefaultArbeiztszeit.getIId(),
                    artikelDtoDefaultArbeiztszeit.formatArtikelbezeichnung());
        }

        hmData.put("artikel", tmArtikel);

        // Zeitbuchen
        String bucheauftrag = request.getParameter("bucheauftrag");
        String buchesondertaetigkeit = request.getParameter("buchesondertaetigkeit");

        // Zeit buchen
        ZeitdatenDto zeitdatenDto = new ZeitdatenDto();
        zeitdatenDto.setPersonalIId(personalDto.getIId());
        Timestamp tZeit = new Timestamp(System.currentTimeMillis());
        zeitdatenDto.setCWowurdegebucht("Quick-ZE " + request.getRemoteHost());
        String meldung = "";

        zeitdatenDto.setTZeit(tZeit);
        String bemerkung = request.getParameter("bemerkung");
        zeitdatenDto.setCBemerkungZuBelegart(bemerkung);

        if (bucheauftrag != null && bucheauftrag.length() > 0) {

            if (request.getParameter("artikel") != null) {

                Integer artikelId = new Integer(request.getParameter("artikel"));
                if (artikelId > 0) {

                    Integer auftragIId = new Integer(selectedAuftragId.trim());

                    String s = "Auf ";

                    if (kunde != null) {
                        PartnerDto partnerDto = getPartnerFac().partnerFindByPrimaryKey(kunde, theclientDto);
                        s += partnerDto.formatFixName1Name2() + ", ";
                    }

                    if (belegart.equals(LocaleFac.BELEGART_AUFTRAG)) {
                        textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.auftrag",
                                theclientDto.getMandant(), theclientDto.getLocUiAsString());

                        if (textDto != null) {
                            s += textDto.getCText() + " ";
                        } else {
                            s += "Auftrag ";
                        }

                        com.lp.server.auftrag.service.AuftragDto auftragDto = getAuftragFac()
                                .auftragFindByPrimaryKey(auftragIId);
                        s += auftragDto.getCNr();
                        if (auftragDto.getCBezProjektbezeichnung() != null) {
                            s += " " + auftragDto.getCBezProjektbezeichnung();
                        }

                        com.lp.server.auftrag.service.AuftragpositionDto[] auftragpositionDtos = getAuftragpositionFac()
                                .auftragpositionFindByAuftrag(auftragIId);
                        if (auftragpositionDtos.length > 0) {
                            zeitdatenDto.setIBelegartpositionid(auftragpositionDtos[0].getIId());
                        }
                    } else if (belegart.equals(LocaleFac.BELEGART_ANGEBOT)) {
                        textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("angb.angebot",
                                theclientDto.getMandant(), theclientDto.getLocUiAsString());

                        if (textDto != null) {
                            s += textDto.getCText() + " ";
                        } else {
                            s += "Angebot ";
                        }

                        com.lp.server.angebot.service.AngebotDto auftragDto = getAngebotFac()
                                .angebotFindByPrimaryKey(auftragIId, theclientDto);
                        s += auftragDto.getCNr();
                        if (auftragDto.getCBez() != null) {
                            s += " " + auftragDto.getCBez();
                        }

                    } else if (belegart.equals(LocaleFac.BELEGART_PROJEKT)) {
                        textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc(
                                "lp.projekt.modulname", theclientDto.getMandant(),
                                theclientDto.getLocUiAsString());

                        if (textDto != null) {
                            s += textDto.getCText() + " ";
                        } else {
                            s += "Projekt ";
                        }

                        com.lp.server.projekt.service.ProjektDto auftragDto = getProjektFac()
                                .projektFindByPrimaryKey(auftragIId);
                        s += auftragDto.getCNr();
                        if (auftragDto.getCTitel() != null) {
                            s += " " + auftragDto.getCTitel();
                        }

                    } else if (belegart.equals(LocaleFac.BELEGART_LOS)) {
                        textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc(
                                "fert.tab.unten.los.title", theclientDto.getMandant(),
                                theclientDto.getLocUiAsString());

                        if (textDto != null) {
                            s += textDto.getCText() + " ";
                        } else {
                            s += "Los ";
                        }

                        LosDto auftragDto = getFertigungFac().losFindByPrimaryKey(auftragIId);
                        s += auftragDto.getCNr();
                        if (auftragDto.getCProjekt() != null) {
                            s += " " + auftragDto.getCProjekt();
                        }

                        LossollarbeitsplanDto[] dtos = getFertigungFac()
                                .lossollarbeitsplanFindByLosIIdArtikelIIdTaetigkeit(auftragIId, artikelId);
                        if (dtos.length > 0) {
                            zeitdatenDto.setIBelegartpositionid(dtos[0].getIId());
                        }

                    }
                    zeitdatenDto.setPersonalIId(personalDto.getIId());
                    zeitdatenDto.setCBelegartnr(belegart);
                    zeitdatenDto.setIBelegartid(auftragIId);
                    zeitdatenDto.setArtikelIId(artikelId);

                    ArtikelDto artikelDto = getArtikelFac().artikelFindByPrimaryKey(artikelId, theclientDto);

                    meldung += s + ", " + artikelDto.formatArtikelbezeichnung();

                    getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, false, theclientDto);
                    meldung += " um " + Helper.formatTime(tZeit, localeLogon) + " gebucht.";
                    getTheClient(request, response).setSMsg(meldung);
                }
            } else {

                getTheClient(request, response).setSMsg("Keine Auftragsposition ausgew\u00E4hlt");
            }
        } else if (buchesondertaetigkeit != null && buchesondertaetigkeit.length() > 0) {
            String zusatz = request.getParameter("zusatz");

            if (zusatz != null && zusatz.length() > 0) {
                // Zeit ist immer jetzt
                Calendar c = Calendar.getInstance();
                c.setTimeInMillis(zeitdatenDto.getTZeit().getTime());
                ZeitdatenDto[] letzeBuchungen = getZeiterfassungsFac()
                        .zeitdatenFindZeitdatenEinesTagesUndEinerPersonOnheBelegzeiten(
                                zeitdatenDto.getPersonalIId(), Helper.cutTimestamp(zeitdatenDto.getTZeit()),
                                zeitdatenDto.getTZeit());
                Integer taetigkeitIId_Kommt = getZeiterfassungsFac()
                        .taetigkeitFindByCNr(ZeiterfassungFac.TAETIGKEIT_KOMMT, theclientDto).getIId();
                Integer taetigkeitIId_Unter = getZeiterfassungsFac()
                        .taetigkeitFindByCNr(ZeiterfassungFac.TAETIGKEIT_UNTER, theclientDto).getIId();
                Integer taetigkeitIId_Geht = getZeiterfassungsFac()
                        .taetigkeitFindByCNr(ZeiterfassungFac.TAETIGKEIT_GEHT, theclientDto).getIId();
                if (zusatz.equals("spezialkommt")) {

                    if (letzeBuchungen.length == 0) {
                        // Zuerst Kommt und dann UNTER
                        ZeitdatenDto dtoKommt = new ZeitdatenDto();
                        dtoKommt.setTaetigkeitIId(taetigkeitIId_Kommt);
                        dtoKommt.setPersonalIId(zeitdatenDto.getPersonalIId());
                        dtoKommt.setCWowurdegebucht("Spezial-Kommt");
                        // Zeit 100 MS vorher
                        dtoKommt.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime()));
                        getZeiterfassungsFac().createZeitdaten(dtoKommt, false, false, false, theclientDto);
                        // Taetigkeit GEHT Buchen
                        ZeitdatenDto dtoUnter = new ZeitdatenDto();
                        dtoUnter.setTaetigkeitIId(taetigkeitIId_Unter);
                        dtoUnter.setPersonalIId(zeitdatenDto.getPersonalIId());
                        dtoUnter.setCWowurdegebucht("Spezial-Kommt");
                        // Zeit 100 MS nachher
                        dtoUnter.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime() + 96));
                        getZeiterfassungsFac().createZeitdaten(dtoUnter, false, false, false, theclientDto);
                    } else if (letzeBuchungen.length == 1) {
                        Integer letztetaetigkeit = letzeBuchungen[0].getTaetigkeitIId();
                        // Wenn nur Kommt, dann Unter buchen
                        if (taetigkeitIId_Kommt.equals(letztetaetigkeit)) {
                            // Taetigkeit UNTER Buchen
                            ZeitdatenDto dtoUnter = new ZeitdatenDto();
                            dtoUnter.setTaetigkeitIId(taetigkeitIId_Unter);
                            dtoUnter.setPersonalIId(zeitdatenDto.getPersonalIId());
                            dtoUnter.setCWowurdegebucht("Spezial-Kommt");
                            dtoUnter.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime()));
                            getZeiterfassungsFac().createZeitdaten(dtoUnter, false, false, false, theclientDto);

                        }
                    } else if (letzeBuchungen.length > 1) {
                        Integer letztetaetigkeit = letzeBuchungen[letzeBuchungen.length - 1].getTaetigkeitIId();
                        if (taetigkeitIId_Kommt.equals(letztetaetigkeit)) {
                            // Taetigkeit UNTER Buchen
                            ZeitdatenDto dtoUnter = new ZeitdatenDto();
                            dtoUnter.setTaetigkeitIId(taetigkeitIId_Unter);
                            dtoUnter.setPersonalIId(zeitdatenDto.getPersonalIId());
                            dtoUnter.setCWowurdegebucht("Spezial-Kommt");
                            dtoUnter.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime()));
                            getZeiterfassungsFac().createZeitdaten(dtoUnter, false, false, false, theclientDto);

                        } else {

                            // Wenn letzte Taetigkeit ein Geht ist wird
                            // Kommt
                            // und Unter gebucht
                            if (!taetigkeitIId_Geht.equals(letztetaetigkeit)) {

                                int iSondertaetigkeitenHintereinander = 1;
                                for (int i = letzeBuchungen.length - 2; i >= 0; i--) {
                                    ZeitdatenDto dto = letzeBuchungen[i];
                                    if (letztetaetigkeit.equals(dto.getTaetigkeitIId())) {
                                        iSondertaetigkeitenHintereinander++;
                                    } else {
                                        break;
                                    }
                                    letztetaetigkeit = dto.getTaetigkeitIId();
                                }

                                if (iSondertaetigkeitenHintereinander % 2 == 0) {
                                    // Taetigkeit UNTER Buchen
                                    ZeitdatenDto dtoUnter = new ZeitdatenDto();
                                    dtoUnter.setTaetigkeitIId(taetigkeitIId_Unter);
                                    dtoUnter.setPersonalIId(zeitdatenDto.getPersonalIId());
                                    dtoUnter.setCWowurdegebucht("Spezial-Geht");
                                    dtoUnter.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime()));
                                    getZeiterfassungsFac().createZeitdaten(dtoUnter, false, false, false,
                                            theclientDto);

                                    /**
                                     * @todo 100ms vorher Projekt-ENDE
                                     *       buchen
                                     */
                                }
                            } else {
                                // Taetigkeit KOMMT Buchen
                                ZeitdatenDto dtoKommt = new ZeitdatenDto();
                                dtoKommt.setTaetigkeitIId(taetigkeitIId_Kommt);
                                dtoKommt.setPersonalIId(zeitdatenDto.getPersonalIId());
                                dtoKommt.setCWowurdegebucht("Spezial-Kommt");
                                dtoKommt.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime()));
                                getZeiterfassungsFac().createZeitdaten(dtoKommt, false, false, false,
                                        theclientDto);
                                // Taetigkeit UNTER Buchen
                                ZeitdatenDto dtoUnter = new ZeitdatenDto();
                                dtoUnter.setTaetigkeitIId(taetigkeitIId_Unter);
                                dtoUnter.setPersonalIId(zeitdatenDto.getPersonalIId());
                                dtoUnter.setCWowurdegebucht("Spezial-Kommt");
                                // Zeit 100 MS nachher
                                dtoUnter.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime() + 96));
                                getZeiterfassungsFac().createZeitdaten(dtoUnter, false, false, false,
                                        theclientDto);

                            }
                        }
                    }

                } else if (zusatz.equals("spezialgeht")) {
                    if (letzeBuchungen.length > 1) {

                        Integer letztetaetigkeit = letzeBuchungen[letzeBuchungen.length - 1].getTaetigkeitIId();
                        // Wenn letzte Taetigkeit kein geht ist, sonst wird
                        // geht verschmissen
                        if (!taetigkeitIId_Geht.equals(letztetaetigkeit)) {

                            int iSondertaetigkeitenHintereinander = 1;
                            for (int i = letzeBuchungen.length - 2; i >= 0; i--) {
                                ZeitdatenDto dto = letzeBuchungen[i];
                                if (letztetaetigkeit.equals(dto.getTaetigkeitIId())) {
                                    iSondertaetigkeitenHintereinander++;
                                } else {
                                    break;
                                }
                                letztetaetigkeit = dto.getTaetigkeitIId();
                            }

                            if (iSondertaetigkeitenHintereinander % 2 == 1) {
                                // Sondertaetigkeit Ende Buchen
                                ZeitdatenDto dtoSonderEnde = new ZeitdatenDto();
                                dtoSonderEnde.setTaetigkeitIId(letztetaetigkeit);
                                dtoSonderEnde.setPersonalIId(zeitdatenDto.getPersonalIId());
                                dtoSonderEnde.setCWowurdegebucht("Spezial-Geht");
                                // Zeit 100 MS vorher
                                dtoSonderEnde.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime() - 96));
                                getZeiterfassungsFac().createZeitdaten(dtoSonderEnde, false, false, false,
                                        theclientDto);
                                // Taetigkeit GEHT Buchen
                                ZeitdatenDto dtoUnter = new ZeitdatenDto();
                                dtoUnter.setTaetigkeitIId(taetigkeitIId_Geht);
                                dtoUnter.setPersonalIId(zeitdatenDto.getPersonalIId());
                                dtoUnter.setCWowurdegebucht("Spezial-Geht");
                                // Zeit 100 MS vorher
                                dtoUnter.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime()));
                                getZeiterfassungsFac().createZeitdaten(dtoUnter, false, false, false,
                                        theclientDto);

                            } else {
                                // Taetigkeit GEHT Buchen
                                ZeitdatenDto dtoUnter = new ZeitdatenDto();
                                dtoUnter.setTaetigkeitIId(taetigkeitIId_Geht);
                                dtoUnter.setPersonalIId(zeitdatenDto.getPersonalIId());
                                dtoUnter.setCWowurdegebucht("Spezial-Geht");
                                // Zeit 100 MS vorher
                                dtoUnter.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime()));
                                getZeiterfassungsFac().createZeitdaten(dtoUnter, false, false, false,
                                        theclientDto);

                            }
                        }

                    }

                } else {
                    response.sendError(HttpServletResponse.SC_BAD_REQUEST, "zusatz '" + zusatz + "' unbekannt");
                }
                setSJSPNext("mecs.jsp");
                return getSJSPNext();
            } else {

                if (request.getParameter("taetigkeit") != null) {

                    Integer taetigkeitId = new Integer(request.getParameter("taetigkeit"));
                    zeitdatenDto.setTaetigkeitIId(taetigkeitId);

                    TaetigkeitDto dto = getZeiterfassungsFac().taetigkeitFindByPrimaryKey(taetigkeitId,
                            theclientDto);
                    meldung += dto.getBezeichnung();

                    getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, false, theclientDto);
                    meldung += " um " + Helper.formatTime(tZeit, localeLogon) + " gebucht.";
                    getTheClient(request, response).setSMsg(meldung);
                } else {
                    getTheClient(request, response).setSMsg("Keine T\u00E4tigkeit ausgew\u00E4hlt");
                }
            }
        }
        getTheClient(request, response).setData(hmData);
        // AD+CK logout wegen usercount
        synchronized (mutex) { // PJ 15986
            getLogonFac().logout(theclientDto);
        }
    } else if (command.equals(TheApp.CMD_ZU_MECS_TERMINAL)) {
        // Personalstamm holen

        String master = request.getParameter("master");

        try {
            ZutrittscontrollerDto zutrittscontrollerDto = getZutrittscontrollerFac()
                    .zutrittscontrollerFindByCNr(master);
            ZutrittsobjektDto[] zutrittsobjektDtos = getZutrittscontrollerFac()
                    .zutrittsobjektFindByZutrittscontrollerIId(zutrittscontrollerDto.getIId());

            StringBuffer objekte = new StringBuffer();

            for (int i = 0; i < zutrittsobjektDtos.length; i++) {
                objekte.append(Helper.fitString2Length(zutrittsobjektDtos[i].getCNr(), 6, ' ')); // terminal-
                // id
                objekte.append(Helper.fitString2Length(zutrittsobjektDtos[i].getCAdresse(), 100, ' ')); // adresse
                objekte.append("\r\n");
            }
            myLogger.info(command + ":" + new String(objekte));

            getTheClient(request, response).setSMsg(new String(objekte));
        }

        catch (RemoteException ex5) {
            if (ex5.getCause() instanceof EJBExceptionLP) {
                EJBExceptionLP lpex = (EJBExceptionLP) ex5.getCause();
                if (lpex.getCode() == EJBExceptionLP.FEHLER_BEI_FIND) {
                    myLogger.error("Zutrittscontroller '" + master + "' nicht angelegt", ex5);
                } else {
                    myLogger.error(ex5.getMessage(), ex5);
                }
            }

        }
    } else if (command.equals(TheApp.CMD_ZU_MECS_RELAIS)) {
        // Personalstamm holen

        String termid = request.getParameter("termid");
        if (termid == null || termid.length() == 0) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                    "Es ist der Parameter termid='LanPortName' erforderlich");
            return null;
        }
        try {
            ZutrittsobjektDto zutrittsobjektDto = getZutrittscontrollerFac().zutrittsobjektFindByCNr(termid);

            StringBuffer objekte = new StringBuffer();

            objekte.append("10"); // readerid
            objekte.append("0"); // port
            objekte.append(zutrittsobjektDto.getCRelais()); // relais
            String oeffnungszeit = zutrittsobjektDto.getFOeffnungszeit().toString();
            oeffnungszeit = oeffnungszeit.replaceAll(",", ".");
            objekte.append(Helper.fitString2LengthAlignRight(oeffnungszeit, 4, ' ')); // oeffnungszeit
            objekte.append(zutrittsobjektDto.getZutrittsleserCNr().trim()); // readerid
            objekte.append("\r\n");
            myLogger.info(command + ":" + new String(objekte));

            getTheClient(request, response).setSMsg(new String(objekte));
        } catch (EJBExceptionLP ex4) {

            if (ex4.getCode() == EJBExceptionLP.FEHLER_BEI_FIND) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND,
                        "Zutrittsobjekt '" + termid + "' nicht angelegt");
                return null;
            } else {
                ex4.printStackTrace();
                myLogger.error(ex4.getMessage(), ex4);
            }

        }
    } else if (command.equals(TheApp.CMD_ZU_MECS_ZUTRITT)) {
        String termid = request.getParameter("termid");
        try {
            ZutrittsobjektDto dto = getZutrittscontrollerFac().zutrittsobjektFindByCNr(termid);

            String s = getZutrittscontrollerFac().getZutrittsdatenFuerEinObjektFuerMecs(dto.getIId(),
                    theclientDto);
            myLogger.info(command + ":" + new String(s));

            getTheClient(request, response).setSMsg(new String(s));
        } catch (EJBExceptionLP ex4) {
            if (ex4.getCode() == EJBExceptionLP.FEHLER_BEI_FIND) {
                myLogger.error("Zutrittsobjekt '" + termid + "' nicht angelegt", ex4);
                response.sendError(HttpServletResponse.SC_NOT_FOUND,
                        "Zutrittsobjekt '" + termid + "' nicht angelegt");

            } else {
                myLogger.error(ex4.getMessage(), ex4);
                response.sendError(HttpServletResponse.SC_NOT_FOUND, ex4.getMessage());
            }

        }

    } else if (command.equals(TheApp.CMD_ZU_MECS_AUSWEISE_ZUTRITT)) {
        PersonalzutrittsklasseDto[] dtos = getZutrittscontrollerFac().personalzutrittsklassenFindByTGueltigab(
                new Timestamp(System.currentTimeMillis()), theclientDto);

        ArrayList<StringBuffer> alDaten = new ArrayList<StringBuffer>();

        for (int i = 0; i < dtos.length; i++) {
            StringBuffer sb = new StringBuffer();
            sb.append("10");

            // Hole personalDto
            PersonalDto personalDto = getPersonalFac().personalFindByPrimaryKeySmall(dtos[i].getPersonalIId());

            sb.append(Helper.fitString2Length(personalDto.getCAusweis(), 20, ' '));
            sb.append(Helper.fitString2Length(personalDto.getCPersonalnr().toString(), 10, ' '));
            sb.append(Helper.fitString2Length("", 24, ' '));

            // Hole Zutrittsklasse
            ZutrittsklasseDto zutrittsklasseDto = getZutrittscontrollerFac()
                    .zutrittsklasseFindByPrimaryKey(dtos[i].getZutrittsklasseIId());
            sb.append(Helper.fitString2Length(zutrittsklasseDto.getCNr(), 3, ' '));
            alDaten.add(sb);
        }

        // Besucherausweise
        String[] ausweise = getZutrittscontrollerFac()
                .zutrittonlinecheckAusweiseFindByTGueltigab(new Timestamp(System.currentTimeMillis()));

        for (int i = 0; i < ausweise.length; i++) {
            StringBuffer sb = new StringBuffer();
            sb.append("10");
            sb.append(Helper.fitString2Length(ausweise[i], 20, ' '));
            sb.append(Helper.fitString2Length("", 10, ' '));
            sb.append(Helper.fitString2Length("", 24, ' '));
            sb.append(Helper.fitString2Length(ZutrittscontrollerFac.ZUTRITTSKLASSE_ONLINECHECK, 3, ' '));
            alDaten.add(sb);
        }

        // sortieren
        String datenGesamt = "";
        for (int i = alDaten.size() - 1; i > 0; --i) {
            for (int j = 0; j < i; ++j) {
                if ((new String(alDaten.get(j))).compareTo(new String(alDaten.get(j + 1))) > 0) {
                    StringBuffer lagerbewegungDtoTemp = alDaten.get(j);
                    alDaten.set(j, alDaten.get(j + 1));
                    alDaten.set(j + 1, lagerbewegungDtoTemp);
                }
            }
        }

        for (int i = 0; i < alDaten.size(); i++) {

            StringBuffer sbTemp = alDaten.get(i);
            sbTemp.append("\r\n");
            datenGesamt += new String(sbTemp);
        }

        myLogger.info(command + ":" + datenGesamt);
        getTheClient(request, response).setSMsg(datenGesamt);
    } else if (command.startsWith(TheApp.CMD_ZU_MECS_ZUTRITT_ONLINE_CHECK)) {
        String termid = request.getParameter("termid");

        String card = request.getParameter("card");
        String pin = request.getParameter("pin");

        try {
            ZutrittsobjektDto dto = getZutrittscontrollerFac().zutrittsobjektFindByCNr(termid);
            boolean b = getZutrittscontrollerFac().onlineCheck(card, pin,
                    new Timestamp(System.currentTimeMillis()), dto.getIId());
            if (b == true) {
                myLogger.info(command + ": ZUTRITT ERLAUBT");
                getTheClient(request, response).setSMsg("A");

            } else {
                myLogger.info(command + ": ZUTRITT VERWEIGERT");
                getTheClient(request, response).setSMsg("Z");
            }
        } catch (EJBExceptionLP ex4) {
            if (ex4.getCause() instanceof EJBExceptionLP) {
                EJBExceptionLP lpex = (EJBExceptionLP) ex4.getCause();
                if (lpex.getCode() == EJBExceptionLP.FEHLER_BEI_FIND) {

                    response.sendError(HttpServletResponse.SC_NOT_FOUND,
                            "Zutrittsobjekt '" + termid + "' nicht angelegt");
                    myLogger.error("Zutrittsobjekt '" + termid + "' nicht angelegt", ex4);
                } else {
                    myLogger.error(ex4.getMessage(), ex4);
                    response.sendError(HttpServletResponse.SC_NOT_FOUND, ex4.getMessage());
                }
            }
        }

    } else if (command.startsWith(TheApp.CMD_ZU_MECS_ZUTRITT_EVENTS)) {
        String termid = request.getParameter("termid");

        if (termid == null || termid.length() == 0) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                    "Es ist der Parameter termid='LanPortName' erforderlich");
            return null;
        }

        ZutrittsobjektDto dto = null;
        try {
            dto = getZutrittscontrollerFac().zutrittsobjektFindByCNr(termid);
        } catch (EJBExceptionLP e) {
            if (e.getCode() == EJBExceptionLP.FEHLER_BEI_FIND) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND,
                        "Zutrittsobjekt '" + termid + "' nicht angelegt");
                return null;
            } else {
                e.printStackTrace();
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                        "Unerwarteter Fehler aufgetreten.");
                return null;
            }
        }

        String s = getZutrittscontrollerFac().getZutrittsEventsFuerMecs(dto.getIId(), theclientDto);
        myLogger.info(command + ":" + s);
        getTheClient(request, response).setSMsg(s);

    } else if (command.startsWith(TheApp.CMD_ZU_MECS_MAXTRANSNR)) {
        getTheClient(request, response).setSMsg("999");
    } else if (command.startsWith(TheApp.CMD_ZU_MECS_LOG)) {

        String record = command.substring(17);
        ZutrittslogDto dto = new ZutrittslogDto();

        Calendar c = Calendar.getInstance();
        String zeitpunkt = record.substring(5, 19);
        int iJahr = new Integer(zeitpunkt.substring(0, 4));
        int iMonat = new Integer(zeitpunkt.substring(4, 6)) - 1;
        int iTag = new Integer(zeitpunkt.substring(6, 8));
        int iStunden = new Integer(zeitpunkt.substring(8, 10));
        int iMinuten = new Integer(zeitpunkt.substring(10, 12));
        int iSekunden = new Integer(zeitpunkt.substring(12, 14));

        c.set(iJahr, iMonat, iTag, iStunden, iMinuten, iSekunden);
        dto.setTZeitpunkt(new Timestamp(c.getTimeInMillis()));
        String personalnr = record.substring(19, 24);
        String erlaubt = record.substring(24, 27);
        String objekt = record.substring(46, 67).trim();
        String ausweis = record.substring(106, 135).trim();
        String event = record.substring(126, 137).trim();

        PersonalDto personalDto = getPersonalFac().personalFindByCAusweis(ausweis);

        if (personalDto != null || event.equals("PINONLINE") || personalnr.equals("?????")
                || personalnr.equals("     ")) {
            if (personalDto != null) {
                dto.setCPerson(
                        getPartnerFac().partnerFindByPrimaryKey(personalDto.getPartnerIId(), theclientDto)
                                .formatFixAnredeTitelName2Name1());
                dto.setMandantCNr(personalDto.getMandantCNr());
            } else if (personalnr.equals("     ")) {
                dto.setCPerson("Besucher");
                dto.setMandantCNr(mandant);
            } else if (event != null && event.equals("PINONLINE")) {
                dto.setCPerson("Tempor\u00E4rer Pin-Code");
                dto.setMandantCNr(mandant);
            } else {
                dto.setCPerson("Unbekannt");
                dto.setMandantCNr(mandant);
            }

            if (erlaubt.equals("ZZ1")) {
                dto.setBErlaubt(Helper.boolean2Short(true));
            } else {
                dto.setBErlaubt(Helper.boolean2Short(false));
            }

            dto.setCAusweis(ausweis);
            dto.setCZutrittscontroller(null);

            try {
                ZutrittsobjektDto zutrittsobjektDto = getZutrittscontrollerFac()
                        .zutrittsobjektFindByCNr(objekt);

                dto.setCZutrittsobjekt(
                        zutrittsobjektDto.getBezeichnung() + "-" + zutrittsobjektDto.getCAdresse());
                dto.setCZutrittscontroller(getZutrittscontrollerFac()
                        .zutrittscontrollerFindByPrimaryKey(zutrittsobjektDto.getZutrittscontrollerIId())
                        .getCNr());
                dto.setMandantCNrObjekt(zutrittsobjektDto.getMandantCNr());
            } catch (RemoteException ex6) {
                dto.setCZutrittsobjekt("Zutrittsobjekt unbekannt");
            }

            getZutrittscontrollerFac().createZutrittslog(dto);
        }

        myLogger.info(command);

    } else if (command.startsWith(TheApp.CMD_ZU_MECS_TEMPLATES)) {

        String sAendern = request.getParameter("changedsince");

        PersonalfingerDto[] personalfingerDtos = null;
        if (sAendern == null) {
            personalfingerDtos = getZutrittscontrollerFac().personalfingerFindAll();

        } else {

            Calendar c = Calendar.getInstance();
            int iJahr = new Integer(sAendern.substring(0, 4));
            int iMonat = new Integer(sAendern.substring(4, 6)) - 1;
            int iTag = new Integer(sAendern.substring(6, 8));
            int iStunden = new Integer(sAendern.substring(8, 10));
            int iMinuten = new Integer(sAendern.substring(10, 12));

            c.set(iJahr, iMonat, iTag, iStunden, iMinuten, 0);
            c.set(Calendar.MILLISECOND, 0);

            personalfingerDtos = getZutrittscontrollerFac()
                    .personalfingerFindByTAendern(new java.sql.Timestamp(c.getTimeInMillis()), theclientDto);
        }
        StringBuffer sb = new StringBuffer();
        // Zuerts alle loeschen
        sb.append(Helper.fitString2LengthAlignRight("0", 5, ' '));
        sb.append(Helper.fitString2LengthAlignRight("0", 2, ' '));
        sb.append(Helper.fitString2Length("X", 512, 'X'));
        StringBuffer zeit = new StringBuffer();
        Calendar cAendern = Calendar.getInstance();
        zeit.append(Helper.fitString2Length(cAendern.get(Calendar.YEAR) + "", 4, '0'));
        zeit.append(Helper.fitString2Length((cAendern.get(Calendar.MONTH) + 1) + "", 2, '0'));
        zeit.append(Helper.fitString2Length(cAendern.get(Calendar.DAY_OF_MONTH) + "", 2, '0'));
        zeit.append(Helper.fitString2Length(cAendern.get(Calendar.HOUR_OF_DAY) + "", 2, '0'));
        zeit.append(Helper.fitString2Length(cAendern.get(Calendar.MINUTE) + "", 2, '0'));

        sb.append(zeit);
        sb.append("\r\n");

        for (int i = 0; i < personalfingerDtos.length; i++) {
            PersonalfingerDto personalfingerDto = personalfingerDtos[i];
            sb.append(Helper.fitString2LengthAlignRight(personalfingerDto.getIId() + "", 5, ' '));
            sb.append(Helper.fitString2LengthAlignRight("1", 2, ' '));
            String templateBase64 = new String(
                    org.apache.commons.codec.binary.Base64.encodeBase64(personalfingerDto.getOTemplate1()));
            sb.append(Helper.fitString2Length(templateBase64, 512, ' '));

            cAendern = Calendar.getInstance();
            cAendern.setTimeInMillis(personalfingerDto.getTAendern().getTime());
            zeit = new StringBuffer();
            zeit.append(Helper.fitString2Length(cAendern.get(Calendar.YEAR) + "", 4, '0'));
            zeit.append(Helper.fitString2Length((cAendern.get(Calendar.MONTH) + 1) + "", 2, '0'));
            zeit.append(Helper.fitString2Length(cAendern.get(Calendar.DAY_OF_MONTH) + "", 2, '0'));
            zeit.append(Helper.fitString2Length(cAendern.get(Calendar.HOUR_OF_DAY) + "", 2, '0'));
            zeit.append(Helper.fitString2Length(cAendern.get(Calendar.MINUTE) + "", 2, '0'));

            sb.append(zeit);
            sb.append("\r\n");

            if (personalfingerDto.getOTemplate2() != null) {
                sb.append(Helper.fitString2LengthAlignRight(personalfingerDto.getIId() + "", 5, ' '));
                sb.append(Helper.fitString2LengthAlignRight("2", 2, ' '));
                templateBase64 = new String(
                        org.apache.commons.codec.binary.Base64.encodeBase64(personalfingerDto.getOTemplate2()));
                sb.append(Helper.fitString2Length(templateBase64, 512, ' '));
                sb.append(zeit);
                if (i == personalfingerDtos.length - 1) {
                    // sb.append("\r");
                } else {
                    sb.append("\r\n");
                }
            }

        }
        getTheClient(request, response).setSMsg(new String(sb));

    }

    return getSJSPNext();
}

From source file:gov.noaa.pfel.erddap.dataset.EDDTableFromNcFiles.java

/** NOT FOR GENERAL USE. Bob uses this to consolidate the individual GTSPP
 * data files into 30 x 30 x 1 month files (tiles).
 * 30 x 30 leads to 12x6=72 files for a given time point, so a request
 * for a short time but entire world opens ~72 files.
 * There are ~240 months worth of data, so a request for a small lon lat 
 * range for all time opens ~240 files.//from ww w. ja  v a 2  s.c  om
 *
 * <p>Why tile? Because there are ~10^6 profiles/year now, so ~10^7 total.
 * And if 100 bytes of info per file for EDDTableFromFiles fileTable, that's 1 GB!.
 * So there needs to be fewer files.
 * We want to balance number of files for 1 time point (all region tiles), 
 * and number of time point files (I'll stick with their use of 1 month).
 * The tiling size selected is ok, but searches for single profile (by name)
 * are slow since a given file may have a wide range of station_ids.
 *
 * <p>Quality flags
 * <br>https://www.nodc.noaa.gov/GTSPP/document/qcmans/GTSPP_RT_QC_Manual_20090916.pdf
 * <br>http://www.ifremer.fr/gosud/formats/gtspp_qcflags.htm
 * <br>CODE  SIGNIFICATION
 * <br>0     NOT CONTROLLED VALUE
 * <br>1     CORRECT VALUE
 * <br>2     VALUE INCONSISTENT WITH STATISTICS
 * <br>3     DOUBTFUL VALUE (spike, ...)
 * <br>4     FALSE VALUE (out of scale, constant profile, vertical instability, ...)
 * <br>5     VALUE MODIFIED DURING QC (only for interpolate location or date)
 * <br>6-8   Not USED
 * <br>9     NO VALUE
 * <br>
 * <br>I interpret as: okay values are 1, 2, 5
 *
 * @param firstYear  e.g., 1990
 * @param firstMonth e.g., 1  (1..)
 * @param lastYear  e.g., 2010
 * @param lastMonth e.g., 12  (1..)     
 * @param testMode if true, this just processes .nc files 
 *    already in testTempDir f:/data/gtspp/testTemp/
 *    and puts results in testDestDir f:/data/gtspp/testDest/.
 *    So the first/last/Year/Month params are ignored.
 */
public static void bobConsolidateGtsppTgz(int firstYear, int firstMonth, int lastYear, int lastMonth,
        boolean testMode) throws Throwable {

    int chunkSize = 45; //lon width, lat height of a tile, in degrees
    int minLat = -90;
    int maxLat = 90;
    int minLon = -180;
    int maxLon = 180;
    String today = Calendar2.getCurrentISODateTimeStringZulu().substring(0, 10); //to nearest day
    String sevenZip = "c:\\progra~1\\7-Zip\\7z";
    String zipDir = "c:\\data\\gtspp\\bestNcZip\\"; //gtspp_at199001.tgz
    String destDir = "c:\\data\\gtspp\\bestNcConsolidated\\";
    String tempDir = "c:\\data\\gtspp\\temp\\";
    String testTempDir = "c:\\data\\gtspp\\testTemp\\"; //tempDir if testMode=true 
    String testDestDir = "c:\\data\\gtspp\\testDest\\"; //destDir if testMode=true
    String logFile = "c:\\data\\gtspp\\log" + String2.replaceAll(today, "-", "") + ".txt";
    File2.makeDirectory(tempDir);
    //https://www.nodc.noaa.gov/GTSPP/document/qcmans/qcflags.htm
    //1=correct, 2=probably correct, 5=modified (so now correct)
    //pre 2012-04-15 was {1,2,5}
    //pre 2012-05-25 was {1,2}
    int okQF[] = { 1, 2, 5 };
    String okQFCsv = String2.toCSSVString(okQF);
    float depthMV = 99999; //was -99;
    float temperatureMV = 99999; //was -99;
    float salinityMV = 99999; //was -99;
    int qMV = 9;
    String timeUnits = "days since 1900-01-01 00:00:00"; //causes roundoff error(!)
    double timeBaseAndFactor[] = Calendar2.getTimeBaseAndFactor(timeUnits);
    //impossible values:
    float minDepth = -0.4f, maxDepth = 10000; //-0.4 allows for imprecise values
    float minTemperature = -4, maxTemperature = 40;
    float minSalinity = 0, maxSalinity = 41;

    if (testMode) {
        firstYear = 1990;
        firstMonth = 1;
        lastYear = 1990;
        lastMonth = 1;
    }

    SSR.verbose = false;

    String2.setupLog(true, false, logFile, false, 1000000000);
    String2.log("*** starting bobConsolidateGtsppTgz " + Calendar2.getCurrentISODateTimeStringLocalTZ() + "\n"
            + "logFile=" + String2.logFileName() + "\n" + String2.standardHelpAboutMessage());
    long elapsedTime = System.currentTimeMillis();
    //q_pos (position quality flag), q_date_time (time quality flag)
    int stationCol = -1, organizationCol = -1, dataTypeCol = -1, platformCol = -1, cruiseCol = -1,
            longitudeCol = -1, latitudeCol = -1, timeCol = -1, depthCol = -1, temperatureCol = -1,
            salinityCol = -1;
    int totalNGoodStation = 0, totalNGoodPos = 0, totalNGoodTime = 0, totalNGoodDepth = 0,
            totalNGoodTemperature = 0, totalNGoodSalinity = 0;
    int totalNBadStation = 0, totalNBadPos = 0, totalNBadTime = 0, totalNBadDepth = 0, totalNBadTemperature = 0,
            totalNBadSalinity = 0, totalNWarnings = 0, totalNExceptions = 0;
    long totalNGoodRows = 0, totalNBadRows = 0;
    StringArray impossibleNanLat = new StringArray();
    StringArray impossibleMinLat = new StringArray();
    StringArray impossibleMaxLat = new StringArray();
    StringArray impossibleNanLon = new StringArray();
    StringArray impossibleMinLon = new StringArray();
    StringArray impossibleMaxLon = new StringArray();
    //StringArray impossibleNaNDepth = new StringArray();
    StringArray impossibleMinDepth = new StringArray();
    StringArray impossibleMaxDepth = new StringArray();
    //StringArray impossibleNanTemperature = new StringArray();
    StringArray impossibleMinTemperature = new StringArray();
    StringArray impossibleMaxTemperature = new StringArray();
    //StringArray impossibleNanSalinity = new StringArray();
    StringArray impossibleMinSalinity = new StringArray();
    StringArray impossibleMaxSalinity = new StringArray();
    int nLons = 0, nLats = 0, nFiles = 0;
    int lonSum = 0, latSum = 0;
    long profilesSum = 0;
    long rowsSum = 0;

    //*** process a month's data
    int year = firstYear;
    int month = firstMonth;
    long chunkTime = System.currentTimeMillis();
    while (year <= lastYear) {
        String2.log("\n*** " + Calendar2.getCurrentISODateTimeStringLocalTZ() + " start processing year=" + year
                + " month=" + month);

        String zMonth = String2.zeroPad("" + month, 2);
        String zMonth1 = String2.zeroPad("" + (month + 1), 2);
        double minEpochSeconds = Calendar2.isoStringToEpochSeconds(year + "-" + zMonth + "-01");
        double maxEpochSeconds = Calendar2.isoStringToEpochSeconds(year + "-" + zMonth1 + "-01");

        //destination directory
        String tDestDir = testMode ? testDestDir : destDir + year + "\\" + zMonth + "\\";
        File2.makeDirectory(tDestDir);
        HashMap tableHashMap = new HashMap();
        //make sure all files are deleted 
        int waitSeconds = 2;
        int nAttempts = 10;
        long cmdTime = System.currentTimeMillis();
        String cmd = "del/q " + tDestDir + "*.*";
        for (int attempt = 0; attempt < nAttempts; attempt++) {
            if (attempt % 8 == 0) {
                String2.log(cmd);
                SSR.dosShell(cmd, 30 * 60); //10 minutes*60 seconds
                //File2.deleteAllFiles(tempDir);  //previous method
            }
            Math2.gc(waitSeconds * 1000); //gtspp: give OS time to settle
            File destDirFile = new File(tDestDir);
            File files[] = destDirFile.listFiles();
            String2.log("  nRemainingFiles=" + files.length);
            if (files.length == 0)
                break;
            waitSeconds = 2 * nAttempts;
        }
        String2.log("  cmd total time=" + Calendar2.elapsedTimeString(System.currentTimeMillis() - cmdTime));

        //unzip all atlantic, indian, and pacific .zip files for that month 
        String region2[] = { "at", "in", "pa" };
        int nRegions = testMode ? 1 : 3;
        for (int region = 0; region < nRegions; region++) {
            String sourceBaseName = "gtspp4_" + region2[region] + year + zMonth;
            String sourceZipJustFileName = sourceBaseName + ".tgz";
            String sourceZipName = zipDir + sourceZipJustFileName;

            if (!testMode) {

                //delete all files in tempDir
                waitSeconds = 2;
                nAttempts = 10;
                cmdTime = System.currentTimeMillis();
                cmd = "del/q " + tempDir + "*.*";
                String2.log(""); //blank line
                for (int attempt = 0; attempt < nAttempts; attempt++) {
                    String2.log(cmd);
                    SSR.dosShell(cmd, 30 * 60); //30 minutes*60 seconds
                    //File2.deleteAllFiles(tempDir);  //previous method

                    //delete dirs too
                    File2.deleteAllFiles(tempDir, true, true);

                    Math2.gc(waitSeconds * 1000); //gtspp: give OS time to settle
                    String2.log("  " + Math2.memoryString());
                    File tempDirFile = new File(tempDir);
                    File files[] = tempDirFile.listFiles();
                    String2.log("  nRemainingFiles=" + files.length);
                    if (files.length == 0)
                        break;
                    waitSeconds = 2 * nAttempts;
                }
                String2.log("  cmd total time="
                        + Calendar2.elapsedTimeString(System.currentTimeMillis() - cmdTime));

                //unzip file into tempDir         //gtspp_at199001.zip
                cmd = sevenZip + " -y e " + sourceZipName + " -o" + tempDir + " -r";
                cmdTime = System.currentTimeMillis();
                String2.log("\n*** " + cmd);
                if (File2.isFile(sourceZipName)) {
                    try {
                        SSR.dosShell(cmd, 30 * 60); //10 minutes*60 seconds
                        String2.log("  cmd time="
                                + Calendar2.elapsedTimeString(System.currentTimeMillis() - cmdTime));

                        //extract from the .tar file   //gtspp4_at199001.tar
                        cmd = sevenZip + " -y e " + tempDir + sourceBaseName + ".tar -o" + tempDir + " -r";
                        cmdTime = System.currentTimeMillis();
                        String2.log("\n*** " + cmd);
                        SSR.dosShell(cmd, 120 * 60); //120 minutes*60 seconds
                        String2.log("  cmd time="
                                + Calendar2.elapsedTimeString(System.currentTimeMillis() - cmdTime));
                    } catch (Exception e) {
                        String2.log("Caught exception: " + MustBe.throwableToString(e));
                    }
                }

                //previous method
                //SSR.unzip(sourceZipName,
                //    tempDir, true, 100 * 60, null); //ignoreZipDirectories, timeOutSeconds 100 minutes
            }

            //read each file and put data in proper table
            String tTempDir = testMode ? testTempDir : tempDir;
            File tTempDirAsFile = new File(tTempDir);
            String sourceFileNames[] = tTempDirAsFile.list(); //just the file names
            String2.log("\nunzipped " + sourceFileNames.length + " files");
            int nSourceFileNames = //testMode? 100 : 
                    sourceFileNames.length;
            int nGoodStation = 0, nGoodPos = 0, nGoodTime = 0, nGoodDepth = 0, nGoodTemperature = 0,
                    nGoodSalinity = 0, nGoodRows = 0;
            int nBadStation = 0, nBadPos = 0, nBadTime = 0, nBadDepth = 0, nBadTemperature = 0,
                    nBadSalinity = 0, nBadRows = 0, nWarnings = 0, nExceptions = 0;
            long fileReadTime = System.currentTimeMillis();
            profilesSum += nSourceFileNames;
            for (int sfi = 0; sfi < nSourceFileNames; sfi++) {
                String sourceFileName = sourceFileNames[sfi];
                if (sfi % 10000 == 0) {
                    //if (sfi > 0)    //2012-12-13 commented out. Let Java handle it.
                    //    Math2.gc(3 * 1000); //gtspp: give OS time to settle
                    //high water mark is ~160 MB, so memory not a problem
                    String2.log("file #" + sfi + " " + Math2.memoryString());
                }

                if (!sourceFileName.endsWith(".nc")) {
                    //String2.log("ERROR: not a .nc file: " + sourceFileName);
                    continue;
                }

                NetcdfFile ncFile = null;

                try {
                    //get the station name
                    //gtspp_13635162_te_111.nc  gtspp_10313692_cu_111.nc
                    if (!sourceFileName.matches("gtspp_[0-9]+_.*\\.nc")) { //was "\\d+")) {//all digits
                        nBadStation++;
                        throw new SimpleException("Invalid sourceFileName=" + sourceFileName);
                    }
                    int po = sourceFileName.indexOf('_', 6);
                    if (po < 0) {
                        nBadStation++;
                        throw new SimpleException("Invalid sourceFileName=" + sourceFileName);
                    }
                    int station = String2.parseInt(sourceFileName.substring(6, po));
                    nGoodStation++;
                    String key = sourceZipJustFileName + " " + sourceFileName;

                    //open the file
                    ncFile = NcHelper.openFile(tTempDir + sourceFileName);
                    Variable var;
                    Attributes tVarAtts = new Attributes();
                    String tUnits;

                    //get all of the data 

                    //stream_ident
                    var = ncFile.findVariable("stream_ident");
                    String organization = "";
                    String dataType = "";
                    if (var == null) {
                        nWarnings++;
                        String2.log("WARNING: No stream_ident in " + sourceFileName);
                    } else {
                        PrimitiveArray streamPA = NcHelper.getPrimitiveArray(var);
                        if (streamPA instanceof StringArray && streamPA.size() > 0) {
                            String stream = streamPA.getString(0);
                            if (stream.length() >= 4) {
                                organization = stream.substring(0, 2).trim();
                                dataType = stream.substring(2, 4).trim();
                            } else {
                                String2.log("WARNING: stream_ident isn't a 4 char string: " + stream);
                            }
                        } else {
                            String2.log("WARNING: stream_ident isn't a StringArray: " + streamPA.toString());
                        }
                    }

                    //platform_code
                    var = ncFile.findVariable("gtspp_platform_code");
                    String platform = "";
                    if (var == null) {
                        //a small percentage have this problem
                        //nWarnings++;
                        //String2.log("WARNING: No gtspp_platform_code in " + sourceFileName);
                    } else {
                        PrimitiveArray pa = NcHelper.getPrimitiveArray(var);
                        if (pa instanceof StringArray && pa.size() > 0) {
                            platform = pa.getString(0).trim();
                            //String2.log("platform_code=" + platform_code);
                        } else {
                            String2.log("WARNING: gtspp_platform_code isn't a StringArray: " + pa.toString());
                        }
                    }

                    //cruise
                    var = ncFile.findVariable("cruise_id");
                    String cruise = "";
                    if (var == null) {
                        nWarnings++;
                        String2.log("WARNING: No cruise_id in " + sourceFileName);
                    } else {
                        PrimitiveArray cruisePA = NcHelper.getPrimitiveArray(var);
                        if (cruisePA instanceof StringArray && cruisePA.size() > 0) {
                            cruise = cruisePA.getString(0).trim();
                        } else {
                            String2.log("WARNING: cruise_id isn't a StringArray: " + cruisePA.toString());
                        }
                    }

                    //prof_type  is TEMP or PSAL so don't save it.
                    /*var = ncFile.findVariable("prof_type");                        
                    String prof_type = "";
                    if (var == null) {
                    nWarnings++;
                    String2.log("WARNING: No prof_type in " + sourceFileName);
                    } else {
                    PrimitiveArray pa = NcHelper.getPrimitiveArray(var);
                    if (pa instanceof StringArray && pa.size() > 0) {
                        prof_type = pa.getString(0).trim();
                        String2.log("prof_type=" + prof_type);
                    } else {
                        String2.log("WARNING: prof_type isn't a StringArray: " + 
                            pa.toString());
                    }
                    }*/

                    //position quality flag 
                    var = ncFile.findVariable("position_quality_flag"); //was "q_pos");                        
                    if (var == null) {
                        nWarnings++;
                        String2.log("WARNING: No position_quality_flag in " + sourceFileName);
                    } else {
                        PrimitiveArray q_pos = NcHelper.getPrimitiveArray(var);
                        if (!(q_pos instanceof IntArray) || q_pos.size() != 1)
                            throw new SimpleException("Invalid position_quality_flag=" + q_pos);
                        int ti = q_pos.getInt(0);
                        if (String2.indexOf(okQF, ti) < 0) {
                            nBadPos++;
                            continue;
                        }
                        //nGoodPos++; is below
                    }

                    //time quality flag 
                    var = ncFile.findVariable("time_quality_flag"); //q_date_time");                        
                    if (var == null) {
                        nWarnings++;
                        String2.log("WARNING: No time_quality_flag in " + sourceFileName);
                    } else {
                        PrimitiveArray q_date_time = NcHelper.getPrimitiveArray(var);
                        if (!(q_date_time instanceof IntArray) || q_date_time.size() != 1)
                            throw new SimpleException("Invalid time_quality_flag=" + q_date_time);
                        int ti = q_date_time.getInt(0);
                        if (String2.indexOf(okQF, ti) < 0) {
                            nBadTime++;
                            continue;
                        }
                        //nGoodTime is below
                    }

                    //time
                    var = ncFile.findVariable("time");
                    if (var == null)
                        throw new SimpleException("No time!");
                    tVarAtts.clear();
                    NcHelper.getVariableAttributes(var, tVarAtts);
                    tUnits = tVarAtts.getString("units");
                    if (!timeUnits.equals(tUnits))
                        throw new SimpleException("Invalid time units=" + tUnits);
                    PrimitiveArray time = NcHelper.getPrimitiveArray(var);
                    if (!(time instanceof DoubleArray) || time.size() != 1)
                        throw new SimpleException("Invalid time=" + time);
                    double tTime = Calendar2.unitsSinceToEpochSeconds(timeBaseAndFactor[0],
                            timeBaseAndFactor[1], time.getDouble(0));
                    if (tTime < minEpochSeconds || tTime > maxEpochSeconds)
                        throw new SimpleException(
                                "Invalid tTime=" + Calendar2.safeEpochSecondsToIsoStringTZ(tTime, ""));
                    //original times (that I looked at) are to nearest second
                    //so round to nearest second (fix .99999 problems)
                    tTime = Math.rint(tTime);
                    nGoodTime++;

                    //longitude  (position qFlag is good)
                    var = ncFile.findVariable("longitude");
                    if (var == null) {
                        impossibleNanLon.add(key + " lon=null");
                        continue;
                    }
                    PrimitiveArray longitude = NcHelper.getPrimitiveArray(var);
                    if (!(longitude instanceof FloatArray) || longitude.size() != 1) {
                        impossibleNanLon.add(key + " lon=wrongTypeOrSize");
                        continue;
                    }
                    float lon = longitude.getFloat(0);
                    if (Float.isNaN(lon)) {
                        impossibleNanLon.add(key + " lon=NaN");
                        continue;
                    } else if (lon < minLon) {
                        impossibleMinLon.add(key + " lon=" + lon);
                        //fall through
                    } else if (lon > maxLon) {
                        impossibleMaxLon.add(key + " lon=" + lon);
                        //fall through
                    }
                    lon = (float) Math2.anglePM180(lon);

                    //latitude (position qFlag is good)
                    var = ncFile.findVariable("latitude");
                    if (var == null) {
                        impossibleNanLat.add(key + " lat=null");
                        continue;
                    }
                    PrimitiveArray latitude = NcHelper.getPrimitiveArray(var);
                    if (!(latitude instanceof FloatArray) || latitude.size() != 1) {
                        impossibleNanLat.add(key + " lat=wrongTypeOrSize");
                        continue;
                    }
                    float lat = latitude.getFloat(0);
                    if (Float.isNaN(lat)) {
                        impossibleNanLat.add(key + " lat=NaN");
                        continue;
                    } else if (lat < minLat) {
                        impossibleMinLat.add(key + " lat=" + lat);
                        continue;
                    } else if (lat > maxLat) {
                        impossibleMaxLat.add(key + " lat=" + lat);
                        continue;
                    }
                    nGoodPos++;

                    //depth
                    var = ncFile.findVariable("z");
                    if (var == null)
                        throw new SimpleException("No z!");
                    PrimitiveArray depth = NcHelper.getPrimitiveArray(var);
                    if (!(depth instanceof FloatArray) || depth.size() == 0)
                        throw new SimpleException("Invalid z=" + depth);
                    int nDepth = depth.size();

                    //DEPH_qparm
                    var = ncFile.findVariable("z_variable_quality_flag"); //DEPH_qparm");                        
                    if (var == null)
                        throw new SimpleException("No z_variable_quality_flag!");
                    PrimitiveArray DEPH_qparm = NcHelper.getPrimitiveArray(var);
                    if (!(DEPH_qparm instanceof IntArray) || DEPH_qparm.size() != nDepth)
                        throw new SimpleException("Invalid z_variable_quality_flag=" + DEPH_qparm);
                    //nGoodDepth is below

                    //temperature
                    var = ncFile.findVariable("temperature");
                    PrimitiveArray temperature;
                    PrimitiveArray TEMP_qparm;
                    float temperatureFV = temperatureMV;
                    if (var == null) {
                        //nWarnings++;
                        //String2.log("WARNING: No temperature in " + sourceFileName); reasonably common
                        temperature = PrimitiveArray.factory(float.class, nDepth, "" + temperatureMV);
                        TEMP_qparm = PrimitiveArray.factory(int.class, nDepth, "" + qMV);
                    } else {
                        temperature = NcHelper.getPrimitiveArray(var);
                        if (!(temperature instanceof FloatArray) || temperature.size() != nDepth)
                            throw new SimpleException("Invalid temperature=" + temperature);

                        tVarAtts.clear();
                        NcHelper.getVariableAttributes(var, tVarAtts);
                        temperatureFV = tVarAtts.getFloat("_FillValue");
                        if (!Float.isNaN(temperatureFV) && temperatureFV != temperatureMV)
                            throw new SimpleException("Invalid temperature _FillValue=" + temperatureFV);

                        //TEMP_qparm
                        var = ncFile.findVariable("temperature_quality_flag"); //TEMP_qparm");                        
                        if (var == null) {
                            nWarnings++;
                            String2.log("WARNING: No temperature_quality_flag in " + sourceFileName);
                            TEMP_qparm = PrimitiveArray.factory(int.class, nDepth, "" + qMV);
                        } else {
                            TEMP_qparm = NcHelper.getPrimitiveArray(var);
                            if (!(TEMP_qparm instanceof IntArray) || TEMP_qparm.size() != nDepth)
                                throw new SimpleException("Invalid temperature_quality_flag=" + TEMP_qparm);
                        }
                    }

                    //salinity
                    var = ncFile.findVariable("salinity");
                    PrimitiveArray salinity;
                    PrimitiveArray PSAL_qparm;
                    float salinityFV = salinityMV;
                    if (var == null) {
                        //String2.log("WARNING: No salinity in " + sourceFileName);   //very common
                        salinity = PrimitiveArray.factory(float.class, nDepth, "" + salinityMV);
                        PSAL_qparm = PrimitiveArray.factory(int.class, nDepth, "" + qMV);
                    } else {
                        salinity = NcHelper.getPrimitiveArray(var);
                        if (!(salinity instanceof FloatArray) || salinity.size() != nDepth)
                            throw new SimpleException("Invalid salinity=" + salinity);

                        tVarAtts.clear();
                        NcHelper.getVariableAttributes(var, tVarAtts);
                        salinityFV = tVarAtts.getFloat("_FillValue");
                        if (!Float.isNaN(salinityFV) && salinityFV != salinityMV)
                            throw new SimpleException("Invalid salinity _FillValue=" + salinityFV);

                        //PSAL_qparm
                        var = ncFile.findVariable("salinity_quality_flag"); //PSAL_qparm");                        
                        if (var == null) {
                            nWarnings++;
                            String2.log("WARNING: No salinity_quality_flag in " + sourceFileName);
                            PSAL_qparm = PrimitiveArray.factory(int.class, nDepth, "" + qMV);
                        } else {
                            PSAL_qparm = NcHelper.getPrimitiveArray(var);
                            if (!(PSAL_qparm instanceof IntArray) || PSAL_qparm.size() != nDepth)
                                throw new SimpleException("Invalid salinity_quality_flag=" + PSAL_qparm);
                        }
                    }

                    //clean the data
                    //(good to do it here so memory usage is low -- table remains as small as possible)
                    //Change "impossible" data to NaN
                    //(from https://www.nodc.noaa.gov/GTSPP/document/qcmans/GTSPP_RT_QC_Manual_20090916.pdf
                    //pg 61 has Table 2.1: Global Impossible Parameter Values).
                    BitSet keep = new BitSet();
                    keep.set(0, nDepth); //all true 

                    //find worst impossible depth/temperature/salinity for this station
                    //boolean tImpossibleNanDepth       = false;
                    //boolean tImpossibleNanTemperature = false;
                    //boolean tImpossibleNanSalinity    = false;
                    float tImpossibleMinDepth = minDepth;
                    float tImpossibleMaxDepth = maxDepth;
                    float tImpossibleMinTemperature = minTemperature;
                    float tImpossibleMaxTemperature = maxTemperature;
                    float tImpossibleMinSalinity = minSalinity;
                    float tImpossibleMaxSalinity = maxSalinity;

                    for (int row = 0; row < nDepth; row++) {

                        //DEPH_qparm
                        int qs = DEPH_qparm.getInt(row);
                        float f = depth.getFloat(row);
                        if (String2.indexOf(okQF, qs) < 0) {
                            nBadDepth++;
                            keep.clear(row);
                            continue;
                        } else if (Float.isNaN(f) || f == depthMV) { //"impossible" depth
                            //tImpossibleNanDepth = true;
                            nBadDepth++;
                            keep.clear(row);
                            continue;
                        } else if (f < minDepth) {
                            tImpossibleMinDepth = Math.min(tImpossibleMinDepth, f);
                            nBadDepth++;
                            keep.clear(row);
                            continue;
                        } else if (f > maxDepth) {
                            tImpossibleMaxDepth = Math.max(tImpossibleMaxDepth, f);
                            nBadDepth++;
                            keep.clear(row);
                            continue;
                        }
                        nGoodDepth++;

                        boolean hasData = false;

                        //temperature
                        qs = TEMP_qparm.getInt(row);
                        f = temperature.getFloat(row);
                        if (String2.indexOf(okQF, qs) < 0) {
                            temperature.setString(row, ""); //so bad value is now NaN
                            nBadTemperature++;
                        } else if (Float.isNaN(f) || f == temperatureMV) {
                            temperature.setString(row, ""); //so missing value is now NaN
                            nBadTemperature++;
                        } else if (f < minTemperature) { //"impossible" water temperature
                            tImpossibleMinTemperature = Math.min(tImpossibleMinTemperature, f);
                            temperature.setString(row, ""); //so impossible value is now NaN
                            nBadTemperature++;
                        } else if (f > maxTemperature) { //"impossible" water temperature
                            tImpossibleMaxTemperature = Math.max(tImpossibleMaxTemperature, f);
                            temperature.setString(row, ""); //so impossible value is now NaN
                            nBadTemperature++;
                        } else {
                            nGoodTemperature++;
                            hasData = true;
                        }

                        //salinity
                        qs = PSAL_qparm.getInt(row);
                        f = salinity.getFloat(row);
                        if (String2.indexOf(okQF, qs) < 0) {
                            salinity.setString(row, ""); //so bad value is now NaN
                            nBadSalinity++;
                        } else if (Float.isNaN(f) || f == salinityMV) {
                            salinity.setString(row, ""); //so missing value is now NaN
                            nBadSalinity++;
                        } else if (f < minSalinity) { //"impossible" salinity
                            tImpossibleMinSalinity = Math.min(tImpossibleMinSalinity, f);
                            salinity.setString(row, ""); //so impossible value is now NaN
                            nBadSalinity++;
                        } else if (f > maxSalinity) { //"impossible" salinity
                            tImpossibleMaxSalinity = Math.max(tImpossibleMaxSalinity, f);
                            salinity.setString(row, ""); //so impossible value is now NaN
                            nBadSalinity++;
                        } else {
                            nGoodSalinity++;
                            hasData = true;
                        }

                        //no valid temperature or salinity data?
                        if (!hasData) {
                            keep.clear(row);
                        }
                    }

                    //ensure sizes still correct
                    Test.ensureEqual(depth.size(), nDepth, "depth.size changed!");
                    Test.ensureEqual(temperature.size(), nDepth, "temperature.size changed!");
                    Test.ensureEqual(salinity.size(), nDepth, "salinity.size changed!");

                    //actually remove the bad rows
                    int tnGood = keep.cardinality();
                    if (testMode && verbose)
                        String2.log(
                                sourceFileName + ": nGoodRows=" + tnGood + " nBadRows=" + (nDepth - tnGood));
                    nGoodRows += tnGood;
                    nBadRows += nDepth - tnGood;
                    depth.justKeep(keep);
                    temperature.justKeep(keep);
                    salinity.justKeep(keep);
                    nDepth = depth.size();

                    //impossible
                    //if (tImpossibleNanDepth)
                    //     impossibleNanDepth.add(key + " hasNaN=true");
                    //if (tImpossibleNanTemperature)
                    //     impossibleNanTemperature.add(key + " hasNaN=true");
                    //if (tImpossibleNanSalinity)
                    //     impossibleNanSalinity.add(key + " hasNaN=true");

                    if (tImpossibleMinDepth < minDepth)
                        impossibleMinDepth.add(key + " worst = " + tImpossibleMinDepth);
                    if (tImpossibleMaxDepth > maxDepth)
                        impossibleMaxDepth.add(key + " worst = " + tImpossibleMaxDepth);
                    if (tImpossibleMinTemperature < minTemperature)
                        impossibleMinTemperature.add(key + " worst = " + tImpossibleMinTemperature);
                    if (tImpossibleMaxTemperature > maxTemperature)
                        impossibleMaxTemperature.add(key + " worst = " + tImpossibleMaxTemperature);
                    if (tImpossibleMinSalinity < minSalinity)
                        impossibleMinSalinity.add(key + " worst = " + tImpossibleMinSalinity);
                    if (tImpossibleMaxSalinity > maxSalinity)
                        impossibleMaxSalinity.add(key + " worst = " + tImpossibleMaxSalinity);

                    //which table
                    if (tnGood == 0)
                        continue;
                    int loni = Math2
                            .roundToInt(Math.floor((Math.min(lon, maxLon - 0.1f) - minLon) / chunkSize));
                    int lati = Math2
                            .roundToInt(Math.floor((Math.min(lat, maxLat - 0.1f) - minLat) / chunkSize));
                    String outTableName = (minLon + loni * chunkSize) + "E_" + (minLat + lati * chunkSize)
                            + "N";
                    //String2.replaceAll(cruise + "_" + organization + dataType, ' ', '_'); //too many: 3000+/month in 2011
                    Table tTable = (Table) tableHashMap.get(outTableName);

                    if (tTable == null) {

                        Attributes ncGlobalAtts = new Attributes();
                        NcHelper.getGlobalAttributes(ncFile, ncGlobalAtts);
                        String tHistory = ncGlobalAtts.getString("history");
                        tHistory = tHistory != null && tHistory.length() > 0 ? tHistory + "\n" : "";

                        //make a table for this platform
                        tTable = new Table();
                        Attributes ga = tTable.globalAttributes();
                        String ack = "These data were acquired from the US NOAA National Oceanographic Data Center (NODC) on "
                                + today + " from https://www.nodc.noaa.gov/GTSPP/.";
                        ga.add("acknowledgment", ack);
                        ga.add("license",
                                "These data are openly available to the public.  "
                                        + "Please acknowledge the use of these data with:\n" + ack + "\n\n"
                                        + "[standard]");
                        ga.add("history", tHistory
                                + ".tgz files from ftp.nodc.noaa.gov /pub/gtspp/best_nc/ (https://www.nodc.noaa.gov/GTSPP/)\n"
                                + today
                                + " Most recent ingest, clean, and reformat at ERD (bob.simons at noaa.gov).");
                        ga.add("infoUrl", "https://www.nodc.noaa.gov/GTSPP/");
                        ga.add("institution", "NOAA NODC");
                        ga.add("title", "Global Temperature and Salinity Profile Programme (GTSPP) Data");

                        String attName = "gtspp_ConventionVersion";
                        String attValue = ncGlobalAtts.getString(attName);
                        if (attValue != null && attValue.length() > 0)
                            ga.add(attName, attValue);

                        attName = "gtspp_program";
                        attValue = ncGlobalAtts.getString(attName);
                        if (attValue != null && attValue.length() > 0)
                            ga.add(attName, attValue);

                        attName = "gtspp_programVersion";
                        attValue = ncGlobalAtts.getString(attName);
                        if (attValue != null && attValue.length() > 0)
                            ga.add(attName, attValue);

                        attName = "gtspp_handbook_version";
                        attValue = ncGlobalAtts.getString(attName);
                        if (attValue != null && attValue.length() > 0)
                            ga.add(attName, attValue);

                        organizationCol = tTable.addColumn(tTable.nColumns(), "org", new StringArray(),
                                new Attributes());
                        platformCol = tTable.addColumn(tTable.nColumns(), "platform", new StringArray(),
                                new Attributes());
                        dataTypeCol = tTable.addColumn(tTable.nColumns(), "type", new StringArray(),
                                new Attributes());
                        cruiseCol = tTable.addColumn(tTable.nColumns(), "cruise", new StringArray(),
                                new Attributes());
                        stationCol = tTable.addColumn(tTable.nColumns(), "station_id", new IntArray(),
                                new Attributes());
                        longitudeCol = tTable.addColumn(tTable.nColumns(), "longitude", new FloatArray(),
                                (new Attributes()).add("units", EDV.LON_UNITS));
                        latitudeCol = tTable.addColumn(tTable.nColumns(), "latitude", new FloatArray(),
                                (new Attributes()).add("units", EDV.LAT_UNITS));
                        timeCol = tTable.addColumn(tTable.nColumns(), "time", new DoubleArray(),
                                (new Attributes()).add("units", EDV.TIME_UNITS));
                        depthCol = tTable.addColumn(tTable.nColumns(), "depth", new FloatArray(),
                                (new Attributes()).add("units", "m"));
                        temperatureCol = tTable.addColumn(tTable.nColumns(), "temperature", new FloatArray(),
                                (new Attributes()).add("units", "degree_C"));
                        salinityCol = tTable.addColumn(tTable.nColumns(), "salinity", new FloatArray(),
                                (new Attributes()).add("units", "1e-3")); //PSU changed to 1e-3 with CF std names 25

                        tableHashMap.put(outTableName, tTable);
                    }

                    //put data in tTable
                    int oNRows = tTable.nRows();
                    ((StringArray) tTable.getColumn(organizationCol)).addN(nDepth, organization);
                    ((StringArray) tTable.getColumn(platformCol)).addN(nDepth, platform);
                    ((StringArray) tTable.getColumn(dataTypeCol)).addN(nDepth, dataType);
                    ((StringArray) tTable.getColumn(cruiseCol)).addN(nDepth, cruise);
                    ((IntArray) tTable.getColumn(stationCol)).addN(nDepth, station);
                    ((FloatArray) tTable.getColumn(longitudeCol)).addN(nDepth, lon);
                    ((FloatArray) tTable.getColumn(latitudeCol)).addN(nDepth, lat);
                    ((DoubleArray) tTable.getColumn(timeCol)).addN(nDepth, tTime);
                    ((FloatArray) tTable.getColumn(depthCol)).append(depth);
                    ((FloatArray) tTable.getColumn(temperatureCol)).append(temperature);
                    ((FloatArray) tTable.getColumn(salinityCol)).append(salinity);

                    //ensure the table is valid (same size for each column)
                    tTable.ensureValid();

                } catch (Throwable t) {
                    nExceptions++;
                    String2.log(
                            "ERROR while processing " + sourceFileName + "\n  " + MustBe.throwableToString(t));
                } finally {
                    //always close the ncFile
                    if (ncFile != null) {
                        try {
                            ncFile.close();
                        } catch (Throwable t) {
                            String2.log("ERROR: unable to close " + sourceFileName + "\n"
                                    + MustBe.getShortErrorMessage(t));
                        }
                    }
                }
            }

            String2.log("\n  time to read all those files = "
                    + Calendar2.elapsedTimeString(System.currentTimeMillis() - fileReadTime));

            //end of region loop
            String2.log("\nIn zip=" + sourceZipName + "\n nExceptions=    " + nExceptions + "        nWarnings="
                    + nWarnings + "\n nBadStation=    " + nBadStation + "        nGoodStation=" + nGoodStation
                    + "\n nBadPos=        " + nBadPos + "        nGoodPos=" + nGoodPos + "\n nBadTime=       "
                    + nBadTime + "        nGoodTime=" + nGoodTime + "\n nBadDepth=      " + nBadDepth
                    + "        nGoodDepth=" + nGoodDepth + "\n nBadTemperature=" + nBadTemperature
                    + "        nGoodTemperature=" + nGoodTemperature + "\n nBadSalinity=   " + nBadSalinity
                    + "        nGoodSalinity=" + nGoodSalinity);
            totalNGoodStation += nGoodStation;
            totalNGoodPos += nGoodPos;
            totalNGoodTime += nGoodTime;
            totalNGoodDepth += nGoodDepth;
            totalNGoodTemperature += nGoodTemperature;
            totalNGoodSalinity += nGoodSalinity;
            totalNGoodRows += nGoodRows;
            totalNBadPos += nBadPos;
            totalNBadTime += nBadTime;
            totalNBadDepth += nBadDepth;
            totalNBadTemperature += nBadTemperature;
            totalNBadSalinity += nBadSalinity;
            totalNBadRows += nBadRows;
            totalNWarnings += nWarnings;
            totalNExceptions += nExceptions;
        } //end of region loop

        //save by outTableName
        boolean filePrinted = false;
        Object keys[] = tableHashMap.keySet().toArray();
        int nKeys = keys.length;
        String2.log("\n*** saving nFiles=" + nKeys);
        for (int keyi = 0; keyi < nKeys; keyi++) {
            String key = keys[keyi].toString();
            Table tTable = (Table) tableHashMap.remove(key);
            if (tTable == null || tTable.nRows() == 0) {
                String2.log("Unexpected: no table for key=" + key);
                continue;
            }

            //sort by time, station, depth  
            //depth matches the source files: from surface to deepest
            tTable.sort(new int[] { timeCol, stationCol, depthCol }, new boolean[] { true, true, true });

            //is this saving a small lat lon range?
            double stationStats[] = tTable.getColumn(stationCol).calculateStats();
            //double lonStats[]     = tTable.getColumn(longitudeCol).calculateStats();
            //double latStats[]     = tTable.getColumn(latitudeCol).calculateStats();
            //nLats++;
            //double latRange = latStats[PrimitiveArray.STATS_MAX] - latStats[PrimitiveArray.STATS_MIN];
            //latSum += latRange;
            rowsSum += tTable.nRows();
            String2.log("    stationRange=" + Math2.roundToInt(
                    stationStats[PrimitiveArray.STATS_MAX] - stationStats[PrimitiveArray.STATS_MIN]) +
            //"  lonRange="     + Math2.roundToInt(lonStats[    PrimitiveArray.STATS_MAX] - lonStats[    PrimitiveArray.STATS_MIN]) +
            //"  latRange="     + Math2.roundToInt(latRange) +
                    "  nRows=" + tTable.nRows());

            //save it
            String tName = tDestDir + String2.encodeFileNameSafe(key);
            /*if (lonStats[PrimitiveArray.STATS_MAX] > 45 &&
            lonStats[PrimitiveArray.STATS_MIN] < -45) {
                    
            //NO MORE: This happened with 1 file/cruise, 
            //  but won't happen now with lon/lat tiles.
            //crosses dateline (or widely across lon=0)?  split into 2 files
            Table ttTable = (Table)tTable.clone();
            ttTable.oneStepApplyConstraint(0, "longitude", "<", "0");
            ttTable.saveAsFlatNc(tName + "_W.nc", "row", false);
            double lonStatsW[] = ttTable.getColumn(longitudeCol).calculateStats();
            nLons++;
            double lonRangeW = lonStatsW[PrimitiveArray.STATS_MAX] - lonStatsW[PrimitiveArray.STATS_MIN];
            lonSum += lonRangeW;
                    
            ttTable = (Table)tTable.clone();
            ttTable.oneStepApplyConstraint(0, "longitude", ">=", "0");
            ttTable.saveAsFlatNc(tName + "_E.nc", "row", false);
            double lonStatsE[] = ttTable.getColumn(longitudeCol).calculateStats();
            nLons++;
            double lonRangeE = lonStatsE[PrimitiveArray.STATS_MAX] - lonStatsE[PrimitiveArray.STATS_MIN];
            lonSum += lonRangeE;
            String2.log("  westLonRange=" + Math2.roundToInt(lonRangeW) +
                        "  eastLonRange=" + Math2.roundToInt(lonRangeE));
            } else */
            {
                //nLons++;
                nFiles++;

                //create trajectory variable: platform + cruise
                StringArray pl = (StringArray) tTable.getColumn("platform");
                StringArray cr = (StringArray) tTable.getColumn("cruise");
                StringArray or = (StringArray) tTable.getColumn("org");
                StringArray ty = (StringArray) tTable.getColumn("type");
                StringArray tr = new StringArray();
                int n = pl.size();
                for (int i = 0; i < n; i++) {
                    pl.set(i, String2.whitespacesToSpace(pl.get(i)));
                    cr.set(i, String2.whitespacesToSpace(cr.get(i)));
                    or.set(i, String2.whitespacesToSpace(or.get(i)));
                    ty.set(i, String2.whitespacesToSpace(ty.get(i)));
                    tr.add(or.getString(i) + "_" + ty.getString(i) + "_" + pl.getString(i) + "_"
                            + cr.getString(i));
                }
                tTable.addColumn(0, "trajectory", tr, new Attributes());

                tTable.saveAsFlatNc(tName + ".nc", "row", false); //convertToFakeMissingValues  (keep mv's as NaNs)
            }

            //print a file
            if (testMode && !filePrinted) {
                filePrinted = true;
                String2.log(NcHelper.dumpString(tName, true));
            }
        }
        String2.log("\ncumulative nProfiles=" + profilesSum + " nRows=" + rowsSum + " mean nRows/file="
                + (rowsSum / Math.max(1, nFiles)));
        //if (nLats > 0) 
        //    String2.log(  "cumulative nLats=" + nLats + " meanLatRange=" + (float)(latSum / nLats));
        //if (nLons > 0) {
        //    String2.log(  "cumulative nLons=" + nLons + " meanLonRange=" + (float)(lonSum / nLons));
        //    String2.log("mean nRows per saved file = " + (rowsSum / nLons));
        //}

        //print list of impossible at end of year or end of run
        if (month == 12 || (year == lastYear && month == lastMonth)) {

            String2.log("\n*** " + Calendar2.getCurrentISODateTimeStringLocalTZ()
                    + " bobConsolidateGtsppTgz finished the chunk ending " + year + "-" + month + "\n"
                    + "chunkTime=" + Calendar2.elapsedTimeString(System.currentTimeMillis() - chunkTime));
            chunkTime = System.currentTimeMillis();

            //print impossible statistics
            String2.log("\nCumulative number of stations with:\n" + "impossibleNanLon         = "
                    + impossibleNanLon.size() + "\n" + "impossibleMinLon         = " + impossibleMinLon.size()
                    + "\n" + "impossibleMaxLon         = " + impossibleMaxLon.size() + "\n"
                    + "impossibleNanLat         = " + impossibleNanLat.size() + "\n"
                    + "impossibleMinLat         = " + impossibleMinLat.size() + "\n"
                    + "impossibleMaxLat         = " + impossibleMaxLat.size() + "\n"
                    + "impossibleMinDepth       = " + impossibleMinDepth.size() + "\n"
                    + "impossibleMaxDepth       = " + impossibleMaxDepth.size() + "\n" +
                    //"impossibleLatLon      = " + impossibleLatLon.size() + "\n" +
                    "impossibleMinTemperature = " + impossibleMinTemperature.size() + "\n"
                    + "impossibleMaxTemperature = " + impossibleMaxTemperature.size() + "\n"
                    + "impossibleMinSalinity    = " + impossibleMinSalinity.size() + "\n"
                    + "impossibleMaxSalinity    = " + impossibleMaxSalinity.size() + "\n");

            //lon
            String2.log("\n*** " + impossibleNanLon.size() + " stations had invalid lon"
                    + " and good pos quality flags (" + okQFCsv + ").");
            impossibleNanLon.sortIgnoreCase();
            String2.log(impossibleNanLon.toNewlineString());

            String2.log("\n*** " + impossibleMinLon.size() + " stations had lon<" + minLon
                    + " and good pos quality flags (" + okQFCsv + ").");
            impossibleMinLon.sortIgnoreCase();
            String2.log(impossibleMinLon.toNewlineString());

            String2.log("\n*** " + impossibleMaxLon.size() + " stations had lon>" + maxLon
                    + " and good pos quality flags (" + okQFCsv + ").");
            impossibleMaxLon.sortIgnoreCase();
            String2.log(impossibleMaxLon.toNewlineString());

            //lat
            String2.log("\n*** " + impossibleNanLat.size() + " stations had invalid lat"
                    + " and good pos quality flags (" + okQFCsv + ").");
            impossibleNanLat.sortIgnoreCase();
            String2.log(impossibleNanLat.toNewlineString());

            String2.log("\n*** " + impossibleMinLat.size() + " stations had lat<" + minLat
                    + " and good pos quality flags (" + okQFCsv + ").");
            impossibleMinLat.sortIgnoreCase();
            String2.log(impossibleMinLat.toNewlineString());

            String2.log("\n*** " + impossibleMaxLat.size() + " stations had lat>" + maxLat
                    + " and good pos quality flags (" + okQFCsv + ").");
            impossibleMaxLat.sortIgnoreCase();
            String2.log(impossibleMaxLat.toNewlineString());

            //depth 
            String2.log("\n*** " + impossibleMinDepth.size() + " stations had depth<" + minDepth
                    + " and good depth quality flags (" + okQFCsv + ").");
            impossibleMinDepth.sortIgnoreCase();
            String2.log(impossibleMinDepth.toNewlineString());

            String2.log("\n*** " + impossibleMaxDepth.size() + " stations had depth>" + maxDepth
                    + " and good depth quality flags (" + okQFCsv + ").");
            impossibleMaxDepth.sortIgnoreCase();
            String2.log(impossibleMaxDepth.toNewlineString());

            //sa = impossibleLatLon.toArray();
            //Arrays.sort(sa);
            //String2.log("\n*** " + sa.length + " stations had impossible latitude or longitude values" +
            //    " and good q_pos quality flags.");
            //String2.log(String2.toNewlineString(sa));

            String2.log("\n*** " + impossibleMinTemperature.size() + " stations had temperature<"
                    + minTemperature + " and good temperature quality flags (" + okQFCsv + ").");
            impossibleMinTemperature.sortIgnoreCase();
            String2.log(impossibleMinTemperature.toNewlineString());

            String2.log("\n*** " + impossibleMaxTemperature.size() + " stations had temperature>"
                    + maxTemperature + " and good temperature quality flags (" + okQFCsv + ").");
            impossibleMaxTemperature.sortIgnoreCase();
            String2.log(impossibleMaxTemperature.toNewlineString());

            String2.log("\n*** " + impossibleMinSalinity.size() + " stations had salinity<" + minSalinity
                    + " and good salinity quality flags (" + okQFCsv + ").");
            impossibleMinSalinity.sortIgnoreCase();
            String2.log(impossibleMinSalinity.toNewlineString());

            String2.log("\n*** " + impossibleMaxSalinity.size() + " stations had salinity>" + maxSalinity
                    + " and good salinity quality flags (" + okQFCsv + ").");
            impossibleMaxSalinity.sortIgnoreCase();
            String2.log(impossibleMaxSalinity.toNewlineString());

        }

        //are we done?
        if (year == lastYear && month == lastMonth)
            break;

        //increment the month
        month++;
        if (month == 13) {
            year++;
            month = 1;
        }

    } //end of month/year loop

    String2.log("\n*** bobConsolidateGtspp completely finished " + firstYear + "-" + firstMonth + " through "
            + lastYear + "-" + lastMonth);

    String2.log("\n***" + "\ntotalNExceptions=    " + totalNExceptions + "        totalNWarnings=       "
            + totalNWarnings + "\ntotalNBadStation=    " + totalNBadStation + "        totalNGoodStation=    "
            + totalNGoodStation + "\ntotalNBadPos=        " + totalNBadPos + "        totalNGoodPos=        "
            + totalNGoodPos + "\ntotalNBadTime=       " + totalNBadTime + "        totalNGoodTime=       "
            + totalNGoodTime + "\ntotalNBadDepth=      " + totalNBadDepth + "        totalNGoodDepth=      "
            + totalNGoodDepth + "\ntotalNBadTemperature=" + totalNBadTemperature
            + "        totalNGoodTemperature=" + totalNGoodTemperature + "\ntotalNBadSalinity=   "
            + totalNBadSalinity + "        totalNGoodSalinity=   " + totalNGoodSalinity
            + "\ntotalNBadRows=       " + totalNBadRows + "        totalNGoodRows=       " + totalNGoodRows
            + "\nlogFile=F:/data/gtspp/log.txt" + "\n\n*** all finished time="
            + Calendar2.elapsedTimeString(System.currentTimeMillis() - elapsedTime));
    String2.returnLoggingToSystemOut();
}