Example usage for java.util Vector clear

List of usage examples for java.util Vector clear

Introduction

In this page you can find the example usage for java.util Vector clear.

Prototype

public void clear() 

Source Link

Document

Removes all of the elements from this Vector.

Usage

From source file:uk.ac.babraham.SeqMonk.ProbeGenerators.MacsPeakCaller.java

public void run() {

    //      for (int i=0;i<selectedChIPStores.length;i++) {
    //         System.err.println("Selcted ChIP="+selectedChIPStores[i]);
    //      }/*from   w ww .ja v  a2  s. com*/
    //      for (int i=0;i<selectedInputStores.length;i++) {
    //         System.err.println("Selcted Input="+selectedInputStores[i]);
    //      }

    // First find the tag offsets between the watson and crick strands

    // Work out the total average coverage for all of the combined ChIP samples
    long totalChIPCoverage = 0;

    for (int i = 0; i < selectedChIPStores.length; i++) {
        totalChIPCoverage += selectedChIPStores[i].getTotalReadLength();
    }

    if (cancel) {
        generationCancelled();
        return;
    }

    double averageChIPCoveragePerBase = totalChIPCoverage / (double) collection.genome().getTotalGenomeLength();

    double lowerCoverage = averageChIPCoveragePerBase * minFoldEnrichment;
    double upperCoverage = averageChIPCoveragePerBase * maxFoldEnrichment;

    System.err.println("Coverage range for high confidence peaks is " + lowerCoverage + " - " + upperCoverage);

    // Now we go through the data to find locations for our high confidence peaks so we can
    // randomly select 1000 of these to use to find the offset between the two strands

    Chromosome[] chromosomes = collection.genome().getAllChromosomes();

    Vector<Probe> potentialHighConfidencePeaks = new Vector<Probe>();

    for (int c = 0; c < chromosomes.length; c++) {

        if (cancel) {
            generationCancelled();
            return;
        }

        // Time for an update
        updateGenerationProgress("Finding high confidence peaks on chromosome " + chromosomes[c].name(), c,
                chromosomes.length);

        Probe lastValidProbe = null;

        for (int startPosition = 1; startPosition < chromosomes[c].length()
                - fragmentSize; startPosition += fragmentSize / 2) {

            // See if we need to quit
            if (cancel) {
                generationCancelled();
                return;
            }

            long totalLength = 0;
            Probe probe = new Probe(chromosomes[c], startPosition, startPosition + fragmentSize);

            for (int s = 0; s < selectedChIPStores.length; s++) {
                long[] reads = selectedChIPStores[s].getReadsForProbe(probe);
                for (int j = 0; j < reads.length; j++) {
                    totalLength += SequenceRead.length(reads[j]);
                }
            }

            if (totalLength >= (lowerCoverage * probe.length())
                    && totalLength <= upperCoverage * probe.length()) {

                // We have a potential high quality peak.

                // See if we can merge it with the last valid probe

                if (lastValidProbe != null
                        && SequenceRead.overlaps(lastValidProbe.packedPosition(), probe.packedPosition())) {

                    lastValidProbe = new Probe(chromosomes[c], lastValidProbe.start(), probe.end());

                } else if (lastValidProbe != null) {

                    // Check that the overall density over the region falls within our limits
                    totalLength = 0;
                    for (int s = 0; s < selectedChIPStores.length; s++) {
                        long[] reads = selectedChIPStores[s].getReadsForProbe(lastValidProbe);
                        for (int j = 0; j < reads.length; j++) {
                            totalLength += SequenceRead.length(reads[j]);
                        }
                    }

                    if (totalLength >= (lowerCoverage * lastValidProbe.length())
                            && totalLength <= upperCoverage * lastValidProbe.length()) {
                        potentialHighConfidencePeaks.add(lastValidProbe);
                    }

                    lastValidProbe = probe;
                } else {
                    lastValidProbe = probe;
                }
            }

        }

        if (lastValidProbe != null) {
            long totalLength = 0;
            for (int s = 0; s < selectedChIPStores.length; s++) {
                long[] reads = selectedChIPStores[s].getReadsForProbe(lastValidProbe);
                for (int j = 0; j < reads.length; j++) {
                    totalLength += SequenceRead.length(reads[j]);
                }
            }

            if (totalLength >= (lowerCoverage * lastValidProbe.length())
                    && totalLength <= upperCoverage * lastValidProbe.length()) {
                potentialHighConfidencePeaks.add(lastValidProbe);
            }
        }
    }

    if (potentialHighConfidencePeaks.size() == 0) {

        JOptionPane.showMessageDialog(SeqMonkApplication.getInstance(), "No high confidence peaks found",
                "Quitting generator", JOptionPane.INFORMATION_MESSAGE);
        generationCancelled();
        return;
    }

    //      System.err.println("Found "+potentialHighConfidencePeaks.size()+" high confidence peaks");

    // Now we select 1000 random probes from this set
    Probe[] highConfidencePeaks = potentialHighConfidencePeaks.toArray(new Probe[0]);

    Collections.shuffle(Arrays.asList(highConfidencePeaks));

    Probe[] randomHighConfidenceProbes = new Probe[Math.min(highConfidencePeaks.length, 1000)];

    for (int i = 0; i < randomHighConfidenceProbes.length; i++) {
        randomHighConfidenceProbes[i] = highConfidencePeaks[i];
    }

    // Now find the average distance between forward / reverse reads in the candidate peaks

    int[] distances = new int[highConfidencePeaks.length];

    // Sort the candidates so we don't do stupid stuff with the cache
    Arrays.sort(randomHighConfidenceProbes);

    for (int p = 0; p < randomHighConfidenceProbes.length; p++) {

        // See if we need to quit
        if (cancel) {
            generationCancelled();
            return;
        }

        distances[p] = getInterStrandDistance(randomHighConfidenceProbes[p], selectedChIPStores);
    }

    int medianInterStrandDistance = (int) SimpleStats.median(distances);

    if (medianInterStrandDistance < 0)
        medianInterStrandDistance = 0;

    //      System.err.println("Median inter strand difference = "+medianInterStrandDistance);

    // Now we find the depth cutoff for overrepresented single tags using a binomial distribution
    int totalReadCount = 0;
    for (int i = 0; i < selectedChIPStores.length; i++) {
        totalReadCount += selectedChIPStores[i].getTotalReadCount();
    }

    BinomialDistribution bin = new BinomialDistribution(totalReadCount,
            1d / collection.genome().getTotalGenomeLength());

    // We want to know what depth has a chance of less than 1^-5
    int redundantThreshold = bin.inverseCumulativeProbability(1 - 0.00001d);

    if (redundantThreshold < 1)
        redundantThreshold = 1;

    //      System.err.println("Redundancy threshold is "+redundantThreshold);

    // Now we construct a poisson distribution to work out the threshold to use for
    // constructing a full candidate peak set.

    updateGenerationProgress("Counting non-redundant reads", 0, 1);

    // To do this we need to get the full non-redundant length from the whole set
    int totalNonRedCount = getNonRedundantReadCount(selectedChIPStores, redundantThreshold);

    //      System.err.println("Total non-redundant sequences is "+totalNonRedCount);

    // We need to know the median read length for the data
    int readLength = 0;
    for (int i = 0; i < selectedChIPStores.length; i++) {
        readLength += selectedChIPStores[i].getTotalReadLength() / selectedChIPStores[i].getTotalReadCount();
    }
    readLength /= selectedChIPStores.length;

    double expectedCountsPerWindow = getExpectedCountPerWindow(totalNonRedCount,
            collection.genome().getTotalGenomeLength(), fragmentSize, readLength);

    PoissonDistribution poisson = new PoissonDistribution(expectedCountsPerWindow);

    int readCountCutoff = poisson.inverseCumulativeProbability(1 - pValue);

    //      System.err.println("Threshold for enrichment in a window is "+readCountCutoff+" reads using a p-value of "+pValue+" and a mean of "+(totalNonRedCount/(collection.genome().getTotalGenomeLength()/(double)fragmentSize)));

    // Now we go back through the whole dataset to do a search for all possible candidate probes 

    // We re-use the peak vector we came up with before.
    potentialHighConfidencePeaks.clear();

    for (int c = 0; c < chromosomes.length; c++) {
        // Time for an update
        updateGenerationProgress("Finding candidate peaks on chromosome " + chromosomes[c].name(), c,
                chromosomes.length);

        Probe lastValidProbe = null;

        for (int startPosition = 1; startPosition < chromosomes[c].length()
                - fragmentSize; startPosition += fragmentSize / 2) {

            // See if we need to quit
            if (cancel) {
                generationCancelled();
                return;
            }

            // We expand the region we're looking at by the inter-strand distance as we're going to
            // be adjusting the read positions
            Probe probe = new Probe(chromosomes[c], startPosition, (startPosition + fragmentSize - 1));

            long[] mergedProbeReads = getReadsFromDataStoreCollection(probe, selectedChIPStores,
                    medianInterStrandDistance);

            mergedProbeReads = deduplicateReads(mergedProbeReads, redundantThreshold);

            SequenceRead.sort(mergedProbeReads);

            int thisProbeOverlapCount = 0;
            for (int i = 0; i < mergedProbeReads.length; i++) {
                if (SequenceRead.overlaps(mergedProbeReads[i], probe.packedPosition())) {
                    ++thisProbeOverlapCount;
                }
            }

            if (thisProbeOverlapCount > readCountCutoff) {

                // We have a potential high quality peak.

                // See if we can merge it with the last valid probe

                if (lastValidProbe != null
                        && SequenceRead.overlaps(lastValidProbe.packedPosition(), probe.packedPosition())) {

                    lastValidProbe = new Probe(chromosomes[c], lastValidProbe.start(), probe.end());

                } else if (lastValidProbe != null) {
                    potentialHighConfidencePeaks.add(lastValidProbe);
                    lastValidProbe = probe;
                } else {
                    lastValidProbe = probe;
                }
            }

        }

        if (lastValidProbe != null) {
            potentialHighConfidencePeaks.add(lastValidProbe);
        }
    }

    // Finally we re-filter the peaks we have using local poisson distributions with densities taken
    // from either the input samples (if there are any), or the local region.  The densities are 
    // estimated over 1,5 and 10kb around the peak and genome wide and the max of these is taken.
    // If there is no input then the 1kb region is not used.

    Probe[] allCandidateProbes = potentialHighConfidencePeaks.toArray(new Probe[0]);

    // Work out which stores we're using to validate against.
    DataStore[] validationStores;
    boolean useInput = false;
    double inputCorrection = 1;

    int validationNonRedCount;

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

        // See if we need to quit
        if (cancel) {
            generationCancelled();
            return;
        }

        validationStores = selectedInputStores;
        useInput = true;

        // We also need to work out the total number of nonredundant seqences
        // in the input so we can work out a scaling factor so that the densities
        // for input and chip are comparable.
        validationNonRedCount = getNonRedundantReadCount(validationStores, redundantThreshold);

        inputCorrection = totalNonRedCount / (double) validationNonRedCount;

        System.err.println("From chip=" + totalNonRedCount + " input=" + validationNonRedCount
                + " correction is " + inputCorrection);

    } else {
        validationStores = selectedChIPStores;
        validationNonRedCount = totalNonRedCount;
    }

    Vector<Probe> finalValidatedProbes = new Vector<Probe>();

    for (int p = 0; p < allCandidateProbes.length; p++) {

        // See if we need to quit
        if (cancel) {
            generationCancelled();
            return;
        }

        if (p % 100 == 0) {
            updateGenerationProgress("Validated " + p + " out of " + allCandidateProbes.length + " raw peaks",
                    p, allCandidateProbes.length);
        }

        //         System.err.println("Validating "+allCandidateProbes[p].chromosome()+":"+allCandidateProbes[p].start()+"-"+allCandidateProbes[p].end());

        // We now need to find the maximum read density per 2*bandwidth against which
        // we're going to validate this peak

        // We're going to get all reads within 10kb of the peak, and then we can subselect from there

        int midPoint = allCandidateProbes[p].middle();

        Probe region10kb = new Probe(allCandidateProbes[p].chromosome(), Math.max(midPoint - 5000, 1),
                Math.min(midPoint + 4999, allCandidateProbes[p].chromosome().length()),
                allCandidateProbes[p].strand());
        Probe region5kb = new Probe(allCandidateProbes[p].chromosome(), Math.max(midPoint - 2500, 1),
                Math.min(midPoint + 2499, allCandidateProbes[p].chromosome().length()),
                allCandidateProbes[p].strand());
        Probe region1kb = new Probe(allCandidateProbes[p].chromosome(), Math.max(midPoint - 500, 1),
                Math.min(midPoint + 499, allCandidateProbes[p].chromosome().length()),
                allCandidateProbes[p].strand());

        // Get the probes for the largest region
        long[] thisRegionReads = getReadsFromDataStoreCollection(region10kb, validationStores, 0);

        // Deduplicate so it's a fair comparison
        thisRegionReads = deduplicateReads(thisRegionReads, redundantThreshold); // Should we recalculate the redundant threshold based on the input coverage?

        int region10kbcount = thisRegionReads.length;
        int region5kbcount = 0;
        int region1kbcount = 0;

        // Go through the reads seeing if they fit into the 5 or 1kb regions
        for (int r = 0; r < thisRegionReads.length; r++) {
            if (SequenceRead.overlaps(region5kb.packedPosition(), thisRegionReads[r]))
                ++region5kbcount;
            if (SequenceRead.overlaps(region1kb.packedPosition(), thisRegionReads[r]))
                ++region1kbcount;
        }

        //         System.err.println("Input counts 10kb="+region10kbcount+" 5kb="+region5kbcount+" 1kb="+region1kbcount);

        // Convert to densities per window and ajdust for global coverage

        double globalDensity = getExpectedCountPerWindow(validationNonRedCount,
                collection.genome().getTotalGenomeLength(), allCandidateProbes[p].length(), readLength)
                * inputCorrection;
        double density10kb = getExpectedCountPerWindow(region10kbcount, region10kb.length(),
                allCandidateProbes[p].length(), readLength) * inputCorrection;
        double density5kb = getExpectedCountPerWindow(region5kbcount, region5kb.length(),
                allCandidateProbes[p].length(), readLength) * inputCorrection;
        double density1kb = getExpectedCountPerWindow(region1kbcount, region1kb.length(),
                allCandidateProbes[p].length(), readLength) * inputCorrection;

        // Find the highest density to use for the validation
        double highestDensity = globalDensity;
        if (density10kb > highestDensity)
            highestDensity = density10kb;
        if (density5kb > highestDensity)
            highestDensity = density5kb;
        if (useInput && density1kb > highestDensity)
            highestDensity = density1kb;

        //         System.err.println("Global="+globalDensity+" 10kb="+density10kb+" 5kb="+density5kb+" 1kb="+density1kb+" using="+highestDensity);

        // Construct a poisson distribution with this density
        PoissonDistribution localPoisson = new PoissonDistribution(highestDensity);

        //         System.err.println("Cutoff from global="+(new PoissonDistribution(globalDensity)).inverseCumulativeProbability(1-pValue)+" 10kb="+(new PoissonDistribution(density10kb)).inverseCumulativeProbability(1-pValue)+" 5kb="+(new PoissonDistribution(density5kb)).inverseCumulativeProbability(1-pValue)+" 1kb="+(new PoissonDistribution(density1kb)).inverseCumulativeProbability(1-pValue));
        // Now check to see if the actual count from this peak is enough to still pass
        long[] mergedProbeReads = getReadsFromDataStoreCollection(allCandidateProbes[p], selectedChIPStores,
                medianInterStrandDistance);
        mergedProbeReads = deduplicateReads(mergedProbeReads, redundantThreshold);

        SequenceRead.sort(mergedProbeReads);

        int thisProbeOverlapCount = 0;
        for (int i = 0; i < mergedProbeReads.length; i++) {
            if (SequenceRead.overlaps(mergedProbeReads[i], allCandidateProbes[p].packedPosition())) {
                ++thisProbeOverlapCount;
            }
        }

        //         System.err.println("Read count in ChIP is "+thisProbeOverlapCount);

        if (thisProbeOverlapCount > localPoisson.inverseCumulativeProbability(1 - pValue)) {
            finalValidatedProbes.add(allCandidateProbes[p]);
            //            System.err.println("Adding probe to final set");
        }

    }

    //      System.err.println("From "+allCandidateProbes.length+" candidates "+finalValidatedProbes.size()+" peaks were validated");

    ProbeSet finalSet = new ProbeSet(getDescription(), finalValidatedProbes.toArray(new Probe[0]));

    generationComplete(finalSet);

}

From source file:sos.scheduler.cron.CronConverter.java

private void createRunTime(final Matcher pcronRegExMatcher, final Element runTimeElement) throws Exception {
    try {//from  w  w w  .  j ava 2 s .  co m
        if (!pcronRegExMatcher.matches()) {
            throw new JobSchedulerException("Fail to parse cron line \"" + strCronLine + "\", regexp is "
                    + pcronRegExMatcher.toString());
        }

        String minutes = pcronRegExMatcher.group(1);
        String hours = pcronRegExMatcher.group(2);
        String days = pcronRegExMatcher.group(3);
        String months = pcronRegExMatcher.group(4);
        String weekdays = pcronRegExMatcher.group(5);

        if (minutes.equals("@reboot")) {
            runTimeElement.setAttribute("once", "yes");
            return;
        }
        Vector<Element> childElements = new Vector<Element>();
        Element periodElement = runTimeElement.getOwnerDocument().createElement("period");

        logger.debug("processing hours [" + hours + "] and minutes [" + minutes + "]");
        if (minutes.startsWith("*")) {
            if (minutes.equalsIgnoreCase("*")) {
                // every minute
                periodElement.setAttribute("repeat", "60");
            } else { // repeat interval is given
                String repeat = minutes.substring(2);
                repeat = formatTwoDigits(repeat);
                periodElement.setAttribute("repeat", "00:" + repeat);
            }
            if (hours.startsWith("*")) {
                if (!hours.equalsIgnoreCase("*")) {
                    // repeat interval is given for hours and minutes. Doesn't make sense.
                    // e.g. */2 */3 every 3 hours repeat every 2 minutes
                    throw new JobSchedulerException(
                            "Combination of minutes and hours not supported: " + minutes + " " + hours);
                }
                // every hour: keep interval from minutes
                childElements.add(periodElement);
            } else {
                logger.debug("Found specific hours, creating periods with begin and end.");
                String[] hourArray = hours.split(",");
                for (int i = 0; i < hourArray.length; i++) {
                    String currentHour = hourArray[i];
                    if (currentHour.indexOf("/") != -1) {
                        String[] additionalHours = getArrayFromColumn(currentHour);
                        hourArray = combineArrays(hourArray, additionalHours);
                        continue;
                    }
                    String[] currentHourArray = currentHour.split("-");
                    Element currentPeriodElement = (Element) periodElement.cloneNode(true);
                    String beginHour = currentHourArray[0];

                    int iEndHour = (Integer.parseInt(beginHour) + 1) % 24;
                    // workaround, bis endhour am nchsten Tag erlaubt
                    if (iEndHour == 0)
                        iEndHour = 24;
                    String endHour = "" + iEndHour;
                    if (currentHourArray.length > 1)
                        endHour = currentHourArray[1];
                    beginHour = formatTwoDigits(beginHour);
                    endHour = formatTwoDigits(endHour);
                    currentPeriodElement.setAttribute("begin", beginHour + ":00");
                    currentPeriodElement.setAttribute("end", endHour + ":00");
                    childElements.add(currentPeriodElement);
                }
            }
        } // end if  minutes.startsWith("*")
        else { // one or more minutes are fixed
            String[] minutesArray = getArrayFromColumn(minutes);
            for (String element : minutesArray) {
                Element currentPeriodElement = (Element) periodElement.cloneNode(true);
                String currentMinute = element;

                currentMinute = formatTwoDigits(currentMinute);
                if (hours.startsWith("*")) {
                    currentPeriodElement.setAttribute("absolute_repeat", "01:00");
                    usedNewRunTime = true;
                    if (!hours.equalsIgnoreCase("*")) {// repeat interval is given for hours
                        String repeat = hours.substring(2);
                        repeat = formatTwoDigits(repeat);
                        currentPeriodElement.setAttribute("absolute_repeat", repeat + ":00");
                    }
                    currentPeriodElement.setAttribute("begin", "00:" + currentMinute);
                    childElements.add(currentPeriodElement);
                } else { //fixed hour(s) is set
                    String[] hourArray = hours.split(",");
                    for (String element2 : hourArray) {
                        currentPeriodElement = (Element) periodElement.cloneNode(true);
                        String currentHour = element2;
                        if (currentHour.indexOf("-") == -1) {
                            // fixed hour and fixed minute --> create single_start
                            currentHour = formatTwoDigits(currentHour);
                            currentPeriodElement.setAttribute("single_start",
                                    currentHour + ":" + currentMinute);
                        } else {
                            // range of hours is set, create begin and end attributes
                            String[] currentHourArray = currentHour.split("[-/]");
                            int beginHour = Integer.parseInt(currentHourArray[0]);
                            int endHour = Integer.parseInt(currentHourArray[1]);
                            int beginMinute = Integer.parseInt(currentMinute);
                            int endMinute = beginMinute + 1;
                            // workaround, bis endhour am nchsten Tag erlaubt
                            endMinute = beginMinute;
                            if (endMinute == 60) {
                                endMinute = 0;
                                endHour = endHour + 1;
                            }
                            endHour = endHour % 24;
                            // workaround, bis endhour am nchsten Tag erlaubt
                            if (endHour == 0)
                                endHour = 24;
                            String stepSize = "1";
                            if (currentHourArray.length == 3) {
                                stepSize = formatTwoDigits(currentHourArray[2]);
                            }
                            currentPeriodElement.setAttribute("absolute_repeat", stepSize + ":00");
                            usedNewRunTime = true;
                            currentPeriodElement.setAttribute("begin",
                                    formatTwoDigits(beginHour) + ":" + formatTwoDigits(beginMinute));
                            currentPeriodElement.setAttribute("end",
                                    formatTwoDigits(endHour) + ":" + formatTwoDigits(endMinute));
                        }
                        childElements.add(currentPeriodElement);
                    }
                }
            }

        }

        logger.debug("processing days [" + days + "]");
        boolean monthDaysSet = false;
        if (days.startsWith("*")) {
            if (days.equals("*")) {
                // every day - do nothing, just keep periods
            } else {
                // repeat interval is given for days
                // this is not possible in the JobScheduler but can be poorly emulated
                Element monthDaysElement = runTimeElement.getOwnerDocument().createElement("monthdays");
                String repeat = days.substring(2);
                int iRepeat = Integer.parseInt(repeat);
                // use only 30 days
                for (int i = 1; i <= 30; i = i + iRepeat) {
                    String day = "" + i;
                    addDay(day, monthDaysElement, childElements);
                }
                childElements.clear();
                childElements.add(monthDaysElement);
                monthDaysSet = true;
            }
        } else {
            Element monthDaysElement = runTimeElement.getOwnerDocument().createElement("monthdays");
            String[] daysArray = getArrayFromColumn(days);
            for (String day : daysArray) {
                addDay(day, monthDaysElement, childElements);
            }
            childElements.clear();
            childElements.add(monthDaysElement);
            monthDaysSet = true;
        }

        if (!weekdays.equals("*") && monthDaysSet) {
            logger.info("Weekdays will not be processed as days are already set in current line.");
        } else {
            logger.debug("processing weekdays [" + weekdays + "]");
            weekdays = replaceDayNames(weekdays);
            if (weekdays.startsWith("*/"))
                throw new JobSchedulerException("Repeat intervals for the weekdays column [" + weekdays
                        + "] are not supported. Please use the days column.");
            if (weekdays.equals("*")) {
                // all weekdays, do nothing
            } else {
                Element weekDaysElement = runTimeElement.getOwnerDocument().createElement("weekdays");
                String[] daysArray = getArrayFromColumn(weekdays);
                for (String day : daysArray) {
                    addDay(day, weekDaysElement, childElements);
                }
                childElements.clear();
                childElements.add(weekDaysElement);
            }
        }

        logger.debug("processing months [" + months + "]");
        if (months.startsWith("*")) {
            if (months.equals("*")) {
                // every month - do nothing
            } else {
                months = replaceMonthNames(months);
                // repeat interval is given for months
                // this is not possible in the JobScheduler but can be poorly emulated
                Vector<Element> newChildElements = new Vector<Element>();
                String repeat = months.substring(2);
                int iRepeat = Integer.parseInt(repeat);

                for (int i = 1; i <= 12; i = i + iRepeat) {
                    String month = "" + i;
                    Element monthElement = runTimeElement.getOwnerDocument().createElement("month");
                    usedNewRunTime = true;
                    monthElement.setAttribute("month", month);
                    Iterator<Element> iter = childElements.iterator();
                    while (iter.hasNext()) {
                        Element child = iter.next();
                        monthElement.appendChild(child.cloneNode(true));
                    }
                    newChildElements.add(monthElement);
                }
                childElements = newChildElements;
            }
        } else {// list of months is given
            Vector<Element> newChildElements = new Vector<Element>();
            String[] monthArray = getArrayFromColumn(months);
            for (String month : monthArray) {
                Element monthElement = runTimeElement.getOwnerDocument().createElement("month");
                usedNewRunTime = true;
                monthElement.setAttribute("month", month);
                Iterator<Element> iter = childElements.iterator();
                while (iter.hasNext()) {
                    Element child = iter.next();
                    monthElement.appendChild(child.cloneNode(true));
                }
                newChildElements.add(monthElement);
            }
            childElements = newChildElements;
        }

        // add topmost child elements to run_time element
        Iterator<Element> iter = childElements.iterator();
        while (iter.hasNext()) {
            Element someElement = iter.next();
            runTimeElement.appendChild(someElement);
        }
    } catch (Exception e) {
        throw new JobSchedulerException("Error creating run time: " + e, e);
    }

}

From source file:edu.ku.brc.specify.ui.db.ResultSetTableModel.java

@Override
//@SuppressWarnings("null")
public synchronized void exectionDone(final SQLExecutionProcessor process, final ResultSet resultSet) {
    if (statusBar != null) {
        statusBar.incrementValue(getClass().getSimpleName());
    }/*from w  ww  . j  a va2  s .c  o  m*/

    if (resultSet == null || results == null) {
        log.error("The " + (resultSet == null ? "resultSet" : "results") + " is null.");
        if (propertyListener != null) {
            propertyListener.propertyChange(new PropertyChangeEvent(this, "rowCount", null, 0));
        }
        return;
    }

    List<ERTICaptionInfo> captions = results.getVisibleCaptionInfo();

    // This can do one of two things:
    // 1) Take multiple columns and create an object and use a DataObjectFormatter to format the object.
    // 2) Table multiple objects that were derived from the columns and roll those up into a single column's value.
    //    This happens when you get back rows of info where part of the columns are duplicated because you really
    //    want those value to be put into a single column.
    //
    // Step One - Is to figure out what type of object needs to be created and what the Columns are 
    //            that need to be set into the object so the dataObjFormatter can do its job.
    //
    // Step Two - If the objects are being aggregated then the object created from the columns are added to a List
    //            and then last formatted as an "aggregation"

    try {
        if (resultSet.next()) {
            ResultSetMetaData metaData = resultSet.getMetaData();

            // Composite
            boolean hasCompositeObj = false;
            DataObjSwitchFormatter dataObjFormatter = null;
            UIFieldFormatterIFace formatter = null;
            Object compObj = null;

            // Aggregates
            ERTICaptionInfo aggCaption = null;
            ERTICaptionInfo compositeCaption = null;
            Vector<Object> aggList = null;
            DataObjectSettable aggSetter = null;
            Stack<Object> aggListRecycler = null;

            DataObjectSettable dataSetter = null; // data getter for Aggregate or the Subclass

            // Loop through the caption to figure out what columns will be displayed.
            // Watch for Captions with an Aggregator or Composite 
            numColumns = captions.size();
            for (ERTICaptionInfo caption : captions) {
                colNames.addElement(caption.getColLabel());

                int inx = caption.getPosIndex() + 1;
                //log.debug(metaData.getColumnClassName(inx));
                Class<?> cls = null;
                try {
                    cls = Class.forName(metaData.getColumnClassName(inx));
                    if (cls == Calendar.class || cls == java.sql.Date.class || cls == Date.class) {
                        cls = String.class;
                    }
                } catch (SQLException ex) {
                    cls = String.class;
                }
                classNames.addElement(cls);
                caption.setColClass(cls);

                if (caption.getAggregatorName() != null) {
                    //log.debug("The Agg is ["+caption.getAggregatorName()+"] "+caption.getColName());

                    // Alright we have an aggregator
                    aggList = new Vector<Object>();
                    aggListRecycler = new Stack<Object>();
                    aggCaption = caption;
                    aggSetter = DataObjectSettableFactory.get(aggCaption.getAggClass().getName(),
                            FormHelper.DATA_OBJ_SETTER);

                    // Now check to see if we are aggregating the this type of object or a child object of this object
                    // For example Collectors use an Agent as part of the aggregation
                    if (aggCaption.getSubClass() != null) {
                        dataSetter = DataObjectSettableFactory.get(aggCaption.getSubClass().getName(),
                                FormHelper.DATA_OBJ_SETTER);
                    } else {
                        dataSetter = aggSetter;
                    }

                } else if (caption.getColInfoList() != null) {
                    formatter = caption.getUiFieldFormatter();
                    if (formatter != null) {
                        compositeCaption = caption;
                    } else {
                        // OK, now aggregation but we will be rolling up multiple columns into a single object for formatting
                        // We need to get the formatter to see what the Class is of the object
                        hasCompositeObj = true;
                        aggCaption = caption;
                        dataObjFormatter = caption.getDataObjFormatter();
                        if (dataObjFormatter != null) {
                            if (dataObjFormatter.getDataClass() != null) {
                                aggSetter = DataObjectSettableFactory.get(
                                        dataObjFormatter.getDataClass().getName(),
                                        "edu.ku.brc.af.ui.forms.DataSetterForObj");
                            } else {
                                log.error("formatterObj.getDataClass() was null for " + caption.getColName());
                            }
                        } else {
                            log.error("DataObjFormatter was null for " + caption.getColName());
                        }
                    }

                }
                //colNames.addElement(metaData.getColumnName(i));
                //System.out.println("**************** " + caption.getColLabel()+ " "+inx+ " " + caption.getColClass().getSimpleName());
            }

            // aggCaption will be non-null for both a Aggregate AND a Composite
            if (aggCaption != null) {
                // Here we need to dynamically discover what the column indexes are that we to grab
                // in order to set them into the created data object
                for (ERTICaptionInfo.ColInfo colInfo : aggCaption.getColInfoList()) {
                    for (int i = 0; i < metaData.getColumnCount(); i++) {
                        String colName = StringUtils.substringAfterLast(colInfo.getColumnName(), ".");
                        if (colName.equalsIgnoreCase(metaData.getColumnName(i + 1))) {
                            colInfo.setPosition(i);
                            break;
                        }
                    }
                }

                // Now check to see if there is an Order Column because the Aggregator 
                // might need it for sorting the Aggregation
                String ordColName = aggCaption.getOrderCol();
                if (StringUtils.isNotEmpty(ordColName)) {
                    String colName = StringUtils.substringAfterLast(ordColName, ".");
                    //log.debug("colName ["+colName+"]");
                    for (int i = 0; i < metaData.getColumnCount(); i++) {
                        //log.debug("["+colName+"]["+metaData.getColumnName(i+1)+"]");
                        if (colName.equalsIgnoreCase(metaData.getColumnName(i + 1))) {
                            aggCaption.setOrderColIndex(i);
                            break;
                        }
                    }
                    if (aggCaption.getOrderColIndex() == -1) {
                        log.error("Agg Order Column Index wasn't found [" + ordColName + "]");
                    }
                }
            }

            if (ids == null) {
                ids = new Vector<Integer>();
            } else {
                ids.clear();
            }

            // Here is the tricky part.
            // When we are doing a Composite we are just taking multiple columns and 
            // essentially replace them with a single value from the DataObjFormatter
            //
            // But when doing an Aggregation we taking several rows and rolling them up into a single value.
            // so this code knows when it is doing an aggregation, so it knows to only add a new row to the display-able
            // results when primary id changes.

            DataObjFieldFormatMgr dataObjMgr = DataObjFieldFormatMgr.getInstance();
            Vector<Object> row = null;
            boolean firstTime = true;
            int prevId = Integer.MAX_VALUE; // really can't assume any value but will choose Max

            int numCols = resultSet.getMetaData().getColumnCount();
            do {
                int id = resultSet.getInt(1);
                //log.debug("id: "+id+"  prevId: "+prevId);

                // Remember aggCaption is used by both a Aggregation and a Composite
                if (aggCaption != null && !hasCompositeObj) {
                    if (firstTime) {
                        prevId = id;
                        row = new Vector<Object>();
                        firstTime = false;
                        cache.add(row);
                        ids.add(id);

                    } else if (id != prevId) {
                        //log.debug("Agg List len: "+aggList.size());

                        if (row != null && aggList != null) {
                            int aggInx = captions.indexOf(aggCaption);
                            row.remove(aggInx);
                            row.insertElementAt(dataObjMgr.aggregate(aggList, aggCaption.getAggClass()),
                                    aggInx);

                            if (aggListRecycler != null) {
                                aggListRecycler.addAll(aggList);
                            }
                            aggList.clear();

                            row = new Vector<Object>();
                            cache.add(row);
                            ids.add(id);
                        }
                        prevId = id;

                    } else if (row == null) {
                        row = new Vector<Object>();
                        cache.add(row);
                        ids.add(id);
                    }
                } else {
                    row = new Vector<Object>();
                    cache.add(row);
                    ids.add(id);
                }

                // Now for each Caption column get a value
                for (ERTICaptionInfo caption : captions) {
                    int posIndex = caption.getPosIndex();
                    if (caption == aggCaption) // Checks to see if we need to take multiple columns and make one column
                    {
                        if (hasCompositeObj) // just doing a Composite
                        {
                            if (aggSetter != null && row != null && dataObjFormatter != null) {
                                if (compObj == null) {
                                    compObj = aggCaption.getAggClass().newInstance();
                                }

                                for (ERTICaptionInfo.ColInfo colInfo : aggCaption.getColInfoList()) {
                                    setField(aggSetter, compObj, colInfo.getFieldName(),
                                            colInfo.getFieldClass(), resultSet, colInfo.getPosition());
                                }
                                row.add(DataObjFieldFormatMgr.getInstance().format(compObj,
                                        compObj.getClass()));

                            } else if (formatter != null) {
                                int len = compositeCaption.getColInfoList().size();
                                Object[] val = new Object[len];
                                int i = 0;
                                for (ERTICaptionInfo.ColInfo colInfo : compositeCaption.getColInfoList()) {
                                    int colInx = colInfo.getPosition() + posIndex + 1;
                                    if (colInx < numCols) {
                                        val[i++] = resultSet.getObject(colInx);
                                    } else {
                                        //val[i++] = resultSet.getObject(posIndex+1);
                                        val[i++] = "(Missing Data)";
                                    }
                                }
                                row.add(formatter.formatToUI(val));

                            } else {
                                log.error("Aggregator is null! [" + aggCaption.getAggregatorName()
                                        + "] or row or aggList");
                            }
                        } else if (aggSetter != null && row != null && aggList != null) // Doing an Aggregation
                        {
                            Object aggObj;
                            if (aggListRecycler.size() == 0) {
                                aggObj = aggCaption.getAggClass().newInstance();
                            } else {
                                aggObj = aggListRecycler.pop();
                            }
                            Object aggSubObj = aggCaption.getSubClass() != null
                                    ? aggCaption.getSubClass().newInstance()
                                    : null;
                            aggList.add(aggObj);

                            //@SuppressWarnings("unused")
                            //DataObjAggregator aggregator = DataObjFieldFormatMgr.getInstance().getAggregator(aggCaption.getAggregatorName());
                            //log.debug(" aggCaption.getOrderColIndex() "+ aggCaption.getOrderColIndex());

                            //aggSetter.setFieldValue(aggObj, aggregator.getOrderFieldName(), resultSet.getObject(aggCaption.getOrderColIndex() + 1));

                            Object dataObj;
                            if (aggSubObj != null) {
                                aggSetter.setFieldValue(aggObj, aggCaption.getSubClassFieldName(), aggSubObj);
                                dataObj = aggSubObj;
                            } else {
                                dataObj = aggObj;
                            }

                            for (ERTICaptionInfo.ColInfo colInfo : aggCaption.getColInfoList()) {
                                setField(dataSetter, dataObj, colInfo.getFieldName(), colInfo.getFieldClass(),
                                        resultSet, colInfo.getPosition());
                            }
                            row.add("PlaceHolder");

                        } else if (aggSetter == null || aggList == null) {
                            log.error("Aggregator is null! [" + aggCaption.getAggregatorName() + "] or aggList["
                                    + aggList + "]");
                        }

                    } else if (row != null) {
                        if (caption.getColName() == null && caption.getColInfoList().size() > 0) {
                            int len = caption.getColInfoList().size();
                            Object[] val = new Object[len];
                            for (int i = 0; i < caption.getColInfoList().size(); i++) {
                                int inx = posIndex + 1 + i;
                                val[i] = caption.processValue(resultSet.getObject(inx));
                            }
                            row.add(caption.getUiFieldFormatter().formatToUI(val));
                            //col += caption.getColInfoList().size() - 1;

                        } else {
                            Object obj = caption.processValue(resultSet.getObject(posIndex + 1));
                            row.add(obj);
                        }
                    }
                }

            } while (resultSet.next());

            // We were always setting the rolled up data when the ID changed
            // but on the last row we need to do it here manually (so to speak)
            if (aggCaption != null && aggList != null && aggList.size() > 0 && row != null) {
                int aggInx = captions.indexOf(aggCaption);
                row.remove(aggInx);
                String colStr;
                if (StringUtils.isNotEmpty(aggCaption.getAggregatorName())) {
                    colStr = DataObjFieldFormatMgr.getInstance().aggregate(aggList,
                            aggCaption.getAggregatorName());

                } else {
                    colStr = DataObjFieldFormatMgr.getInstance().aggregate(aggList, aggCaption.getAggClass());
                }
                row.insertElementAt(colStr, aggInx);
                aggList.clear();
                aggListRecycler.clear();
            }

            fireTableStructureChanged();
            fireTableDataChanged();
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    if (propertyListener != null) {
        propertyListener
                .propertyChange(new PropertyChangeEvent(this, "rowCount", null, new Integer(cache.size())));
    }
}

From source file:edu.ku.brc.specify.utilapps.BuildSampleDatabase.java

/**
 * Creates a single disciplineType collection.
 * @param disciplineName the name of the Discipline to use
 * @param disciplineName the disciplineType name
 * @return the entire list of DB object to be persisted
 *//*from ww  w .j a v  a 2  s .c o m*/
public List<Object> createHugeBotanyCollection(final DisciplineType disciplineType,
        final Institution institution, final SpecifyUser user, final CollectionChoice choice) {
    frame.setProcess(0, 16);
    frame.setDesc("Creating Botany...");

    createStep = 0;

    startTx();

    Division division = createDivision(institution, disciplineType.getName(), "Botany", "BT", "Botany");

    // create tree defs (later we will make the def items and nodes)
    TaxonTreeDef taxonTreeDef = createTaxonTreeDef("Taxon", TreeDefIface.FORWARD);
    GeographyTreeDef geoTreeDef = createGeographyTreeDef("Geography", TreeDefIface.REVERSE);
    GeologicTimePeriodTreeDef gtpTreeDef = createGeologicTimePeriodTreeDef("Chronos Stratigraphy",
            TreeDefIface.REVERSE);
    LithoStratTreeDef lithoStratTreeDef = createLithoStratTreeDef("LithoStrat");

    boolean buildStorageTree = false;
    if (stgTreeDef == null) {
        stgTreeDef = createStorageTreeDef("Storage", TreeDefIface.REVERSE);
        institution.setStorageTreeDef(stgTreeDef);
        buildStorageTree = true;
    }

    lithoStratTreeDef.setRemarks("A simple super, group, formation, member, bed Litho Stratigraphy tree");

    Discipline discipline = createDiscipline(division, disciplineType.getName(), disciplineType.getTitle(),
            dataType, taxonTreeDef, geoTreeDef, gtpTreeDef, lithoStratTreeDef);
    AppContextMgr.getInstance().setClassObject(Discipline.class, discipline);
    AppContextMgr.getInstance().setClassObject(Institution.class, institution);

    persist(institution);
    persist(division);
    persist(discipline);

    AppContextMgr.getInstance().setClassObject(Division.class, division);
    AppContextMgr.getInstance().setClassObject(Discipline.class, discipline);
    AppContextMgr.getInstance().setClassObject(Institution.class, institution);

    loadSchemaLocalization(discipline, SpLocaleContainer.CORE_SCHEMA, DBTableIdMgr.getInstance(),
            choice.getCatalogNumberingFmtName(), choice.getAccessionNumberingFmtName());

    ////////////////////////////////
    // Create the really high-level stuff
    ////////////////////////////////
    String title = initPrefs.getProperty("useragent.title", "mr");
    String firstName = initPrefs.getProperty("useragent.firstname", "Test");
    String lastName = initPrefs.getProperty("useragent.lastname", "User");
    String midInit = initPrefs.getProperty("useragent.midinit", "A");
    String abbrev = initPrefs.getProperty("useragent.abbrev", "tu");
    String email = initPrefs.getProperty("useragent.email", "testuser@ku.edu");
    String userType = initPrefs.getProperty("useragent.usertype", SpecifyUserTypes.UserType.Manager.toString());

    System.out.println("----- User Agent -----");
    System.out.println("Title:     " + title);
    System.out.println("FirstName: " + firstName);
    System.out.println("LastName:  " + lastName);
    System.out.println("MidInit:   " + midInit);
    System.out.println("Abbrev:    " + abbrev);
    System.out.println("Email:     " + email);
    System.out.println("UserType:  " + userType);

    Agent userAgent = createAgent(title, firstName, midInit, lastName, abbrev, email);

    discipline.addReference(userAgent, "agents");
    user.addReference(userAgent, "agents");

    persist(discipline);
    persist(userAgent);
    persist(user);

    frame.setProcess(++createStep);

    Pair<AutoNumberingScheme, AutoNumberingScheme> pairANS = createAutoNumberingSchemes(choice);
    AutoNumberingScheme cns = pairANS.first;
    AutoNumberingScheme accessionNS = pairANS.second;

    persist(cns);
    persist(accessionNS);

    commitTx();

    startTx();

    ////////////////////////////////
    // Create Collection
    ////////////////////////////////
    log.info("Creating a Collection");
    Collection collection = createCollection("KUBOT", "Botany", choice.getCatalogNumberingFmtName(), cns,
            discipline);
    persist(collection);

    AppContextMgr.getInstance().setClassObject(Collection.class, collection);

    division.addReference(accessionNS, "numberingSchemes");
    persist(division);

    commitTx();

    doShowHideTablesAndFields(null, discipline);
    doShowHideTablesAndFields(disciplineType.getName(), discipline);

    frame.setProcess(++createStep);

    startTx();

    //DBTableIdMgr schema = new DBTableIdMgr(false);
    //schema.initialize(new File(XMLHelper.getConfigDirPath("specify_datamodel.xml")));
    //loadSchemaLocalization(discipline, SpLocaleContainer, schema);
    //buildDarwinCoreSchema(discipline);

    AppContextMgr.getInstance().setClassObject(SpecifyUser.class, user);
    user.addReference(userAgent, "agents");

    persist(user);

    Journal journal = createJournalsAndReferenceWork();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // build the tree def items and nodes
    ////////////////////////////////
    List<Object> taxa = createSimpleBotanyTaxonTree(taxonTreeDef);
    List<Object> geos = createSimpleGeography(geoTreeDef, true);
    List<Object> gtps = createSimpleGeologicTimePeriod(gtpTreeDef, true);
    List<Object> lithoStrats = createSimpleLithoStrat(lithoStratTreeDef, true);

    persist(journal);
    persist(taxa);
    persist(geos);
    persist(buildStorageTree ? createSimpleStorage(stgTreeDef) : null);
    persist(gtps);
    persist(lithoStrats);
    commitTx();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // picklists
    ////////////////////////////////

    log.info("Creating picklists");
    frame.setDesc("Creating PickLists...");
    //frame.setProcess(++createStep);

    createPickLists(session, null);
    createPickLists(session, discipline);

    Vector<Object> dataObjects = new Vector<Object>();

    startTx();
    standardQueries(dataObjects, userAgent);
    persist(dataObjects);
    dataObjects.clear();

    //BldrPickList colMethods = createPickLists();

    persist(dataObjects);
    dataObjects.clear();

    commitTx();

    startTx();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // localities
    ////////////////////////////////
    String POINT = "Point";

    frame.setDesc("Creating localities");
    log.info("Creating localities");
    frame.setProcess(0, NUM_LOCALTIES);
    Vector<Locality> localities = new Vector<Locality>();
    Vector<Object> evictList = new Vector<Object>();
    for (int i = 0; i < NUM_LOCALTIES; i++) {
        Locality locality = createLocality("Unnamed forest stream pond", (Geography) geos.get(12));
        locality.setLatLongType(POINT);
        locality.setOriginalLatLongUnit(0);
        locality.setLat1text("38.925467 deg N");
        locality.setLatitude1(new BigDecimal(38.925467));
        locality.setLong1text("94.984867 deg W");
        locality.setLongitude1(new BigDecimal(-94.984867));
        persist(locality);

        evictList.add(locality);

        localities.add(locality);

        if ((i + 1) % 10 == 0) {
            commitTx();
            for (Object obj : evictList) {
                session.evict(obj);
            }
            evictList.clear();
            startTx();
            frame.setProcess(i);
        }
    }

    commitTx();

    startTx();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // agents and addresses
    ////////////////////////////////
    log.info("Creating agents and addresses");

    List<Agent> agents = new Vector<Agent>();

    lastName = userAgent.getLastName();
    Agent steveBoyd = createAgent("mr", "Steve", "D", "Boyd", "jb", "jb@net.edu");
    if (!lastName.equals("Cooper"))
        agents.add(createAgent("mr", "Peter", "D", "Cooper", "ds", "ds@whitehouse.gov"));
    if (!lastName.equals("Peck"))
        agents.add(createAgent("mr", "David", "H", "Peck", "rb", "beach@net.edu"));
    if (!lastName.equals("Appleton"))
        agents.add(createAgent("mrs", "Sally", "H", "Appleton", "jm", "jm@net.edu"));
    if (!lastName.equals("Brown"))
        agents.add(createAgent("mr", "Taylor", "C", "Brown", "kcs", "taylor.brown@ku.edu"));
    if (!lastName.equals("Boyd"))
        agents.add(steveBoyd);
    if (!lastName.equals("Thomas"))
        agents.add(createAgent("Mr", "James", "X", "Thomas", "dxt", ""));
    if (!lastName.equals("Peterson"))
        agents.add(createAgent("mr", "Pete", "A", "Peterson", "jb", ""));
    if (!lastName.equals("Guttenburg"))
        agents.add(createAgent("mr", "Mitch", "A", "Guttenburg", "jb", ""));
    if (!lastName.equals("Ford"))
        agents.add(createAgent("mr", "Daniel", "A", "Ford", "mas", "mas@ku.edu"));
    agents.add(userAgent);

    Agent ku = new Agent();
    ku.initialize();
    ku.setAbbreviation("KU");
    ku.setAgentType(Agent.ORG);
    ku.setLastName("University of Kansas");
    ku.setEmail("webadmin@ku.edu");
    ku.setTimestampCreated(new Timestamp(System.currentTimeMillis()));
    ku.setDivision(AppContextMgr.getInstance().getClassObject(Division.class));

    agents.add(ku);
    agents.get(0).setOrganization(ku);
    agents.get(1).setOrganization(ku);
    agents.get(2).setOrganization(ku);
    agents.get(3).setOrganization(ku);
    agents.get(8).setOrganization(ku);

    Agent otherAgent = new Agent();
    otherAgent.initialize();
    otherAgent.setAbbreviation("O");
    otherAgent.setAgentType(Agent.OTHER);
    otherAgent.setLastName("The Other Guys");
    otherAgent.setEmail("other@other.com");
    otherAgent.setTimestampCreated(new Timestamp(System.currentTimeMillis()));
    otherAgent.setDivision(AppContextMgr.getInstance().getClassObject(Division.class));

    agents.add(otherAgent);

    commitTx();

    List<GroupPerson> gpList = new ArrayList<GroupPerson>();
    if (true) {
        startTx();
        Agent gm1 = createAgent("mr", "John", "A", "Lyon", "jal", "jal@group.edu");
        Agent gm2 = createAgent("mr", "Dave", "D", "Jones", "ddj", "ddj@group.edu");
        persist(gm1);
        persist(gm2);
        commitTx();

        Agent groupAgent = new Agent();
        groupAgent.initialize();
        groupAgent.setAbbreviation("GRP");
        groupAgent.setAgentType(Agent.GROUP);
        groupAgent.setLastName("The Group");
        groupAgent.setEmail("group@group.com");
        groupAgent.setTimestampCreated(new Timestamp(System.currentTimeMillis()));
        groupAgent.setDivision(AppContextMgr.getInstance().getClassObject(Division.class));

        agents.add(groupAgent);

        gpList.add(createGroupPerson(groupAgent, gm1, 0, division));
        gpList.add(createGroupPerson(groupAgent, gm2, 1, division));
    }

    startTx();

    List<AgentVariant> agentVariants = new Vector<AgentVariant>();
    agentVariants.add(createAgentVariant(AgentVariant.VARIANT, "James Variant #1", steveBoyd));
    agentVariants.add(createAgentVariant(AgentVariant.VERNACULAR, "James VERNACULAR #1", steveBoyd));

    List<Address> addrs = new Vector<Address>();
    addrs.add(createAddress(agents.get(1), "1600 Pennsylvania Avenue NW", null, "Washington", "DC", "USA",
            "20500", 0));
    addrs.add(createAddress(agents.get(1), "??? Mississippi", null, "Lawrence", "KS", "USA", "66045", 1));
    addrs.add(createAddress(agents.get(2), "1 Main St", "", "Lenexa", "KS", "USA", "66071"));
    addrs.add(createAddress(agents.get(3), "13355 Inverness", "Bldg #3", "Lawrence", "KS", "USA", "66047"));
    addrs.add(createAddress(agents.get(4), "Natural History Museum", "Cromwell Rd", "London", null, "UK",
            "SW7 5BD"));
    addrs.add(createAddress(agents.get(6), "1212 Apple Street", null, "Chicago", "IL", "USA", "01010"));
    addrs.add(createAddress(agents.get(8), "11911 Oak Ln", null, "Orion", "KS", "USA", "66061"));
    addrs.add(createAddress(ku, null, null, "Lawrence", "KS", "USA", "66045"));

    // User Agent Address
    addrs.add(createAddress(userAgent, "1214 East Street", null, "Grinnell", "IA", "USA", "56060"));
    userAgent.setDivision(division);

    persist(agents);
    persist(agentVariants);
    persist(gpList);
    commitTx();

    startTx();
    ////////////////////////////////
    // Determination Status (Must be done here)
    ////////////////////////////////
    //        log.info("Creating determinations status");
    //        current    = createDeterminationStatus(discipline, "Current",    "", DeterminationStatus.CURRENT);
    //        currentAccepted    = createDeterminationStatus(discipline, "Current Accepted",    "", DeterminationStatus.CURRENTTOACCEPTED);
    //        notCurrent = createDeterminationStatus(discipline, "Not current","", DeterminationStatus.NOTCURRENT);
    //        incorrect  = createDeterminationStatus(discipline, "Incorrect",  "", DeterminationStatus.USERDEFINED);
    //        oldDet     = createDeterminationStatus(discipline, "Old Determination","", DeterminationStatus.OLDDETERMINATION);

    //        persist(current);
    //        persist(currentAccepted);
    //        persist(notCurrent);
    //        persist(incorrect);
    //        persist(oldDet);

    persist(dataObjects);
    dataObjects.clear();

    commitTx();

    startTx();

    ////////////////////////////////
    // collection objects
    ////////////////////////////////
    log.info("Creating collection objects");

    Calendar[] catDates = new Calendar[300];
    for (int i = 0; i < catDates.length; i++) {
        catDates[i] = Calendar.getInstance();
        int year = 1980 + (int) (rand.nextDouble() * 20.0);
        catDates[i].set(year, 01, 12 + i);
    }

    Vector<PrepType> prepTypesForSaving = loadPrepTypes(discipline.getType());
    dataObjects.addAll(prepTypesForSaving);

    persist(dataObjects);
    dataObjects.clear();

    frame.setProcess(++createStep);

    commitTx();

    PrepType pressed = prepTypesForSaving.get(0);

    startTx();

    frame.setProcess(0, NUM_COLOBJS);

    evictList.clear();

    frame.setDesc("Creating Collection Objects");
    int catNo = 100;
    for (int i = 0; i < NUM_COLOBJS; i++) {
        int years20 = (int) (rand.nextDouble() * 20.0);
        int years10 = (int) (rand.nextDouble() * 10.0);
        int years50 = (int) (rand.nextDouble() * 50.0);

        Calendar recent = Calendar.getInstance();
        recent.set(1950 + years20 + years10, 10, 27, 13, 44, 00);
        Calendar longAgo = Calendar.getInstance();
        longAgo.set(1900 + years50, 01, 29, 8, 12, 00);
        Calendar whileBack = Calendar.getInstance();
        whileBack.set(1960 + years20, 7, 4, 9, 33, 12);

        String catNumStr = String.format("%09d", catNo);

        int inx = (int) (rand.nextDouble() * localities.size());
        int agentInx = (int) (rand.nextDouble() * agents.size());
        int agentInx2 = (int) (rand.nextDouble() * agents.size());
        int calInx = (int) (rand.nextDouble() * catDates.length);

        CollectingEvent ce = createFakeCollectingEvent(agents, localities.get(inx), "cut");
        CollectionObject co = createCollectionObject(catNumStr, "RSC" + Integer.toString(catNo),
                agents.get(agentInx), collection, 1, ce, catDates[calInx], "BuildSampleDatabase");
        Determination dt = createDetermination(co, getRandomAgent(agents),
                getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent);

        Calendar prepDate = Calendar.getInstance();
        Preparation prep = createPreparation(pressed, agents.get(agentInx2), co, (Storage) locs.get(7),
                rand.nextInt(20) + 1, prepDate);

        persist(ce);
        persist(co);
        persist(dt);
        persist(prep);

        evictList.add(ce);
        evictList.add(co);
        evictList.add(dt);
        evictList.add(prep);

        if ((i + 1) % 10 == 0) {
            commitTx();
            for (Object obj : evictList) {
                session.evict(obj);
            }
            evictList.clear();
            startTx();

            frame.setProcess(i);
        }
        catNo++;
    }

    commitTx();

    frame.setProcess(++createStep);

    buildDarwinCoreSchema(discipline);

    // done
    log.info("Done creating Botany disciplineType database: " + disciplineType.getTitle());
    return dataObjects;
}

From source file:edu.ku.brc.specify.utilapps.BuildSampleDatabase.java

/**
 * Creates a single disciplineType collection.
 * @param disciplineName the name of the Discipline to use
 * @param disciplineName the disciplineType name
 * @return the entire list of DB object to be persisted
 *//*from  w  w w  .  j a  va 2 s .  co m*/
public List<Object> createSingleBotanyCollection(final DisciplineType disciplineType,
        final Institution institution, final SpecifyUser user, final CollectionChoice choice) {
    frame.setProcess(0, 16);
    frame.setDesc("Creating Botany...");

    createStep = 0;

    startTx();

    Division division = createDivision(institution, disciplineType.getName(), "Botany", "BT", "Botany");

    // create tree defs (later we will make the def items and nodes)
    TaxonTreeDef taxonTreeDef = createTaxonTreeDef("Taxon", TreeDefIface.FORWARD);
    GeographyTreeDef geoTreeDef = createGeographyTreeDef("Geography", TreeDefIface.REVERSE);
    GeologicTimePeriodTreeDef gtpTreeDef = createGeologicTimePeriodTreeDef("Chronos Stratigraphy",
            TreeDefIface.REVERSE);
    LithoStratTreeDef lithoStratTreeDef = createLithoStratTreeDef("LithoStrat");

    boolean buildStorageTree = false;
    if (stgTreeDef == null) {
        stgTreeDef = createStorageTreeDef("Storage", TreeDefIface.REVERSE);
        institution.setStorageTreeDef(stgTreeDef);
        buildStorageTree = true;
    }

    lithoStratTreeDef.setRemarks("A simple super, group, formation, member, bed Litho Stratigraphy tree");

    Discipline discipline = createDiscipline(division, disciplineType.getName(), disciplineType.getTitle(),
            dataType, taxonTreeDef, geoTreeDef, gtpTreeDef, lithoStratTreeDef);
    AppContextMgr.getInstance().setClassObject(Discipline.class, discipline);

    List<SpPrincipal> groups = new ArrayList<SpPrincipal>();

    persist(institution);
    persist(division);
    persist(discipline);

    AppContextMgr.getInstance().setClassObject(Division.class, division);
    AppContextMgr.getInstance().setClassObject(Discipline.class, discipline);
    AppContextMgr.getInstance().setClassObject(Institution.class, institution);

    loadSchemaLocalization(discipline, SpLocaleContainer.CORE_SCHEMA, DBTableIdMgr.getInstance(),
            choice.getCatalogNumberingFmtName(), choice.getAccessionNumberingFmtName());

    ////////////////////////////////
    // Create the really high-level stuff
    ////////////////////////////////
    String title = initPrefs.getProperty("useragent.title", "mr");
    String firstName = initPrefs.getProperty("useragent.firstname", "Test");
    String lastName = initPrefs.getProperty("useragent.lastname", "User");
    String midInit = initPrefs.getProperty("useragent.midinit", "A");
    String abbrev = initPrefs.getProperty("useragent.abbrev", "tu");
    String email = initPrefs.getProperty("useragent.email", "testuser@ku.edu");
    String userType = initPrefs.getProperty("useragent.usertype", SpecifyUserTypes.UserType.Manager.toString());
    //String           password         = initPrefs.getProperty("useragent.password", "rods");

    System.out.println("----- User Agent -----");
    System.out.println("Title:     " + title);
    System.out.println("FirstName: " + firstName);
    System.out.println("LastName:  " + lastName);
    System.out.println("MidInit:   " + midInit);
    System.out.println("Abbrev:    " + abbrev);
    System.out.println("Email:     " + email);
    System.out.println("UserType:  " + userType);

    Agent userAgent = createAgent(title, firstName, midInit, lastName, abbrev, email);
    discipline.addReference(userAgent, "agents");
    user.addReference(userAgent, "agents");

    persist(userAgent);
    persist(user);

    frame.setProcess(++createStep);

    Pair<AutoNumberingScheme, AutoNumberingScheme> pairANS = createAutoNumberingSchemes(choice);
    AutoNumberingScheme cns = pairANS.first;
    AutoNumberingScheme accessionNS = pairANS.second;

    persist(cns);
    persist(accessionNS);

    commitTx();

    startTx();

    ////////////////////////////////
    // Create Collection
    ////////////////////////////////
    log.info("Creating a Collection");
    Collection collection = createCollection("KUBOT", "Botany", choice.getCatalogNumberingFmtName(), cns,
            discipline);
    persist(collection);

    // create the standard user groups for this collection
    Map<String, SpPrincipal> groupMap = DataBuilder.createStandardGroups(session, collection);

    // add the administrator as a Collections Manager in this group
    user.addUserToSpPrincipalGroup(groupMap.get(SpecifyUserTypes.UserType.Manager.toString()));

    // Tester
    createAndAddTesterToCollection(session, "botanyuser", "botanyuser@ku.edu", "botanyuser", "mr", "Bob", "",
            "Botony", "", discipline, division, collection, groupMap, "Guest");

    persist(discipline);

    AppContextMgr.getInstance().setClassObject(Collection.class, collection);

    persist(groups);

    division.addReference(accessionNS, "numberingSchemes");
    persist(division);

    commitTx();

    doShowHideTablesAndFields(null, discipline);
    doShowHideTablesAndFields(disciplineType.getName(), discipline);

    frame.setProcess(++createStep);

    startTx();

    //DBTableIdMgr schema = new DBTableIdMgr(false);
    //schema.initialize(new File(XMLHelper.getConfigDirPath("specify_datamodel.xml")));
    //loadSchemaLocalization(discipline, SpLocaleContainer, schema);
    //buildDarwinCoreSchema(discipline);

    AppContextMgr.getInstance().setClassObject(SpecifyUser.class, user);
    user.addReference(userAgent, "agents");
    persist(user);

    Journal journal = createJournalsAndReferenceWork();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // build the tree def items and nodes
    ////////////////////////////////
    List<Object> taxa = createSimpleBotanyTaxonTree(taxonTreeDef);
    List<Object> geos = createSimpleGeography(geoTreeDef, true);
    List<Object> gtps = createSimpleGeologicTimePeriod(gtpTreeDef, true);
    List<Object> lithoStrats = createSimpleLithoStrat(lithoStratTreeDef, true);

    institution.setStorageTreeDef(stgTreeDef);

    persist(institution);
    persist(journal);
    persist(taxa);
    persist(geos);
    persist(buildStorageTree ? createSimpleStorage(stgTreeDef) : null);
    persist(gtps);
    persist(lithoStrats);
    commitTx();

    AppContextMgr.getInstance().setClassObject(Division.class, division);
    AppContextMgr.getInstance().setClassObject(Discipline.class, discipline);
    AppContextMgr.getInstance().setClassObject(Institution.class, institution);

    frame.setProcess(++createStep);

    ////////////////////////////////
    // picklists
    ////////////////////////////////

    log.info("Creating picklists");
    frame.setDesc("Creating PickLists...");
    //frame.setProcess(++createStep);

    createPickLists(session, null);
    createPickLists(session, discipline);

    Vector<Object> dataObjects = new Vector<Object>();

    startTx();
    standardQueries(dataObjects, userAgent);
    persist(dataObjects);
    dataObjects.clear();

    //BldrPickList colMethods = createPickLists();

    persist(dataObjects);
    dataObjects.clear();

    commitTx();

    startTx();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // localities
    ////////////////////////////////
    String POINT = "Point";
    String LINE = "Line";
    String RECT = "Rectangle";

    log.info("Creating localities");
    Locality forestStream = createLocality("Unnamed forest stream pond", (Geography) geos.get(12));
    forestStream.setLatLongType(POINT);
    forestStream.setOriginalLatLongUnit(0);
    forestStream.setLat1text("38.925467 deg N");
    forestStream.setLatitude1(new BigDecimal(38.925467));
    forestStream.setLong1text("94.984867 deg W");
    forestStream.setLongitude1(new BigDecimal(-94.984867));

    Locality lake = createLocality("Deep, dark lake pond", (Geography) geos.get(17));
    lake.setLatLongType(RECT);
    lake.setOriginalLatLongUnit(1);
    lake.setLat1text("41.548842 deg N");
    lake.setLatitude1(new BigDecimal(41.548842));
    lake.setLong1text("93.732129 deg W");
    lake.setLongitude1(new BigDecimal(-93.732129));

    lake.setLat2text("41.642195 deg N");
    lake.setLatitude2(new BigDecimal(41.642195));
    lake.setLong2text("100.403180 deg W");
    lake.setLongitude2(new BigDecimal(-100.403180));

    Locality farmpond = createLocality("Shoal Creek at Schermerhorn Park, S of Galena at Rt. 26",
            (Geography) geos.get(11));
    farmpond.setLatLongType(LINE);
    farmpond.setOriginalLatLongUnit(2);
    farmpond.setLat1text("41.642187 deg N");
    farmpond.setLatitude1(new BigDecimal(41.642187));
    farmpond.setLong1text("100.403163 deg W");
    farmpond.setLongitude1(new BigDecimal(-100.403163));

    farmpond.setLat2text("49.647435 deg N");
    farmpond.setLatitude2(new BigDecimal(49.647435));
    farmpond.setLong2text("-55.112163 deg W");
    farmpond.setLongitude2(new BigDecimal(-55.112163));

    persist(forestStream);
    persist(lake);
    persist(farmpond);

    commitTx();

    startTx();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // agents and addresses
    ////////////////////////////////
    log.info("Creating agents and addresses");

    List<Agent> agents = new Vector<Agent>();

    lastName = userAgent.getLastName();
    Agent steveBoyd = createAgent("mr", "Steve", "D", "Boyd", "jb", "jb@net.edu");
    if (!lastName.equals("Cooper"))
        agents.add(createAgent("mr", "Peter", "D", "Cooper", "ds", "ds@whitehouse.gov"));
    if (!lastName.equals("Peck"))
        agents.add(createAgent("mr", "David", "H", "Peck", "rb", "beach@net.edu"));
    if (!lastName.equals("Appleton"))
        agents.add(createAgent("mrs", "Sally", "H", "Appleton", "jm", "jm@net.edu"));
    if (!lastName.equals("Brown"))
        agents.add(createAgent("mr", "Taylor", "C", "Brown", "kcs", "taylor.brown@ku.edu"));
    if (!lastName.equals("Boyd"))
        agents.add(steveBoyd);
    if (!lastName.equals("Thomas"))
        agents.add(createAgent("Mr", "James", "X", "Thomas", "dxt", ""));
    if (!lastName.equals("Peterson"))
        agents.add(createAgent("mr", "Pete", "A", "Peterson", "jb", ""));
    if (!lastName.equals("Guttenburg"))
        agents.add(createAgent("mr", "Mitch", "A", "Guttenburg", "jb", ""));
    if (!lastName.equals("Ford"))
        agents.add(createAgent("mr", "Daniel", "A", "Ford", "mas", "mas@ku.edu"));
    agents.add(userAgent);

    Agent ku = new Agent();
    ku.initialize();
    ku.setAbbreviation("KU");
    ku.setAgentType(Agent.ORG);
    ku.setLastName("University of Kansas");
    ku.setEmail("webadmin@ku.edu");
    ku.setTimestampCreated(new Timestamp(System.currentTimeMillis()));
    ku.setDivision(AppContextMgr.getInstance().getClassObject(Division.class));

    agents.add(ku);
    agents.get(0).setOrganization(ku);
    agents.get(1).setOrganization(ku);
    agents.get(2).setOrganization(ku);
    agents.get(3).setOrganization(ku);
    agents.get(8).setOrganization(ku);

    Agent otherAgent = new Agent();
    otherAgent.initialize();
    otherAgent.setAbbreviation("O");
    otherAgent.setAgentType(Agent.OTHER);
    otherAgent.setLastName("The Other Guys");
    otherAgent.setEmail("other@other.com");
    otherAgent.setTimestampCreated(new Timestamp(System.currentTimeMillis()));
    otherAgent.setDivision(AppContextMgr.getInstance().getClassObject(Division.class));

    agents.add(otherAgent);

    commitTx();

    List<GroupPerson> gpList = new ArrayList<GroupPerson>();
    if (true) {
        startTx();
        Agent gm1 = createAgent("mr", "John", "A", "Lyon", "jal", "jal@group.edu");
        Agent gm2 = createAgent("mr", "Dave", "D", "Jones", "ddj", "ddj@group.edu");
        persist(gm1);
        persist(gm2);
        commitTx();

        Agent groupAgent = new Agent();
        groupAgent.initialize();
        groupAgent.setAbbreviation("GRP");
        groupAgent.setAgentType(Agent.GROUP);
        groupAgent.setLastName("The Group");
        groupAgent.setEmail("group@group.com");
        groupAgent.setTimestampCreated(new Timestamp(System.currentTimeMillis()));
        groupAgent.setDivision(AppContextMgr.getInstance().getClassObject(Division.class));

        agents.add(groupAgent);

        gpList.add(createGroupPerson(groupAgent, gm1, 0, division));
        gpList.add(createGroupPerson(groupAgent, gm2, 1, division));
    }

    startTx();

    List<AgentVariant> agentVariants = new Vector<AgentVariant>();
    agentVariants.add(createAgentVariant(AgentVariant.VARIANT, "James Variant #1", steveBoyd));
    agentVariants.add(createAgentVariant(AgentVariant.VERNACULAR, "James VERNACULAR #1", steveBoyd));

    List<Address> addrs = new Vector<Address>();
    addrs.add(createAddress(agents.get(1), "1600 Pennsylvania Avenue NW", null, "Washington", "DC", "USA",
            "20500", 0));
    addrs.add(createAddress(agents.get(1), "??? Mississippi", null, "Lawrence", "KS", "USA", "66045", 1));
    addrs.add(createAddress(agents.get(2), "1 Main St", "", "Lenexa", "KS", "USA", "66071"));
    addrs.add(createAddress(agents.get(3), "13355 Inverness", "Bldg #3", "Lawrence", "KS", "USA", "66047"));
    addrs.add(createAddress(agents.get(4), "Natural History Museum", "Cromwell Rd", "London", null, "UK",
            "SW7 5BD"));
    addrs.add(createAddress(agents.get(6), "1212 Apple Street", null, "Chicago", "IL", "USA", "01010"));
    addrs.add(createAddress(agents.get(8), "11911 Oak Ln", null, "Orion", "KS", "USA", "66061"));
    addrs.add(createAddress(ku, null, null, "Lawrence", "KS", "USA", "66045"));

    // User Agent Address
    addrs.add(createAddress(userAgent, "1214 East Street", null, "Grinnell", "IA", "USA", "56060"));
    userAgent.setDivision(division);

    persist(agents);
    persist(agentVariants);
    persist(gpList);
    commitTx();

    startTx();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // collecting events (collectors, collecting trip)
    ////////////////////////////////
    log.info("Creating collecting events, collectors and a collecting trip");
    Collector collectorMitch = createCollector(agents.get(7), 2);
    Collector collectorJim = createCollector(agents.get(2), 1);

    calendar.set(1994, 4, 21, 11, 56, 00);
    String stationFieldNumber = String.format(STATION_FIELD_FORMAT, stationFieldNumberCounter++);
    CollectingEvent ce1 = createCollectingEvent(forestStream, calendar, stationFieldNumber,
            new Collector[] { collectorMitch, collectorJim });
    ce1.setStartDateVerbatim("21 Apr 1994, 11:56 AM");
    calendar.set(1994, 4, 21, 13, 03, 00);
    ce1.setEndDate(calendar);
    ce1.setEndDateVerbatim("21 Apr 1994, 1:03 PM");
    ce1.setMethod("Picked");

    AttributeDef cevAttrDef = createAttributeDef(AttributeIFace.FieldType.StringType, "ParkName", discipline,
            null);//meg added cod

    persist(cevAttrDef);
    commitTx();

    startTx();

    CollectingEventAttr cevAttr = createCollectingEventAttr(ce1, cevAttrDef, "Sleepy Hollow", null);

    Collector collectorMeg = createCollector(agents.get(2), 1);
    Collector collectorRod = createCollector(agents.get(3), 2);
    stationFieldNumber = String.format(STATION_FIELD_FORMAT, stationFieldNumberCounter++);
    calendar.set(1994, 4, 22, 06, 12, 00);
    CollectingEvent ce2 = createCollectingEvent(farmpond, calendar, stationFieldNumber,
            new Collector[] { collectorMeg, collectorRod });
    ce2.setStartDateVerbatim("22 Apr 1994, 6:12 AM");
    calendar.set(1994, 4, 22, 07, 31, 00);
    ce2.setEndDate(calendar);
    ce2.setEndDateVerbatim("22 Apr 1994, 7:31 AM");
    ce2.setMethod("Picked");

    //CollectingTrip trip = createCollectingTrip("Sample collecting trip", new CollectingEvent[]{ce1,ce2});

    //dataObjects.add(trip);
    dataObjects.add(ce1);
    dataObjects.add(cevAttr);
    dataObjects.add(ce2);
    dataObjects.add(collectorMitch);
    dataObjects.add(collectorJim);
    dataObjects.add(collectorMeg);
    dataObjects.add(collectorRod);

    persist(dataObjects);
    dataObjects.clear();

    commitTx();

    startTx();
    ////////////////////////////////
    // permit
    ////////////////////////////////
    log.info("Creating a permit");
    Calendar issuedDate = Calendar.getInstance();
    issuedDate.set(1993, 1, 12);
    Calendar startDate = Calendar.getInstance();
    startDate.set(1993, 2, 1);
    Calendar endDate = Calendar.getInstance();
    endDate.set(1993, 5, 30);
    Permit permit = createPermit("1991-PLAN-0001", "US Dept Wildlife", issuedDate, startDate, endDate, null);
    permit.setIssuedTo(ku);
    permit.setIssuedBy(agents.get(4));
    dataObjects.add(permit);

    log.info("Creating a repository agreement");
    RepositoryAgreement repoAg = new RepositoryAgreement();
    repoAg.initialize();
    repoAg.setDivision(division);
    repoAg.setRepositoryAgreementNumber("KU-1990-01");
    repoAg.setOriginator(ku);
    Calendar received = Calendar.getInstance();
    received.set(1992, 2, 10);
    repoAg.setDateReceived(received);
    Calendar repoEndDate = Calendar.getInstance();
    received.set(2010, 2, 9);
    repoAg.setEndDate(repoEndDate);
    dataObjects.add(repoAg);

    persist(dataObjects);
    dataObjects.clear();
    commitTx();

    startTx();
    frame.setProcess(++createStep);

    ////////////////////////////////
    // collection objects
    ////////////////////////////////
    log.info("Creating collection objects");

    List<CollectionObject> collObjs = new Vector<CollectionObject>();
    Collection col = collection;

    Calendar[] catDates = new Calendar[8];
    for (int i = 0; i < catDates.length; i++) {
        catDates[i] = Calendar.getInstance();
        int year = 1980 + (int) (rand.nextDouble() * 20.0);
        catDates[i].set(year, 01, 12 + i);
    }

    String prefix = "000000";
    int catNo = 100;
    CollectingEvent[] colEves = new CollectingEvent[8];
    for (int i = 0; i < colEves.length; i++) {
        colEves[i] = createFakeCollectingEvent(agents, farmpond, "cut");
        collObjs.add(createCollectionObject(prefix + Integer.toString(catNo), "RSC" + Integer.toString(catNo),
                agents.get(i), col, 1, colEves[i], catDates[i], "BuildSampleDatabase"));
        catNo++;
    }
    dataObjects.addAll(collObjs);

    for (CollectingEvent ce : colEves) {
        persist(ce);
    }

    persist(dataObjects);
    dataObjects.clear();

    commitTx();

    startTx();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // determinations (determination status)
    ////////////////////////////////
    log.info("Creating determinations");

    List<Determination> determs = new Vector<Determination>();
    Calendar recent = Calendar.getInstance();
    recent.set(2005, 10, 27, 13, 44, 00);
    Calendar longAgo = Calendar.getInstance();
    longAgo.set(1976, 01, 29, 8, 12, 00);
    Calendar whileBack = Calendar.getInstance();
    whileBack.set(2000, 7, 4, 9, 33, 12);

    determs.add(createDetermination(collObjs.get(0), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(1), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(2), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(3), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(4), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(5), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(6), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(7), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));

    determs.add(createDetermination(collObjs.get(0), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, longAgo));
    determs.add(createDetermination(collObjs.get(1), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, whileBack));
    determs.add(createDetermination(collObjs.get(2), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, whileBack));
    determs.add(createDetermination(collObjs.get(3), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, whileBack));
    determs.add(createDetermination(collObjs.get(4), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, whileBack));
    determs.add(createDetermination(collObjs.get(4), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, longAgo));
    determs.add(createDetermination(collObjs.get(4), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, longAgo));
    determs.get(13).setRemarks("This determination is totally wrong.  What a foolish determination.");

    persist(determs);
    dataObjects.clear();
    commitTx();

    startTx();
    frame.setProcess(++createStep);

    ////////////////////////////////
    // preparations (prep types)
    ////////////////////////////////
    log.info("Creating preparations");

    Vector<PrepType> prepTypesForSaving = loadPrepTypes(discipline.getType());
    PrepType pressed = prepTypesForSaving.get(0);

    List<Preparation> preps = new Vector<Preparation>();
    Calendar prepDate = Calendar.getInstance();
    preps.add(createPreparation(pressed, agents.get(0), collObjs.get(0), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(0), collObjs.get(1), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(2), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(3), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(2), collObjs.get(4), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(2), collObjs.get(5), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(3), collObjs.get(6), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(3), collObjs.get(7), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(0), (Storage) locs.get(11),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(1), (Storage) locs.get(11),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(2), (Storage) locs.get(10),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(2), collObjs.get(3), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(3), collObjs.get(4), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(0), collObjs.get(5), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(6), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(7), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(2), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));

    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(0), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(1), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(2), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(3), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(4), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));

    dataObjects.addAll(prepTypesForSaving);
    dataObjects.addAll(preps);

    persist(dataObjects);
    dataObjects.clear();

    commitTx();

    startTx();
    frame.setProcess(++createStep);

    ////////////////////////////////
    // accessions (accession agents)
    ////////////////////////////////
    log.info("Creating accessions and accession agents");
    calendar.set(2006, 10, 27, 23, 59, 59);
    Accession acc1 = createAccession(division, "gift", "complete", "2000-PL-001",
            DateFormat.getInstance().format(calendar.getTime()), calendar, calendar);
    acc1.setText1("Ichthyology");
    acc1.setRepositoryAgreement(repoAg);

    Agent donor = agents.get(4);
    Agent receiver = agents.get(1);
    Agent reviewer = agents.get(2);

    List<AccessionAgent> accAgents = new Vector<AccessionAgent>();

    accAgents.add(createAccessionAgent("donor", donor, acc1, null));
    accAgents.add(createAccessionAgent("receiver", receiver, acc1, null));
    accAgents.add(createAccessionAgent("reviewer", reviewer, acc1, null));

    Accession acc2 = createAccession(division, "field_work", "inprocess", "2004-PL-002",
            DateFormat.getInstance().format(calendar.getTime()), calendar, calendar);

    Agent donor2 = agents.get(5);
    Agent receiver2 = agents.get(3);
    Agent reviewer2 = agents.get(1);

    accAgents.add(createAccessionAgent("donor", donor2, acc2, null));
    accAgents.add(createAccessionAgent("receiver", receiver2, acc2, null));
    accAgents.add(createAccessionAgent("reviewer", reviewer2, acc2, null));

    dataObjects.add(acc1);
    dataObjects.add(acc2);
    dataObjects.addAll(accAgents);

    persist(dataObjects);
    dataObjects.clear();
    commitTx();

    startTx();
    frame.setProcess(++createStep);

    createLoanExamples(preps, agents, dataObjects);
    frame.setProcess(++createStep);

    persist(dataObjects);
    dataObjects.clear();

    persist(dataObjects);
    dataObjects.clear();

    frame.setProcess(++createStep);

    commitTx();

    frame.setProcess(++createStep);

    buildDarwinCoreSchema(discipline);

    // done
    log.info("Done creating Botany disciplineType database: " + disciplineType.getTitle());
    return dataObjects;
}

From source file:edu.ku.brc.specify.utilapps.BuildSampleDatabase.java

/**
 * Creates a single disciplineType collection.
 * @param disciplineName the name of the Discipline to use
 * @param disciplineName the disciplineType name
 * @return the entire list of DB object to be persisted
 *//*  w w w  .  ja va2  s .c  o  m*/
public List<Object> createSingleInvertPaleoCollection(final DisciplineType disciplineType,
        final Institution institution, final SpecifyUser user, final CollectionChoice choice) {
    frame.setProcess(0, 16);
    frame.setDesc("Creating " + disciplineType.getTitle() + "...");

    createStep = 0;

    startTx();

    Division division = createDivision(institution, disciplineType.getName(), disciplineType.getTitle(), "INVP",
            disciplineType.getTitle());

    // create tree defs (later we will make the def items and nodes)
    TaxonTreeDef taxonTreeDef = createTaxonTreeDef("Taxon", TreeDefIface.FORWARD);
    GeographyTreeDef geoTreeDef = createGeographyTreeDef("Geography", TreeDefIface.REVERSE);
    GeologicTimePeriodTreeDef gtpTreeDef = createGeologicTimePeriodTreeDef("Chronos Stratigraphy",
            TreeDefIface.REVERSE);
    LithoStratTreeDef lithoStratTreeDef = createLithoStratTreeDef("LithoStrat");

    boolean buildStorageTree = false;
    if (stgTreeDef == null) {
        stgTreeDef = createStorageTreeDef("Storage", TreeDefIface.REVERSE);
        institution.setStorageTreeDef(stgTreeDef);
        buildStorageTree = true;
    }

    lithoStratTreeDef.setRemarks("A simple super, group, formation, member, bed Litho Stratigraphy tree");

    Discipline discipline = createDiscipline(division, disciplineType.getName(), disciplineType.getTitle(),
            dataType, taxonTreeDef, geoTreeDef, gtpTreeDef, lithoStratTreeDef);
    AppContextMgr.getInstance().setClassObject(Discipline.class, discipline);
    AppContextMgr.getInstance().setClassObject(Institution.class, institution);

    persist(institution);
    persist(division);
    persist(discipline);

    AppContextMgr.getInstance().setClassObject(Division.class, division);
    AppContextMgr.getInstance().setClassObject(Discipline.class, discipline);
    AppContextMgr.getInstance().setClassObject(Institution.class, institution);

    loadSchemaLocalization(discipline, SpLocaleContainer.CORE_SCHEMA, DBTableIdMgr.getInstance(),
            choice.getCatalogNumberingFmtName(), choice.getAccessionNumberingFmtName());

    ////////////////////////////////
    // Create the really high-level stuff
    ////////////////////////////////
    String title = initPrefs.getProperty("useragent.title", "mr");
    String firstName = initPrefs.getProperty("useragent.firstname", "Test");
    String lastName = initPrefs.getProperty("useragent.lastname", "User");
    String midInit = initPrefs.getProperty("useragent.midinit", "A");
    String abbrev = initPrefs.getProperty("useragent.abbrev", "tu");
    String email = initPrefs.getProperty("useragent.email", "testuser@ku.edu");
    String userType = initPrefs.getProperty("useragent.usertype", SpecifyUserTypes.UserType.Manager.toString());

    System.out.println("----- User Agent -----");
    System.out.println("Title:     " + title);
    System.out.println("FirstName: " + firstName);
    System.out.println("LastName:  " + lastName);
    System.out.println("MidInit:   " + midInit);
    System.out.println("Abbrev:    " + abbrev);
    System.out.println("Email:     " + email);
    System.out.println("UserType:  " + userType);

    Agent userAgent = createAgent(title, firstName, midInit, lastName, abbrev, email);
    discipline.addReference(userAgent, "agents");
    user.addReference(userAgent, "agents");

    persist(discipline);
    persist(userAgent);
    persist(user);

    //        LithoStratTreeDefItem earth     = createLithoStratTreeDefItem(lithoStratTreeDef, "Earth", 0, false);
    //        LithoStratTreeDefItem superGrp  = createLithoStratTreeDefItem(earth,     "Super Group", 100, false);
    //        LithoStratTreeDefItem lithoGrp  = createLithoStratTreeDefItem(superGrp,  "Litho Group", 200, false);
    //        LithoStratTreeDefItem formation = createLithoStratTreeDefItem(lithoGrp,  "Formation",   300, false);
    //        LithoStratTreeDefItem member    = createLithoStratTreeDefItem(formation, "Member",      400, false);
    //        @SuppressWarnings("unused")
    //        LithoStratTreeDefItem bed       = createLithoStratTreeDefItem(member,    "Bed",         500, true);

    frame.setProcess(++createStep);

    Pair<AutoNumberingScheme, AutoNumberingScheme> pairANS = createAutoNumberingSchemes(choice);
    AutoNumberingScheme cns = pairANS.first;
    AutoNumberingScheme accessionNS = pairANS.second;

    persist(cns);
    persist(accessionNS);
    //persist(earth);

    commitTx();

    startTx();

    ////////////////////////////////
    // Create Collection
    ////////////////////////////////
    log.info("Creating a Collection");
    Collection collection = createCollection("KUIVP", disciplineType.getTitle(),
            choice.getCatalogNumberingFmtName(), cns, discipline);
    persist(collection);

    // create the standard user groups for this collection
    Map<String, SpPrincipal> groupMap = DataBuilder.createStandardGroups(session, collection);

    // add the administrator as a Collections Manager in this group
    user.addUserToSpPrincipalGroup(groupMap.get(SpecifyUserTypes.UserType.Manager.toString()));

    // Tester
    createAndAddTesterToCollection(session, "ivpuser", "InvertPaleo@ku.edu", "ivpuser", "mr", "Joe", "",
            "InvertPaleo", "", discipline, division, collection, groupMap, "Guest");

    AppContextMgr.getInstance().setClassObject(Collection.class, collection);

    division.addReference(accessionNS, "numberingSchemes");
    persist(division);

    commitTx();

    doShowHideTablesAndFields(null, discipline);
    doShowHideTablesAndFields(disciplineType.getName(), discipline);

    frame.setProcess(++createStep);

    startTx();

    //DBTableIdMgr schema = new DBTableIdMgr(false);
    //schema.initialize(new File(XMLHelper.getConfigDirPath("specify_datamodel.xml")));
    //loadSchemaLocalization(discipline, SpLocaleContainer, schema);

    AppContextMgr.getInstance().setClassObject(SpecifyUser.class, user);
    user.addReference(userAgent, "agents");

    persist(user);

    Journal journal = createJournalsAndReferenceWork();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // build the tree def items and nodes
    ////////////////////////////////
    List<Object> taxa = createSimpleBotanyTaxonTree(taxonTreeDef);
    List<Object> geos = createSimpleGeography(geoTreeDef, true);
    List<Object> gtps = createSimpleGeologicTimePeriod(gtpTreeDef, true);
    //List<Object> lithoStrats = createSimpleLithoStrat(lithoStratTreeDef, true);

    persist(journal);
    persist(taxa);
    persist(geos);
    persist(buildStorageTree ? createSimpleStorage(stgTreeDef) : null);
    persist(gtps);
    //persist(lithoStrats);
    commitTx();

    LithoStrat earthLithoStrat = convertLithoStratFromCSV(lithoStratTreeDef);
    if (earthLithoStrat == null) {
        //throw new RuntimeException("No Tree");
        startTx();
        List<Object> lithoStrats = createSimpleLithoStrat(lithoStratTreeDef, true);
        persist(lithoStrats);
        commitTx();
    }

    frame.setProcess(++createStep);

    ////////////////////////////////
    // picklists
    ////////////////////////////////

    log.info("Creating picklists");
    frame.setDesc("Creating Common PickLists...");
    //frame.setProcess(++createStep);

    createPickLists(session, null);

    frame.setDesc("Creating PickLists...");
    createPickLists(session, discipline);

    Vector<Object> dataObjects = new Vector<Object>();

    startTx();

    frame.setDesc("Creating Queries...");
    standardQueries(dataObjects, userAgent);
    persist(dataObjects);
    dataObjects.clear();

    //BldrPickList colMethods = createPickLists();

    persist(dataObjects);
    dataObjects.clear();

    frame.setDesc("Intermediate save....");
    commitTx();

    frame.setDesc("Creating Localities....");
    startTx();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // localities
    ////////////////////////////////
    String POINT = "Point";
    String LINE = "Line";
    String RECT = "Rectangle";

    log.info("Creating localities");
    Locality forestStream = createLocality("Gravel Pit", (Geography) geos.get(12));
    forestStream.setLatLongType(POINT);
    forestStream.setOriginalLatLongUnit(0);
    forestStream.setLat1text("38.925467 deg N");
    forestStream.setLatitude1(new BigDecimal(38.925467));
    forestStream.setLong1text("94.984867 deg W");
    forestStream.setLongitude1(new BigDecimal(-94.984867));

    Locality lake = createLocality("Deep, dark lake pond", (Geography) geos.get(17));
    lake.setLatLongType(RECT);
    lake.setOriginalLatLongUnit(1);
    lake.setLat1text("41.548842 deg N");
    lake.setLatitude1(new BigDecimal(41.548842));
    lake.setLong1text("93.732129 deg W");
    lake.setLongitude1(new BigDecimal(-93.732129));

    lake.setLat2text("41.642195 deg N");
    lake.setLatitude2(new BigDecimal(41.642195));
    lake.setLong2text("100.403180 deg W");
    lake.setLongitude2(new BigDecimal(-100.403180));

    Locality farmpond = createLocality("Shoal Creek at Schermerhorn Park, S of Galena at Rt. 26",
            (Geography) geos.get(11));
    farmpond.setLatLongType(LINE);
    farmpond.setOriginalLatLongUnit(2);
    farmpond.setLat1text("41.642187 deg N");
    farmpond.setLatitude1(new BigDecimal(41.642187));
    farmpond.setLong1text("100.403163 deg W");
    farmpond.setLongitude1(new BigDecimal(-100.403163));

    farmpond.setLat2text("49.647435 deg N");
    farmpond.setLatitude2(new BigDecimal(49.647435));
    farmpond.setLong2text("-55.112163 deg W");
    farmpond.setLongitude2(new BigDecimal(-55.112163));

    persist(forestStream);
    persist(lake);
    persist(farmpond);

    commitTx();

    startTx();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // agents and addresses
    ////////////////////////////////
    log.info("Creating agents and addresses");

    List<Agent> agents = new Vector<Agent>();

    lastName = userAgent.getLastName();
    Agent steveBoyd = createAgent("mr", "Steve", "D", "Boyd", "jb", "jb@net.edu");
    if (!lastName.equals("Cooper"))
        agents.add(createAgent("mr", "Peter", "D", "Cooper", "ds", "ds@whitehouse.gov"));
    if (!lastName.equals("Peck"))
        agents.add(createAgent("mr", "David", "H", "Peck", "rb", "beach@net.edu"));
    if (!lastName.equals("Appleton"))
        agents.add(createAgent("mrs", "Sally", "H", "Appleton", "jm", "jm@net.edu"));
    if (!lastName.equals("Brown"))
        agents.add(createAgent("mr", "Taylor", "C", "Brown", "kcs", "taylor.brown@ku.edu"));
    if (!lastName.equals("Boyd"))
        agents.add(steveBoyd);
    if (!lastName.equals("Thomas"))
        agents.add(createAgent("Mr", "James", "X", "Thomas", "dxt", ""));
    if (!lastName.equals("Peterson"))
        agents.add(createAgent("mr", "Pete", "A", "Peterson", "jb", ""));
    if (!lastName.equals("Guttenburg"))
        agents.add(createAgent("mr", "Mitch", "A", "Guttenburg", "jb", ""));
    if (!lastName.equals("Ford"))
        agents.add(createAgent("mr", "Daniel", "A", "Ford", "mas", "mas@ku.edu"));
    agents.add(userAgent);

    Agent ku = new Agent();
    ku.initialize();
    ku.setAbbreviation("KU");
    ku.setAgentType(Agent.ORG);
    ku.setLastName("University of Kansas");
    ku.setEmail("webadmin@ku.edu");
    ku.setTimestampCreated(new Timestamp(System.currentTimeMillis()));
    ku.setDivision(AppContextMgr.getInstance().getClassObject(Division.class));

    agents.add(ku);
    agents.get(0).setOrganization(ku);
    agents.get(1).setOrganization(ku);
    agents.get(2).setOrganization(ku);
    agents.get(3).setOrganization(ku);
    agents.get(8).setOrganization(ku);

    Agent otherAgent = new Agent();
    otherAgent.initialize();
    otherAgent.setAbbreviation("O");
    otherAgent.setAgentType(Agent.OTHER);
    otherAgent.setLastName("The Other Guys");
    otherAgent.setEmail("other@other.com");
    otherAgent.setTimestampCreated(new Timestamp(System.currentTimeMillis()));
    otherAgent.setDivision(AppContextMgr.getInstance().getClassObject(Division.class));

    agents.add(otherAgent);

    commitTx();

    List<GroupPerson> gpList = new ArrayList<GroupPerson>();
    if (true) {
        startTx();
        Agent gm1 = createAgent("mr", "John", "A", "Lyon", "jal", "jal@group.edu");
        Agent gm2 = createAgent("mr", "Dave", "D", "Jones", "ddj", "ddj@group.edu");
        persist(gm1);
        persist(gm2);
        commitTx();

        Agent groupAgent = new Agent();
        groupAgent.initialize();
        groupAgent.setAbbreviation("GRP");
        groupAgent.setAgentType(Agent.GROUP);
        groupAgent.setLastName("The Group");
        groupAgent.setEmail("group@group.com");
        groupAgent.setTimestampCreated(new Timestamp(System.currentTimeMillis()));
        groupAgent.setDivision(AppContextMgr.getInstance().getClassObject(Division.class));

        agents.add(groupAgent);

        gpList.add(createGroupPerson(groupAgent, gm1, 0, division));
        gpList.add(createGroupPerson(groupAgent, gm2, 1, division));
    }

    startTx();

    List<Address> addrs = new Vector<Address>();
    addrs.add(createAddress(agents.get(1), "1600 Pennsylvania Avenue NW", null, "Washington", "DC", "USA",
            "20500", 0));
    addrs.add(createAddress(agents.get(1), "??? Mississippi", null, "Lawrence", "KS", "USA", "66045", 1));
    addrs.add(createAddress(agents.get(2), "1 Main St", "", "Lenexa", "KS", "USA", "66071"));
    addrs.add(createAddress(agents.get(3), "13355 Inverness", "Bldg #3", "Lawrence", "KS", "USA", "66047"));
    addrs.add(createAddress(agents.get(4), "Natural History Museum", "Cromwell Rd", "London", null, "UK",
            "SW7 5BD"));
    addrs.add(createAddress(agents.get(6), "1212 Apple Street", null, "Chicago", "IL", "USA", "01010"));
    addrs.add(createAddress(agents.get(8), "11911 Oak Ln", null, "Orion", "KS", "USA", "66061"));
    addrs.add(createAddress(ku, null, null, "Lawrence", "KS", "USA", "66045"));

    // User Agent Address
    addrs.add(createAddress(userAgent, "1214 East Street", null, "Grinnell", "IA", "USA", "56060"));
    userAgent.setDivision(division);

    persist(agents);
    persist(gpList);
    commitTx();

    startTx();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // collecting events (collectors, collecting trip)
    ////////////////////////////////
    log.info("Creating collecting events, collectors and a collecting trip");
    Collector collectorMitch = createCollector(agents.get(7), 2);
    Collector collectorJim = createCollector(agents.get(2), 1);

    String stationFieldNumber = String.format(STATION_FIELD_FORMAT, stationFieldNumberCounter++);
    calendar.set(1994, 4, 21, 11, 56, 00);
    CollectingEvent ce1 = createCollectingEvent(forestStream, calendar, stationFieldNumber,
            new Collector[] { collectorMitch, collectorJim });
    ce1.setStartDateVerbatim("21 Apr 1994, 11:56 AM");
    calendar.set(1994, 4, 21, 13, 03, 00);
    ce1.setEndDate(calendar);
    ce1.setEndDateVerbatim("21 Apr 1994, 1:03 PM");
    ce1.setMethod("Picked");

    AttributeDef cevAttrDef = createAttributeDef(AttributeIFace.FieldType.StringType, "ParkName", discipline,
            null);//meg added cod

    persist(cevAttrDef);
    commitTx();

    startTx();

    CollectingEventAttr cevAttr = createCollectingEventAttr(ce1, cevAttrDef, "Sleepy Hollow", null);

    Collector collectorMeg = createCollector(agents.get(2), 1);
    Collector collectorRod = createCollector(agents.get(3), 2);
    stationFieldNumber = String.format(STATION_FIELD_FORMAT, stationFieldNumberCounter++);
    calendar.set(1994, 4, 22, 06, 12, 00);
    CollectingEvent ce2 = createCollectingEvent(farmpond, calendar, stationFieldNumber,
            new Collector[] { collectorMeg, collectorRod });
    ce2.setStartDateVerbatim("22 Apr 1994, 6:12 AM");
    calendar.set(1994, 4, 22, 07, 31, 00);
    ce2.setEndDate(calendar);
    ce2.setEndDateVerbatim("22 Apr 1994, 7:31 AM");
    ce2.setMethod("Picked");

    //CollectingTrip trip = createCollectingTrip("Sample collecting trip", new CollectingEvent[]{ce1,ce2});

    //dataObjects.add(trip);
    dataObjects.add(ce1);
    dataObjects.add(cevAttr);
    dataObjects.add(ce2);
    dataObjects.add(collectorMitch);
    dataObjects.add(collectorJim);
    dataObjects.add(collectorMeg);
    dataObjects.add(collectorRod);

    persist(dataObjects);
    dataObjects.clear();

    commitTx();

    startTx();
    ////////////////////////////////
    // permit
    ////////////////////////////////
    log.info("Creating a permit");
    Calendar issuedDate = Calendar.getInstance();
    issuedDate.set(1993, 1, 12);
    Calendar startDate = Calendar.getInstance();
    startDate.set(1993, 2, 1);
    Calendar endDate = Calendar.getInstance();
    endDate.set(1993, 5, 30);
    Permit permit = createPermit("1980-INVRTP-0001", "US Dept Wildlife", issuedDate, startDate, endDate, null);
    permit.setIssuedTo(ku);
    permit.setIssuedBy(agents.get(4));
    dataObjects.add(permit);

    log.info("Creating a repository agreement");
    RepositoryAgreement repoAg = new RepositoryAgreement();
    repoAg.initialize();
    repoAg.setDivision(division);
    repoAg.setRepositoryAgreementNumber("KU-1979-01");
    repoAg.setOriginator(ku);
    Calendar received = Calendar.getInstance();
    received.set(1992, 2, 10);
    repoAg.setDateReceived(received);
    Calendar repoEndDate = Calendar.getInstance();
    received.set(2010, 2, 9);
    repoAg.setEndDate(repoEndDate);
    dataObjects.add(repoAg);

    persist(dataObjects);
    dataObjects.clear();
    commitTx();

    startTx();
    frame.setProcess(++createStep);

    log.info("Creating collection objects");

    List<CollectionObject> collObjs = new Vector<CollectionObject>();
    Collection col = collection;

    Calendar[] catDates = new Calendar[8];
    for (int i = 0; i < catDates.length; i++) {
        catDates[i] = Calendar.getInstance();
        int year = 1980 + (int) (rand.nextDouble() * 20.0);
        catDates[i].set(year, 01, 12 + i);
    }

    String prefix = "000000";
    int catNo = 100;
    CollectingEvent[] colEves = new CollectingEvent[8];
    for (int i = 0; i < colEves.length; i++) {
        colEves[i] = createFakeCollectingEvent(agents, farmpond, "Dug");
        collObjs.add(createCollectionObject(prefix + Integer.toString(catNo), "RSC" + Integer.toString(catNo),
                agents.get(i), col, 1, colEves[i], catDates[i], "BuildSampleDatabase"));
        catNo++;
    }
    dataObjects.addAll(collObjs);
    for (CollectingEvent ce : colEves) {
        persist(ce);
    }

    persist(dataObjects);
    dataObjects.clear();

    commitTx();

    startTx();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // determinations (determination status)
    ////////////////////////////////
    log.info("Creating determinations");

    List<Determination> determs = new Vector<Determination>();
    Calendar recent = Calendar.getInstance();
    recent.set(2005, 10, 27, 13, 44, 00);
    Calendar longAgo = Calendar.getInstance();
    longAgo.set(1976, 01, 29, 8, 12, 00);
    Calendar whileBack = Calendar.getInstance();
    whileBack.set(2000, 7, 4, 9, 33, 12);

    determs.add(createDetermination(collObjs.get(0), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(1), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(2), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(3), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(4), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(5), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(6), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(7), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));

    determs.add(createDetermination(collObjs.get(0), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, longAgo));
    determs.add(createDetermination(collObjs.get(1), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, whileBack));
    determs.add(createDetermination(collObjs.get(2), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, whileBack));
    determs.add(createDetermination(collObjs.get(3), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, whileBack));
    determs.add(createDetermination(collObjs.get(4), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, whileBack));
    determs.add(createDetermination(collObjs.get(4), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, longAgo));
    determs.add(createDetermination(collObjs.get(4), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, longAgo));
    determs.get(13).setRemarks("This determination is totally wrong.  What a foolish determination.");

    persist(determs);
    dataObjects.clear();
    commitTx();

    startTx();
    frame.setProcess(++createStep);

    ////////////////////////////////
    // preparations (prep types)
    ////////////////////////////////
    log.info("Creating preparations");

    Vector<PrepType> prepTypesForSaving = loadPrepTypes(discipline.getType());
    PrepType pressed = prepTypesForSaving.get(0);

    List<Preparation> preps = new Vector<Preparation>();
    Calendar prepDate = Calendar.getInstance();
    preps.add(createPreparation(pressed, agents.get(0), collObjs.get(0), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(0), collObjs.get(1), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(2), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(3), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(2), collObjs.get(4), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(2), collObjs.get(5), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(3), collObjs.get(6), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(3), collObjs.get(7), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(0), (Storage) locs.get(11),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(1), (Storage) locs.get(11),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(2), (Storage) locs.get(10),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(2), collObjs.get(3), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(3), collObjs.get(4), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(0), collObjs.get(5), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(6), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(7), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(2), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));

    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(0), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(1), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(2), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(3), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(4), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));

    dataObjects.addAll(prepTypesForSaving);
    dataObjects.addAll(preps);

    persist(dataObjects);
    dataObjects.clear();

    commitTx();

    startTx();
    frame.setProcess(++createStep);

    ////////////////////////////////
    // accessions (accession agents)
    ////////////////////////////////
    log.info("Creating accessions and accession agents");
    calendar.set(2006, 10, 27, 23, 59, 59);
    Accession acc1 = createAccession(division, "gift", "complete", "2000-IP-001",
            DateFormat.getInstance().format(calendar.getTime()), calendar, calendar);
    acc1.setText1(disciplineType.getTitle());
    acc1.setRepositoryAgreement(repoAg);

    Agent donor = agents.get(4);
    Agent receiver = agents.get(1);
    Agent reviewer = agents.get(2);

    List<AccessionAgent> accAgents = new Vector<AccessionAgent>();

    accAgents.add(createAccessionAgent("donor", donor, acc1, null));
    accAgents.add(createAccessionAgent("receiver", receiver, acc1, null));
    accAgents.add(createAccessionAgent("reviewer", reviewer, acc1, null));

    Accession acc2 = createAccession(division, "field_work", "inprocess", "2004-IP-002",
            DateFormat.getInstance().format(calendar.getTime()), calendar, calendar);

    Agent donor2 = agents.get(5);
    Agent receiver2 = agents.get(3);
    Agent reviewer2 = agents.get(1);

    accAgents.add(createAccessionAgent("donor", donor2, acc2, null));
    accAgents.add(createAccessionAgent("receiver", receiver2, acc2, null));
    accAgents.add(createAccessionAgent("reviewer", reviewer2, acc2, null));

    dataObjects.add(acc1);
    dataObjects.add(acc2);
    dataObjects.addAll(accAgents);

    persist(dataObjects);
    dataObjects.clear();
    commitTx();

    startTx();
    frame.setProcess(++createStep);

    createLoanExamples(preps, agents, dataObjects);
    frame.setProcess(++createStep);

    persist(dataObjects);
    dataObjects.clear();

    persist(dataObjects);
    dataObjects.clear();

    frame.setProcess(++createStep);

    commitTx();

    frame.setProcess(++createStep);

    buildDarwinCoreSchema(discipline);

    // done
    log.info("Done creating " + disciplineType.getTitle() + " disciplineType database: "
            + disciplineType.getTitle());
    return dataObjects;
}

From source file:edu.ku.brc.specify.utilapps.BuildSampleDatabase.java

/**
 * Creates a single disciplineType collection.
 * @param disciplineName the name of the Discipline to use
 * @param disciplineName the disciplineType name
 * @return the entire list of DB object to be persisted
 *///w  w w  . j  a v a  2  s.c om
public List<Object> createGenericCollection(final DisciplineType disciplineType, final Institution institution,
        final SpecifyUser user, final CollectionChoice choice, final String method) {
    frame.setProcess(0, 16);
    frame.setDesc("Creating " + disciplineType.getTitle() + "...");

    createStep = 0;

    startTx();

    Division division = createDivision(institution, disciplineType.getName(), disciplineType.getTitle(),
            disciplineType.getAbbrev(), disciplineType.getTitle());

    // create tree defs (later we will make the def items and nodes)
    TaxonTreeDef taxonTreeDef = createTaxonTreeDef("Taxon", TreeDefIface.FORWARD);
    GeographyTreeDef geoTreeDef = createGeographyTreeDef("Geography", TreeDefIface.REVERSE);
    GeologicTimePeriodTreeDef gtpTreeDef = createGeologicTimePeriodTreeDef("Chronos Stratigraphy",
            TreeDefIface.REVERSE);
    LithoStratTreeDef lithoStratTreeDef = createLithoStratTreeDef("LithoStrat");

    boolean buildStorageTree = false;
    if (stgTreeDef == null) {
        stgTreeDef = createStorageTreeDef("Storage", TreeDefIface.REVERSE);
        institution.setStorageTreeDef(stgTreeDef);
        buildStorageTree = true;
    }

    Discipline discipline = createDiscipline(division, disciplineType.getName(), disciplineType.getTitle(),
            dataType, taxonTreeDef, geoTreeDef, gtpTreeDef, lithoStratTreeDef);
    AppContextMgr.getInstance().setClassObject(Discipline.class, discipline);
    AppContextMgr.getInstance().setClassObject(Institution.class, institution);

    persist(institution);
    persist(division);
    persist(discipline);

    AppContextMgr.getInstance().setClassObject(Division.class, division);
    AppContextMgr.getInstance().setClassObject(Discipline.class, discipline);
    AppContextMgr.getInstance().setClassObject(Institution.class, institution);

    frame.setDesc("Loading Schema...");

    loadSchemaLocalization(discipline, SpLocaleContainer.CORE_SCHEMA, DBTableIdMgr.getInstance(),
            choice.getCatalogNumberingFmtName(), choice.getAccessionNumberingFmtName());

    ////////////////////////////////
    // Create the really high-level stuff
    ////////////////////////////////
    String title = initPrefs.getProperty("useragent.title", "mr");
    String firstName = initPrefs.getProperty("useragent.firstname", "Test");
    String lastName = initPrefs.getProperty("useragent.lastname", "User");
    String midInit = initPrefs.getProperty("useragent.midinit", "A");
    String abbrev = initPrefs.getProperty("useragent.abbrev", "tu");
    String email = initPrefs.getProperty("useragent.email", "testuser@ku.edu");
    String userType = initPrefs.getProperty("useragent.usertype", SpecifyUserTypes.UserType.Manager.toString());

    System.out.println("----- User Agent -----");
    System.out.println("Title:     " + title);
    System.out.println("FirstName: " + firstName);
    System.out.println("LastName:  " + lastName);
    System.out.println("MidInit:   " + midInit);
    System.out.println("Abbrev:    " + abbrev);
    System.out.println("Email:     " + email);
    System.out.println("UserType:  " + userType);

    Agent userAgent = createAgent(title, firstName, midInit, lastName, abbrev, email);

    discipline.addReference(userAgent, "agents");
    user.addReference(userAgent, "agents");

    persist(discipline);
    persist(userAgent);
    persist(user);

    frame.setProcess(++createStep);

    Pair<AutoNumberingScheme, AutoNumberingScheme> pairANS = createAutoNumberingSchemes(choice);
    AutoNumberingScheme cns = pairANS.first;
    AutoNumberingScheme accessionNS = pairANS.second;

    persist(cns);
    persist(accessionNS);

    commitTx();

    startTx();

    ////////////////////////////////
    // Create Collection
    ////////////////////////////////
    log.info("Creating a Collection");
    frame.setDesc("Creating a Collection");

    Collection collection = createCollection("KU", disciplineType.getTitle(),
            choice.getCatalogNumberingFmtName(), cns, discipline, disciplineType.isEmbeddedCollecingEvent());
    persist(collection);

    // create the standard user groups for this collection
    Map<String, SpPrincipal> groupMap = DataBuilder.createStandardGroups(session, collection);

    // add the administrator as a Collections Manager in this group
    user.addUserToSpPrincipalGroup(groupMap.get(SpecifyUserTypes.UserType.Manager.toString()));

    // Tester
    String dspAbbrev = disciplineType.getAbbrev();
    createAndAddTesterToCollection(session, dspAbbrev + "Tester", dspAbbrev + "tester@brc.ku.edu",
            dspAbbrev + "Tester", "", dspAbbrev, "", "Tester", "", discipline, division, collection, groupMap,
            "Guest");

    AppContextMgr.getInstance().setClassObject(Collection.class, collection);

    division.addReference(accessionNS, "numberingSchemes");
    persist(division);

    commitTx();

    doShowHideTablesAndFields(null, discipline);
    doShowHideTablesAndFields(disciplineType.getName(), discipline);

    frame.setProcess(++createStep);

    startTx();

    createTaxonTreeDefFromXML(taxonTreeDef, disciplineType);
    persist(taxonTreeDef);

    //DBTableIdMgr schema = new DBTableIdMgr(false);
    //schema.initialize(new File(XMLHelper.getConfigDirPath("specify_datamodel.xml")));
    //loadSchemaLocalization(discipline, SpLocaleContainer, schema);
    //buildDarwinCoreSchema(discipline);

    AppContextMgr.getInstance().setClassObject(SpecifyUser.class, user);
    user.addReference(userAgent, "agents");

    persist(user);

    Journal journal = createJournalsAndReferenceWork();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // build the tree def items and nodes
    ////////////////////////////////
    frame.setDesc("Building Trees...");
    Vector<Object> taxa = new Vector<Object>();
    createTaxonTreeFromXML(taxa, taxonTreeDef, disciplineType);

    boolean isPaleo = disciplineType.getDisciplineType() == DisciplineType.STD_DISCIPLINES.paleobotany
            || disciplineType.getDisciplineType() == DisciplineType.STD_DISCIPLINES.vertpaleo
            || disciplineType.getDisciplineType() == DisciplineType.STD_DISCIPLINES.invertpaleo;

    if (isPaleo) {
        //            LithoStratTreeDefItem earth     = createLithoStratTreeDefItem(lithoStratTreeDef, "Earth", 0, false);
        //            LithoStratTreeDefItem superGrp  = createLithoStratTreeDefItem(earth,     "Super Group", 100, false);
        //            LithoStratTreeDefItem lithoGrp  = createLithoStratTreeDefItem(superGrp,  "Litho Group", 200, false);
        //            LithoStratTreeDefItem formation = createLithoStratTreeDefItem(lithoGrp,  "Formation",   300, false);
        //            LithoStratTreeDefItem member    = createLithoStratTreeDefItem(formation, "Member",      400, false);
        //            @SuppressWarnings("unused")
        //            LithoStratTreeDefItem bed       = createLithoStratTreeDefItem(member,    "Bed",         500, true);
        //            persist(earth);
    }

    List<Object> geos = createSimpleGeography(geoTreeDef, true);
    List<Object> gtps = createSimpleGeologicTimePeriod(gtpTreeDef, true);
    List<Object> lithoStrats = isPaleo ? null : createSimpleLithoStrat(lithoStratTreeDef, true);

    persist(journal);
    persist(taxa);
    persist(geos);
    persist(buildStorageTree ? createSimpleStorage(stgTreeDef) : null);
    persist(gtps);

    if (lithoStrats != null) {
        persist(lithoStrats);

    }
    commitTx();

    if (isPaleo) {
        LithoStrat earthLithoStrat = convertLithoStratFromCSV(lithoStratTreeDef);// does startTx() / commitTx
        if (earthLithoStrat == null) {
            startTx();
            lithoStrats = createSimpleLithoStrat(lithoStratTreeDef, true);
            persist(lithoStrats);
            commitTx();
        }
    }

    frame.setProcess(++createStep);

    ////////////////////////////////
    // picklists
    ////////////////////////////////

    log.info("Creating picklists");
    frame.setDesc("Creating Common PickLists...");
    //frame.setProcess(++createStep);

    createPickLists(session, null);

    frame.setDesc("Creating PickLists...");
    createPickLists(session, discipline);

    startTx();
    Vector<Object> dataObjects = new Vector<Object>();

    frame.setDesc("Creating Queries...");
    standardQueries(dataObjects, userAgent);
    persist(dataObjects);
    dataObjects.clear();

    persist(dataObjects);
    dataObjects.clear();

    frame.setDesc("Intermediate save....");
    commitTx();

    frame.setDesc("Creating Localities....");
    startTx();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // localities
    ////////////////////////////////
    String POINT = "Point";
    String LINE = "Line";
    String RECT = "Rectangle";

    log.info("Creating localities");
    frame.setDesc("Creating localities...");
    Locality forestStream = createLocality("Gravel Pit", (Geography) geos.get(12));
    forestStream.setLatLongType(POINT);
    forestStream.setOriginalLatLongUnit(0);
    forestStream.setLat1text("38.925467 deg N");
    forestStream.setLatitude1(new BigDecimal(38.925467));
    forestStream.setLong1text("94.984867 deg W");
    forestStream.setLongitude1(new BigDecimal(-94.984867));

    Locality lake = createLocality("Deep, dark lake pond", (Geography) geos.get(17));
    lake.setLatLongType(RECT);
    lake.setOriginalLatLongUnit(1);
    lake.setLat1text("41.548842 deg N");
    lake.setLatitude1(new BigDecimal(41.548842));
    lake.setLong1text("93.732129 deg W");
    lake.setLongitude1(new BigDecimal(-93.732129));

    lake.setLat2text("41.642195 deg N");
    lake.setLatitude2(new BigDecimal(41.642195));
    lake.setLong2text("100.403180 deg W");
    lake.setLongitude2(new BigDecimal(-100.403180));

    Locality farmpond = createLocality("Shoal Creek at Schermerhorn Park, S of Galena at Rt. 26",
            (Geography) geos.get(11));
    farmpond.setLatLongType(LINE);
    farmpond.setOriginalLatLongUnit(2);
    farmpond.setLat1text("41.642187 deg N");
    farmpond.setLatitude1(new BigDecimal(41.642187));
    farmpond.setLong1text("100.403163 deg W");
    farmpond.setLongitude1(new BigDecimal(-100.403163));

    farmpond.setLat2text("49.647435 deg N");
    farmpond.setLatitude2(new BigDecimal(49.647435));
    farmpond.setLong2text("-55.112163 deg W");
    farmpond.setLongitude2(new BigDecimal(-55.112163));

    persist(forestStream);
    persist(lake);
    persist(farmpond);

    commitTx();

    startTx();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // agents and addresses
    ////////////////////////////////
    log.info("Creating agents and addresses");
    frame.setDesc("Creating agents and addresses");

    List<Agent> agents = new Vector<Agent>();

    lastName = userAgent.getLastName();
    Agent steveBoyd = createAgent("mr", "Steve", "D", "Boyd", "jb", "jb@net.edu");
    if (!lastName.equals("Cooper"))
        agents.add(createAgent("mr", "Peter", "D", "Cooper", "ds", "ds@whitehouse.gov"));
    if (!lastName.equals("Peck"))
        agents.add(createAgent("mr", "David", "H", "Peck", "rb", "beach@net.edu"));
    if (!lastName.equals("Appleton"))
        agents.add(createAgent("mrs", "Sally", "H", "Appleton", "jm", "jm@net.edu"));
    if (!lastName.equals("Brown"))
        agents.add(createAgent("mr", "Taylor", "C", "Brown", "kcs", "taylor.brown@ku.edu"));
    if (!lastName.equals("Boyd"))
        agents.add(steveBoyd);
    if (!lastName.equals("Thomas"))
        agents.add(createAgent("Mr", "James", "X", "Thomas", "dxt", ""));
    if (!lastName.equals("Peterson"))
        agents.add(createAgent("mr", "Pete", "A", "Peterson", "jb", ""));
    if (!lastName.equals("Guttenburg"))
        agents.add(createAgent("mr", "Mitch", "A", "Guttenburg", "jb", ""));
    if (!lastName.equals("Ford"))
        agents.add(createAgent("mr", "Daniel", "A", "Ford", "mas", "mas@ku.edu"));
    agents.add(userAgent);

    Agent ku = new Agent();
    ku.initialize();
    ku.setAbbreviation("KU");
    ku.setAgentType(Agent.ORG);
    ku.setLastName("University of Kansas");
    ku.setEmail("webadmin@ku.edu");
    ku.setTimestampCreated(new Timestamp(System.currentTimeMillis()));
    ku.setDivision(AppContextMgr.getInstance().getClassObject(Division.class));

    agents.add(ku);
    agents.get(0).setOrganization(ku);
    agents.get(1).setOrganization(ku);
    agents.get(2).setOrganization(ku);
    agents.get(3).setOrganization(ku);
    agents.get(8).setOrganization(ku);

    Agent otherAgent = new Agent();
    otherAgent.initialize();
    otherAgent.setAbbreviation("O");
    otherAgent.setAgentType(Agent.OTHER);
    otherAgent.setLastName("The Other Guys");
    otherAgent.setEmail("other@other.com");
    otherAgent.setTimestampCreated(new Timestamp(System.currentTimeMillis()));
    otherAgent.setDivision(AppContextMgr.getInstance().getClassObject(Division.class));

    agents.add(otherAgent);

    commitTx();

    frame.setDesc("Group Persons...");
    List<GroupPerson> gpList = new ArrayList<GroupPerson>();
    if (true) {
        startTx();
        Agent gm1 = createAgent("mr", "John", "A", "Lyon", "jal", "jal@group.edu");
        Agent gm2 = createAgent("mr", "Dave", "D", "Jones", "ddj", "ddj@group.edu");
        persist(gm1);
        persist(gm2);
        commitTx();

        Agent groupAgent = new Agent();
        groupAgent.initialize();
        groupAgent.setAbbreviation("GRP");
        groupAgent.setAgentType(Agent.GROUP);
        groupAgent.setLastName("The Group");
        groupAgent.setEmail("group@group.com");
        groupAgent.setTimestampCreated(new Timestamp(System.currentTimeMillis()));
        groupAgent.setDivision(AppContextMgr.getInstance().getClassObject(Division.class));

        agents.add(groupAgent);

        gpList.add(createGroupPerson(groupAgent, gm1, 0, division));
        gpList.add(createGroupPerson(groupAgent, gm2, 1, division));
    }

    startTx();

    List<Address> addrs = new Vector<Address>();
    addrs.add(createAddress(agents.get(1), "1600 Pennsylvania Avenue NW", null, "Washington", "DC", "USA",
            "20500", 0));
    addrs.add(createAddress(agents.get(1), "??? Mississippi", null, "Lawrence", "KS", "USA", "66045", 1));
    addrs.add(createAddress(agents.get(2), "1 Main St", "", "Lenexa", "KS", "USA", "66071"));
    addrs.add(createAddress(agents.get(3), "13355 Inverness", "Bldg #3", "Lawrence", "KS", "USA", "66047"));
    addrs.add(createAddress(agents.get(4), "Natural History Museum", "Cromwell Rd", "London", null, "UK",
            "SW7 5BD"));
    addrs.add(createAddress(agents.get(6), "1212 Apple Street", null, "Chicago", "IL", "USA", "01010"));
    addrs.add(createAddress(agents.get(8), "11911 Oak Ln", null, "Orion", "KS", "USA", "66061"));
    addrs.add(createAddress(ku, null, null, "Lawrence", "KS", "USA", "66045"));

    // User Agent Address
    addrs.add(createAddress(userAgent, "1214 East Street", null, "Grinnell", "IA", "USA", "56060"));
    userAgent.setDivision(division);

    persist(agents);
    persist(gpList);
    commitTx();

    startTx();

    frame.setProcess(++createStep);
    frame.setDesc("Creating collecting events, collectors and a collecting trip...");
    //////////////////////////////////////////////////
    // collecting events (collectors, collecting trip)
    ///////////////////////////////////////////////////
    log.info("Creating collecting events, collectors and a collecting trip");
    Collector collectorMitch = createCollector(agents.get(7), 2);
    Collector collectorJim = createCollector(agents.get(2), 1);

    calendar.set(1994, 4, 21, 11, 56, 00);
    String stationFieldNumber = String.format(STATION_FIELD_FORMAT, stationFieldNumberCounter++);
    CollectingEvent ce1 = createCollectingEvent(forestStream, calendar, stationFieldNumber,
            new Collector[] { collectorMitch, collectorJim });
    ce1.setStartDateVerbatim("21 Apr 1994, 11:56 AM");
    calendar.set(1994, 4, 21, 13, 03, 00);
    ce1.setEndDate(calendar);
    ce1.setEndDateVerbatim("21 Apr 1994, 1:03 PM");
    ce1.setMethod("Picked");

    AttributeDef cevAttrDef = createAttributeDef(AttributeIFace.FieldType.StringType, "ParkName", discipline,
            null);//meg added cod

    persist(cevAttrDef);
    commitTx();

    startTx();

    CollectingEventAttr cevAttr = createCollectingEventAttr(ce1, cevAttrDef, "Sleepy Hollow", null);

    Collector collectorMeg = createCollector(agents.get(2), 1);
    Collector collectorRod = createCollector(agents.get(3), 2);
    calendar.set(1994, 4, 22, 06, 12, 00);
    stationFieldNumber = String.format(STATION_FIELD_FORMAT, stationFieldNumberCounter++);
    CollectingEvent ce2 = createCollectingEvent(farmpond, calendar, stationFieldNumber,
            new Collector[] { collectorMeg, collectorRod });
    ce2.setStartDateVerbatim("22 Apr 1994, 6:12 AM");
    calendar.set(1994, 4, 22, 07, 31, 00);
    ce2.setEndDate(calendar);
    ce2.setEndDateVerbatim("22 Apr 1994, 7:31 AM");
    ce2.setMethod("Picked");

    //CollectingTrip trip = createCollectingTrip("Sample collecting trip", new CollectingEvent[]{ce1,ce2});

    //dataObjects.add(trip);
    dataObjects.add(ce1);
    dataObjects.add(cevAttr);
    dataObjects.add(ce2);
    dataObjects.add(collectorMitch);
    dataObjects.add(collectorJim);
    dataObjects.add(collectorMeg);
    dataObjects.add(collectorRod);

    persist(dataObjects);
    dataObjects.clear();

    commitTx();

    startTx();
    ////////////////////////////////
    // permit
    ////////////////////////////////
    log.info("Creating a permit");
    frame.setDesc("Creating a permit...");

    Calendar issuedDate = Calendar.getInstance();
    issuedDate.set(1993, 1, 12);
    Calendar startDate = Calendar.getInstance();
    startDate.set(1993, 2, 1);
    Calendar endDate = Calendar.getInstance();
    endDate.set(1993, 5, 30);

    Permit permit = createPermit("1980-" + disciplineType.getAbbrev().substring(0, 2) + "-0001",
            "US Dept Wildlife", issuedDate, startDate, endDate, null);
    permit.setIssuedTo(ku);
    permit.setIssuedBy(agents.get(4));
    dataObjects.add(permit);

    log.info("Creating a repository agreement");
    RepositoryAgreement repoAg = new RepositoryAgreement();
    repoAg.initialize();
    repoAg.setDivision(division);
    repoAg.setRepositoryAgreementNumber("KU-" + disciplineType.getAbbrev() + "-01");
    repoAg.setOriginator(ku);
    Calendar received = Calendar.getInstance();
    received.set(1992, 2, 10);
    repoAg.setDateReceived(received);
    Calendar repoEndDate = Calendar.getInstance();
    received.set(2010, 2, 9);
    repoAg.setEndDate(repoEndDate);
    dataObjects.add(repoAg);

    persist(dataObjects);
    dataObjects.clear();
    commitTx();

    startTx();
    frame.setProcess(++createStep);

    ////////////////////////////////
    // collection objects
    ////////////////////////////////
    log.info("Creating collection objects");
    frame.setDesc("Creating collection objects...");

    List<CollectionObject> collObjs = new Vector<CollectionObject>();
    Collection col = collection;

    Calendar[] catDates = new Calendar[8];
    for (int i = 0; i < catDates.length; i++) {
        catDates[i] = Calendar.getInstance();
        int year = 1980 + (int) (rand.nextDouble() * 20.0);
        catDates[i].set(year, 01, 12 + i);
    }

    String prefix = "000000";
    int catNo = 100;
    CollectingEvent[] colEves = new CollectingEvent[8];
    for (int i = 0; i < colEves.length; i++) {
        colEves[i] = createFakeCollectingEvent(agents, farmpond, method);
        collObjs.add(createCollectionObject(prefix + Integer.toString(catNo), "RSC" + Integer.toString(catNo),
                agents.get(i), col, 1, colEves[i], catDates[i], "BuildSampleDatabase"));
        catNo++;
    }
    dataObjects.addAll(collObjs);
    for (CollectingEvent ce : colEves) {
        persist(ce);
    }

    persist(dataObjects);
    dataObjects.clear();

    commitTx();

    startTx();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // determinations (determination status)
    ////////////////////////////////
    log.info("Creating determinations");
    frame.setDesc("Creating determinations...");

    List<Determination> determs = new Vector<Determination>();
    Calendar recent = Calendar.getInstance();
    recent.set(2005, 10, 27, 13, 44, 00);
    Calendar longAgo = Calendar.getInstance();
    longAgo.set(1976, 01, 29, 8, 12, 00);
    Calendar whileBack = Calendar.getInstance();
    whileBack.set(2000, 7, 4, 9, 33, 12);

    determs.add(createDetermination(collObjs.get(0), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(1), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(2), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(3), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(4), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(5), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(6), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));
    determs.add(createDetermination(collObjs.get(7), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), true, recent));

    determs.add(createDetermination(collObjs.get(0), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, longAgo));
    determs.add(createDetermination(collObjs.get(1), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, whileBack));
    determs.add(createDetermination(collObjs.get(2), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, whileBack));
    determs.add(createDetermination(collObjs.get(3), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, whileBack));
    determs.add(createDetermination(collObjs.get(4), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, whileBack));
    determs.add(createDetermination(collObjs.get(4), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, longAgo));
    determs.add(createDetermination(collObjs.get(4), getRandomAgent(agents),
            getRandomTaxon(TaxonTreeDef.SPECIES, taxa), false, longAgo));
    determs.get(13).setRemarks("This determination is totally wrong.  What a foolish determination.");

    persist(determs);
    dataObjects.clear();
    commitTx();

    startTx();
    frame.setProcess(++createStep);

    ////////////////////////////////
    // preparations (prep types)
    ////////////////////////////////
    log.info("Creating preparations");
    frame.setDesc("Creating preparations...");

    Vector<PrepType> prepTypesForSaving = loadPrepTypes(discipline.getType());
    PrepType pressed = prepTypesForSaving.get(0);

    List<Preparation> preps = new Vector<Preparation>();
    Calendar prepDate = Calendar.getInstance();
    preps.add(createPreparation(pressed, agents.get(0), collObjs.get(0), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(0), collObjs.get(1), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(2), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(3), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(2), collObjs.get(4), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(2), collObjs.get(5), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(3), collObjs.get(6), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(3), collObjs.get(7), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(0), (Storage) locs.get(11),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(1), (Storage) locs.get(11),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(2), (Storage) locs.get(10),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(2), collObjs.get(3), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(3), collObjs.get(4), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(0), collObjs.get(5), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(6), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(7), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(2), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));

    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(0), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(1), (Storage) locs.get(7),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(2), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(3), (Storage) locs.get(8),
            rand.nextInt(20) + 1, prepDate));
    preps.add(createPreparation(pressed, agents.get(1), collObjs.get(4), (Storage) locs.get(9),
            rand.nextInt(20) + 1, prepDate));

    dataObjects.addAll(prepTypesForSaving);
    dataObjects.addAll(preps);

    persist(dataObjects);
    dataObjects.clear();

    commitTx();

    startTx();
    frame.setProcess(++createStep);

    ////////////////////////////////
    // accessions (accession agents)
    ////////////////////////////////
    log.info("Creating accessions and accession agents");
    frame.setDesc("Creating accessions...");

    calendar.set(2006, 10, 27, 23, 59, 59);
    Accession acc1 = createAccession(division, "gift", "complete",
            "2000-" + disciplineType.getAbbrev().substring(0, 2) + "-001",
            DateFormat.getInstance().format(calendar.getTime()), calendar, calendar);
    acc1.setText1(disciplineType.getTitle());
    acc1.setRepositoryAgreement(repoAg);

    Agent donor = agents.get(4);
    Agent receiver = agents.get(1);
    Agent reviewer = agents.get(2);

    List<AccessionAgent> accAgents = new Vector<AccessionAgent>();

    accAgents.add(createAccessionAgent("donor", donor, acc1, null));
    accAgents.add(createAccessionAgent("receiver", receiver, acc1, null));
    accAgents.add(createAccessionAgent("reviewer", reviewer, acc1, null));

    Accession acc2 = createAccession(division, "field_work", "inprocess",
            "2004-" + disciplineType.getAbbrev().substring(0, 2) + "-002",
            DateFormat.getInstance().format(calendar.getTime()), calendar, calendar);

    Agent donor2 = agents.get(5);
    Agent receiver2 = agents.get(3);
    Agent reviewer2 = agents.get(1);

    accAgents.add(createAccessionAgent("donor", donor2, acc2, null));
    accAgents.add(createAccessionAgent("receiver", receiver2, acc2, null));
    accAgents.add(createAccessionAgent("reviewer", reviewer2, acc2, null));

    dataObjects.add(acc1);
    dataObjects.add(acc2);
    dataObjects.addAll(accAgents);

    persist(dataObjects);
    dataObjects.clear();
    commitTx();

    startTx();
    frame.setProcess(++createStep);

    createLoanExamples(preps, agents, dataObjects);
    frame.setProcess(++createStep);

    persist(dataObjects);
    dataObjects.clear();

    persist(dataObjects);
    dataObjects.clear();

    frame.setProcess(++createStep);

    commitTx();

    frame.setProcess(++createStep);

    buildDarwinCoreSchema(discipline);

    // done
    log.info("Done creating " + disciplineType.getTitle() + " disciplineType database: "
            + disciplineType.getTitle());
    return dataObjects;
}

From source file:edu.ku.brc.specify.utilapps.BuildSampleDatabase.java

/**
 * Creates a single disciplineType collection.
 * @param disciplineName the name of the Discipline to use
 * @param disciplineName the disciplineType name
 * @return the entire list of DB object to be persisted
 *///from  w  ww.  j  av  a 2s. c  o  m
@SuppressWarnings("unchecked")
public Collection createFishCollection(final Discipline discipline, final SpecifyUser user,
        final Agent userAgent, final Division division, final Journal journal, final List<Object> taxa,
        final List<Object> geos, final String colPrefix, final String colName, final boolean isVoucherCol,
        final boolean doTissues, final CollectionChoice choice) {
    createStep = 0;
    frame.setProcess(0, 15);

    frame.setDesc("Creating Collection " + colName);

    startTx();

    Pair<AutoNumberingScheme, AutoNumberingScheme> pairANS = createAutoNumberingSchemes(choice);
    AutoNumberingScheme cns = pairANS.first;
    AutoNumberingScheme accessionNS = pairANS.second;

    persist(cns);
    persist(accessionNS);

    commitTx();

    startTx();

    ////////////////////////////////
    // Create Collection
    ////////////////////////////////
    log.info("Creating a Collection");
    Collection collection = createCollection(colPrefix, colName, choice.getCatalogNumberingFmtName(), cns,
            discipline, false);
    persist(collection);

    AppContextMgr.getInstance().setClassObject(Collection.class, collection);

    division.addReference(accessionNS, "numberingSchemes");
    persist(division);

    ////////////////////////////////
    // Default user groups and test user
    ////////////////////////////////
    Map<String, SpPrincipal> groupMap = DataBuilder.createStandardGroups(session, collection);

    // add the administrator as a Collections Manager in this group
    user.addUserToSpPrincipalGroup(groupMap.get(SpecifyUserTypes.UserType.Manager.toString()));
    persist(user);

    // Tester
    String userPrefix = (isVoucherCol) ? "" : "Tis";
    createAndAddTesterToCollection(session, userPrefix + "FishTester", "fishtester@brc.ku.edu",
            userPrefix + "FishTester", "", "Fish", "", "Tester", "", discipline, division, collection, groupMap,
            "Guest");

    commitTx();

    ////////////////////////////////
    // picklists
    ////////////////////////////////
    log.info("Creating picklists");

    createPickLists(session, null);
    BldrPickList colMethods = createPickLists(session, discipline);

    frame.setProcess(++createStep);

    startTx();

    //DBTableIdMgr schema = new DBTableIdMgr(false);
    //schema.initialize(new File(XMLHelper.getConfigDirPath("specify_datamodel.xml")));
    //loadSchemaLocalization(discipline, SpLocaleContainer, schema);
    //buildDarwinCoreSchema(discipline);

    AppContextMgr.getInstance().setClassObject(SpecifyUser.class, user);

    user.addReference(userAgent, "agents");

    persist(user);

    frame.setProcess(++createStep);

    Vector<Object> dataObjects = new Vector<Object>();

    standardQueries(dataObjects, userAgent);
    persist(dataObjects);
    dataObjects.clear();

    //startTx();
    persist(dataObjects);
    //commitTx();
    dataObjects.clear();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // localities
    ////////////////////////////////
    List<Locality> localities = new Vector<Locality>();

    String POINT = "Point";
    String LINE = "Line";
    String RECT = "Rectangle";

    Locality forestStream;
    Locality lake;
    Locality clintonLake;
    Locality farmpond;

    if (isVoucherCol) {
        log.info("Creating localities");
        forestStream = createLocality("Unnamed forest stream pond", (Geography) geos.get(12));
        localities.add(forestStream);
        globalLocalities.add(forestStream);
        forestStream.setLatLongType(POINT);
        forestStream.setOriginalLatLongUnit(0);
        forestStream.setLat1text("38.925467 deg N");
        forestStream.setLatitude1(new BigDecimal(38.925467));
        forestStream.setLong1text("94.984867 deg W");
        forestStream.setLongitude1(new BigDecimal(-94.984867));

        lake = createLocality("Deep, dark lake pond", (Geography) geos.get(17));
        localities.add(lake);
        globalLocalities.add(lake);
        lake.setLatLongType(RECT);
        lake.setOriginalLatLongUnit(1);
        lake.setLat1text("41.548842 deg N");
        lake.setLatitude1(new BigDecimal(41.548842));
        lake.setLong1text("93.732129 deg W");
        lake.setLongitude1(new BigDecimal(-93.732129));

        lake.setLat2text("41.642195 deg N");
        lake.setLatitude2(new BigDecimal(41.642195));
        lake.setLong2text("100.403180 deg W");
        lake.setLongitude2(new BigDecimal(-100.403180));

        Geography douglasKS = null;
        for (Object o : geos) {
            if (o instanceof Geography) {
                Geography g = (Geography) o;
                if (g.getFullName().indexOf("Douglas") == 0) {
                    douglasKS = g;
                }
            }
        }
        clintonLake = createLocality("Clinton Lake", douglasKS);
        localities.add(clintonLake);
        globalLocalities.add(clintonLake);

        farmpond = createLocality("Shoal Creek at Schermerhorn Park, S of Galena at Rt. 26",
                (Geography) geos.get(11));
        localities.add(farmpond);
        globalLocalities.add(farmpond);

        farmpond.setLatLongType(LINE);
        farmpond.setOriginalLatLongUnit(2);
        farmpond.setLat1text("41.642187 deg N");
        farmpond.setLatitude1(new BigDecimal(41.642187));
        farmpond.setLong1text("100.403163 deg W");
        farmpond.setLongitude1(new BigDecimal(-100.403163));

        farmpond.setLat2text("49.647435 deg N");
        farmpond.setLatitude2(new BigDecimal(49.647435));
        farmpond.setLong2text("-55.112163 deg W");
        farmpond.setLongitude2(new BigDecimal(-55.112163));

        persist(forestStream);
        persist(lake);
        persist(farmpond);
        persist(clintonLake);
    } else {
        forestStream = globalLocalities.get(0);
        lake = globalLocalities.get(1);
        farmpond = globalLocalities.get(2);
        localities.addAll(globalLocalities);
    }

    frame.setProcess(++createStep);

    ////////////////////////////////
    // agents and addresses
    ////////////////////////////////
    log.info("Creating agents and addresses");
    List<Agent> agents = new Vector<Agent>();
    Agent johnByrn = null;
    Agent ku = new Agent();

    if (isVoucherCol) {
        johnByrn = createAgent("mr", "John", "D", "Byrn", "jb", "jb@net.edu");
        agents.add(createAgent("mr", "David", "D", "Smith", "ds", "ds@whitehouse.gov"));
        agents.add(createAgent("mr", "Robert", "H", "Burk", "rb", "beach@net.edu"));
        agents.add(createAgent("mrs", "Margaret", "H", "Johnson", "jm", "jm@net.edu"));
        agents.add(createAgent("mr", "Kip", "C", "Spencer", "kcs", "kip@ku.edu"));
        agents.add(johnByrn);
        agents.add(createAgent("sir", "Dudley", "X", "Thompson", "dxt", ""));
        agents.add(createAgent("mr", "Joe", "A", "Campbell", "jb", ""));
        agents.add(createAgent("mr", "Joe", "A", "Tester", "jb", ""));
        agents.add(createAgent("mr", "Mitch", "A", "Smyth", "mas", "mas@ku.edu"));
        agents.add(userAgent);

        ku.initialize();
        ku.setAbbreviation("KU");
        ku.setAgentType(Agent.ORG);
        ku.setLastName("University of Kansas");
        ku.setEmail("webadmin@ku.edu");
        ku.setTimestampCreated(new Timestamp(System.currentTimeMillis()));
        ku.setDivision(AppContextMgr.getInstance().getClassObject(Division.class));

        agents.add(ku);
        agents.get(0).setOrganization(ku);
        agents.get(1).setOrganization(ku);
        agents.get(2).setOrganization(ku);
        agents.get(3).setOrganization(ku);
        agents.get(8).setOrganization(ku);

        Agent otherAgent = new Agent();
        otherAgent.initialize();
        otherAgent.setAbbreviation("O");
        otherAgent.setAgentType(Agent.OTHER);
        otherAgent.setLastName("The Other Guys");
        otherAgent.setEmail("other@other.com");
        otherAgent.setTimestampCreated(new Timestamp(System.currentTimeMillis()));
        otherAgent.setDivision(AppContextMgr.getInstance().getClassObject(Division.class));
        agents.add(otherAgent);

        List<GroupPerson> gpList = new ArrayList<GroupPerson>();
        if (true) {
            startTx();
            Agent gm1 = createAgent("mr", "John", "A", "Lyon", "jal", "jal@group.edu");
            Agent gm2 = createAgent("mr", "Dave", "D", "Jones", "ddj", "ddj@group.edu");
            persist(gm1);
            persist(gm2);
            commitTx();

            //Discipline dsp = AppContextMgr.getInstance().getClassObject(Discipline.class);

            Agent groupAgent = new Agent();
            groupAgent.initialize();
            groupAgent.setAbbreviation("GRP");
            groupAgent.setAgentType(Agent.GROUP);
            groupAgent.setLastName("The Group");
            groupAgent.setEmail("group@group.com");
            groupAgent.setTimestampCreated(new Timestamp(System.currentTimeMillis()));
            groupAgent.setDivision(AppContextMgr.getInstance().getClassObject(Division.class));

            agents.add(groupAgent);

            gpList.add(createGroupPerson(groupAgent, gm1, 0, division));
            gpList.add(createGroupPerson(groupAgent, gm2, 1, division));
        }

        globalAgents.addAll(agents);

        List<AgentVariant> agentVariants = new Vector<AgentVariant>();
        agentVariants.add(createAgentVariant(AgentVariant.VARIANT, "John Variant #1", johnByrn));
        agentVariants.add(createAgentVariant(AgentVariant.VERNACULAR, "John VERNACULAR #1", johnByrn));

        List<Address> addrs = new Vector<Address>();
        addrs.add(createAddress(agents.get(1), "1600 Pennsylvania Avenue NW", null, "Washington", "DC", "USA",
                "20500", 0));
        addrs.add(createAddress(agents.get(1), "??? Mississippi", null, "Lawrence", "KS", "USA", "66045", 1));
        addrs.add(createAddress(agents.get(2), "1 Main St", "", "Lenexa", "KS", "USA", "66071"));
        addrs.add(createAddress(agents.get(3), "13355 Inverness", "Bldg #3", "Lawrence", "KS", "USA", "66047"));
        addrs.add(createAddress(agents.get(4), "Natural History Museum", "Cromwell Rd", "London", null, "UK",
                "SW7 5BD"));
        addrs.add(createAddress(agents.get(6), "1212 Apple Street", null, "Chicago", "IL", "USA", "01010"));
        addrs.add(createAddress(agents.get(8), "11911 Oak Ln", null, "Orion", "KS", "USA", "66061"));
        addrs.add(createAddress(ku, null, null, "Lawrence", "KS", "USA", "66045"));
        addrs.add(createAddress(userAgent, "1214 East Street", null, "Grinnell", "IA", "USA", "56060"));

        persist(agents);
        persist(agentVariants);
        persist(gpList);

    } else {
        agents.addAll(globalAgents);
        johnByrn = agents.get(4);
        ku = agents.get(10);
    }

    frame.setProcess(++createStep);

    ////////////////////////////////
    // collecting events (collectors, collecting trip)
    ////////////////////////////////

    log.info("Creating collecting events, collectors and a collecting trip");
    Collector collectorMitch = createCollector(agents.get(8), 2);
    Collector collectorJim = createCollector(agents.get(1), 1);
    calendar.set(1993, 3, 19, 11, 56, 00);
    String stationFieldNumber = String.format(STATION_FIELD_FORMAT, stationFieldNumberCounter++);
    CollectingEvent ce1 = createCollectingEvent(forestStream, calendar, stationFieldNumber,
            new Collector[] { collectorMitch, collectorJim });
    ce1.setStartDateVerbatim("19 Mar 1993, 11:56 AM");
    calendar.set(1993, 3, 19, 13, 03, 00);
    ce1.setEndDate(calendar);
    ce1.setEndDateVerbatim("19 Mar 1993, 1:03 PM");
    ce1.setMethod(colMethods.getItem(1).getValue());

    AttributeDef cevAttrDef = createAttributeDef(AttributeIFace.FieldType.StringType, "ParkName", discipline,
            null);//meg added cod

    //startTx();
    persist(cevAttrDef);
    //commitTx();

    CollectingEventAttr cevAttr = createCollectingEventAttr(ce1, cevAttrDef, "Sleepy Hollow", null);

    Collector collectorMeg = createCollector(agents.get(2), 1);
    Collector collectorRod = createCollector(agents.get(3), 2);
    calendar.set(1993, 3, 20, 06, 12, 00);
    stationFieldNumber = String.format(STATION_FIELD_FORMAT, stationFieldNumberCounter++);
    CollectingEvent ce2 = createCollectingEvent(farmpond, calendar, stationFieldNumber,
            new Collector[] { collectorMeg, collectorRod });
    ce2.setStartDateVerbatim("20 Mar 1993, 6:12 AM");
    calendar.set(1993, 3, 20, 07, 31, 00);
    ce2.setEndDate(calendar);
    ce2.setEndDateVerbatim("20 Mar 1993, 7:31 AM");
    ce2.setMethod(colMethods.getItem(2).getValue());

    CollectingTrip trip = createCollectingTrip("My Collecint Trip", "Sample collecting trip",
            new CollectingEvent[] { ce1, ce2 });

    int[] mn = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    Vector<CollectingEvent> ceList = new Vector<CollectingEvent>();
    boolean oldWay = false;
    if (!oldWay) {
        int monInx = rand.nextInt(12);
        int dayInx = rand.nextInt(mn[monInx]);
        calendar.set(1990 + rand.nextInt(15), monInx + 1, dayInx + 1, rand.nextInt(24), rand.nextInt(60),
                rand.nextInt(60));
        stationFieldNumber = String.format(STATION_FIELD_FORMAT, stationFieldNumberCounter++);

        Collector collector = null;
        int coltrInx = rand.nextInt(4);
        switch (coltrInx) {
        case 0:
            collector = collectorMitch;
            break;
        case 1:
            collector = collectorJim;
            break;
        case 2:
            collector = collectorMeg;
            break;
        case 3:
            collector = collectorRod;
            break;
        default:
            collector = collectorRod;
            break;
        }
        Locality loc = globalLocalities.get(rand.nextInt(globalLocalities.size()));
        CollectingEvent ce = createCollectingEvent(loc, calendar, stationFieldNumber,
                new Collector[] { collector });
        //ce1.setStartDateVerbatim("19 Mar 1993, 11:56 AM");
        ceList.add(ce);
        dataObjects.add(ce);
    }

    dataObjects.add(trip);
    dataObjects.add(ce1);
    dataObjects.add(cevAttr);
    dataObjects.add(ce2);
    dataObjects.add(collectorMitch);
    dataObjects.add(collectorJim);
    dataObjects.add(collectorMeg);
    dataObjects.add(collectorRod);

    //startTx();
    persist(dataObjects);
    //commitTx();
    dataObjects.clear();

    ////////////////////////////////
    // permit
    ////////////////////////////////
    log.info("Creating a permit");
    Calendar issuedDate = Calendar.getInstance();
    issuedDate.set(1993, 1, 12);
    Calendar startDate = Calendar.getInstance();
    startDate.set(1993, 2, 1);
    Calendar endDate = Calendar.getInstance();
    endDate.set(1993, 5, 30);
    Permit permit = createPermit("1993-FISH-0001", "US Dept Wildlife", issuedDate, startDate, endDate, null);
    permit.setIssuedTo(ku);
    permit.setIssuedBy(agents.get(4));
    dataObjects.add(permit);

    log.info("Creating a repository agreement");
    RepositoryAgreement repoAg = new RepositoryAgreement();
    repoAg.initialize();
    repoAg.setDivision(division);
    repoAg.setRepositoryAgreementNumber("KU-1992-01");
    repoAg.setOriginator(ku);
    Calendar received = Calendar.getInstance();
    received.set(1992, 2, 10);
    repoAg.setDateReceived(received);
    Calendar repoEndDate = Calendar.getInstance();
    received.set(2010, 2, 9);
    repoAg.setEndDate(repoEndDate);
    dataObjects.add(repoAg);

    //startTx();
    persist(dataObjects);
    //commitTx();
    dataObjects.clear();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // collection objects
    ////////////////////////////////
    log.info("Creating collection objects");

    List<DNASequence> dnaObjs = new Vector<DNASequence>();
    List<CollectionObject> collObjs = new Vector<CollectionObject>();
    Collection col = collection;

    Calendar[] catDates = new Calendar[oldWay ? 8 : 50];
    for (int i = 0; i < catDates.length; i++) {
        catDates[i] = Calendar.getInstance();
        catDates[i].set(catDates[i].get(Calendar.YEAR), 01, 12 + i);
    }

    String prefix = "000000";
    if (oldWay) {
        collObjs.add(createCollectionObject(prefix + "100", "RSC100", agents.get(0), col, 3, ce1, catDates[0],
                "BuildSampleDatabase"));
        collObjs.add(createCollectionObject(prefix + "101", "RSC101", agents.get(0), col, 2, ce1, catDates[1],
                "BuildSampleDatabase"));
        collObjs.add(createCollectionObject(prefix + "102", "RSC102", agents.get(1), col, 7, ce1, catDates[2],
                "BuildSampleDatabase"));
        collObjs.add(createCollectionObject(prefix + "103", "RSC103", agents.get(1), col, 12, ce1, catDates[3],
                "BuildSampleDatabase"));
        collObjs.add(createCollectionObject(prefix + "104", "RSC104", agents.get(2), col, 8, ce2, catDates[4],
                "BuildSampleDatabase"));
        collObjs.add(createCollectionObject(prefix + "105", "RSC105", agents.get(2), col, 1, ce2, catDates[5],
                "BuildSampleDatabase"));
        collObjs.add(createCollectionObject(prefix + "106", "RSC106", agents.get(2), col, 1, ce2, catDates[6],
                "BuildSampleDatabase"));
        collObjs.add(createCollectionObject(prefix + "107", "RSC107", agents.get(3), col, 1, ce2, catDates[7],
                "BuildSampleDatabase"));
    } else {
        for (int i = 0; i < catDates.length; i++) {
            Integer catNum = i + 100;
            int agentInx = rand.nextInt(agents.size());
            CollectingEvent ce = ceList.get(rand.nextInt(ceList.size()));
            collObjs.add(createCollectionObject(prefix + catNum, "RSC" + catNum, agents.get(agentInx), col,
                    rand.nextInt(12) + 1, ce, catDates[i], "BuildSampleDatabase"));
        }

        /*
                    Comp. A :   148      
                    Comp. G :   131      
                    Comp. C :   199      
                    Comp. T :   174      
                    Ambiguous :     0    
                123456789012345678901234567890123456789012345678901234567890123456789012345
                CCTGTATTTAGTATTTGGTGCCTGAGCAGGCATAGTCGGCACAGCCCTCAGCCTTCTGATCCGTGCCGAACTGAG
                CCAACCCGGTGCCCTGCTTGGCGATGATCAGATCTACAATGTTATCGTCACAGCCCACGCCTTTGTCATGATTTT
                CTTTATAGTAATACCCATCATAATTGGCGGATTCGGAAACTGACTGGTCCCCCTAATAATTGGGGCCCCAGACAT
                GGCATTTCCTCGCATGAACAATATGAGCTTCTGACTCCTACCCCCATCCTTCCTACTCCTTTTAGCCTCCTCTGG
                GGTAGAGGCCGGAGCCGGCACAGGGTGAACTGTTTACCCCCCACTGGCGGGAAACCTGGCCCATGCAGGAGCCTC
                TGTAGACCTAACCATTTTCTCCCTTCACCTGGCTGGGGTTTCGTCCATTTTGGGGGCTATTAATTTTATTACCAC
                CATTATTAACATGAAACCCCCCGCAGTATCCCAATATCAGACACCTCTATTTGTGTGATCTGTATTAATCACGGC
                CGTACTTCTCCTACTATCACTGCCAGTGCTAGCTGCAGGGATCACAATGCTCCTAACAGACCGAAATTTAAACAC
                CACCTTCTTTGACCCAGCCGGAGGAGGAGACCCCATCCTCTACCAACACCTA
                */
        char[] syms = { 'A', 'C', 'T', 'G', };

        for (int i = 0; i < catDates.length; i++) {
            int monInx = rand.nextInt(12);
            int dayInx = rand.nextInt(mn[monInx]);
            Calendar cal = Calendar.getInstance();
            cal.set(2006 + rand.nextInt(3), monInx + 1, dayInx + 1, rand.nextInt(24), rand.nextInt(60),
                    rand.nextInt(60));

            DNASequence dna = new DNASequence();
            dna.initialize();

            //dna.setSeqDate(cal); //moved to DNASequencingRun
            dna.setCollectionMemberId(collObjs.get(i).getCollectionMemberId());
            dna.setCollectionObject(collObjs.get(i));
            //dna.setGeneName("COI5'");
            int agentInx = rand.nextInt(agents.size());
            dna.setCreatedByAgent(agents.get(agentInx));
            dna.setSequencer(agents.get(agentInx));
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < ((8 * 75) + 52); j++) {
                sb.append(syms[rand.nextInt(syms.length)]);
            }
            dna.setGeneSequence(sb.toString());
            //dna.setPcrPrimerFwd("C_VF1LFt1"); //moved to DNASequencingRun (sort of)
            //dna.setPcrPrimerRev("C_VR1LRt1"); //moved to DNASequencingRun (sort of)
            //dna.setProcessIdentifier("M13R"); //moved to DNASequencingRun (sort of)
            if (rand.nextInt(3) < 2) {
                //dna.setBarCodeIdent(String.format("NOSMF%03d-%d02", rand.nextInt(1000), i)); //moved to DNASequencingRun (sort of)
                Calendar submDate = (Calendar) cal.clone();
                submDate.add(Calendar.DAY_OF_MONTH, 12);
                //dna.setSubmissionDate(submDate);
            }
            dnaObjs.add(dna);
        }
    }
    AttributeDef colObjAttrDef = createAttributeDef(AttributeIFace.FieldType.StringType, "MoonPhase",
            discipline, null);//meg added cod
    colObjAttrDef.setDiscipline(discipline);
    discipline.getAttributeDefs().add(colObjAttrDef);

    CollectionObjectAttr colObjAttr = createCollectionObjectAttr(collObjs.get(0), colObjAttrDef, "Full", null);
    dataObjects.add(colObjAttrDef);
    dataObjects.addAll(collObjs);
    dataObjects.addAll(dnaObjs);
    dataObjects.add(colObjAttr);

    //startTx();
    persist(dataObjects);
    //commitTx();
    dataObjects.clear();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // determinations (determination status)
    ////////////////////////////////
    log.info("Creating determinations");

    List<Determination> determs = new Vector<Determination>();
    Calendar recent = Calendar.getInstance();
    recent.set(2006, 10, 27, 13, 44, 00);
    Calendar longAgo = Calendar.getInstance();
    longAgo.set(1976, 01, 29, 8, 12, 00);
    Calendar whileBack = Calendar.getInstance();
    whileBack.set(2002, 7, 4, 9, 33, 12);

    int baseInx = 41 - (doShallowTaxonTree ? 30 : 0);
    if (oldWay) {
        determs.add(createDetermination(collObjs.get(0), agents.get(0), (Taxon) taxa.get(baseInx + 1), true,
                recent));
        determs.add(createDetermination(collObjs.get(1), agents.get(0), (Taxon) taxa.get(baseInx + 2), true,
                recent));
        determs.add(createDetermination(collObjs.get(2), agents.get(0), (Taxon) taxa.get(baseInx + 3), true,
                recent));
        determs.add(createDetermination(collObjs.get(3), agents.get(0), (Taxon) taxa.get(baseInx + 4), true,
                recent));
        determs.add(createDetermination(collObjs.get(4), agents.get(0), (Taxon) taxa.get(baseInx + 5), true,
                recent));
        determs.add(createDetermination(collObjs.get(5), agents.get(0), (Taxon) taxa.get(baseInx + 6), true,
                recent));
        determs.add(createDetermination(collObjs.get(6), agents.get(3), (Taxon) taxa.get(baseInx + 7), true,
                recent));
        determs.add(createDetermination(collObjs.get(7), agents.get(4), (Taxon) taxa.get(baseInx + 8), true,
                recent));

        determs.add(
                createDetermination(collObjs.get(0), agents.get(0), (Taxon) taxa.get(baseInx), false, longAgo));
        determs.add(createDetermination(collObjs.get(1), agents.get(1), (Taxon) taxa.get(baseInx + 7), false,
                whileBack));
        determs.add(createDetermination(collObjs.get(2), agents.get(1), (Taxon) taxa.get(baseInx + 9), false,
                whileBack));
        determs.add(createDetermination(collObjs.get(3), agents.get(2), (Taxon) taxa.get(baseInx + 10), false,
                whileBack));
        determs.add(createDetermination(collObjs.get(4), agents.get(2), (Taxon) taxa.get(baseInx + 10), false,
                whileBack));
        determs.add(createDetermination(collObjs.get(4), agents.get(3), (Taxon) taxa.get(baseInx + 13), false,
                longAgo));
        determs.add(createDetermination(collObjs.get(4), agents.get(4), (Taxon) taxa.get(baseInx + 12), false,
                longAgo));
        determs.get(13).setRemarks("This determination is totally wrong.  What a foolish determination.");
    } else {
        for (CollectionObject co : collObjs) {
            determs.add(createDetermination(co, agents.get(0), (Taxon) taxa.get(baseInx + rand.nextInt(13)),
                    true, recent));
        }
    }

    //startTx();
    persist(determs);
    //commitTx();
    dataObjects.clear();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // preparations (prep types)
    ////////////////////////////////
    log.info("Creating preparations");

    Vector<PrepType> prepTypesForSaving = loadPrepTypes(discipline.getType());
    Vector<PrepType> pt = new Vector<PrepType>();

    if (doTissues) {
        pt.clear();
        PrepType tissuePT = createPrepType(collection, "Tissue");
        for (int i = 0; i < prepTypesForSaving.size(); i++) {
            pt.add(tissuePT);
        }
        prepTypesForSaving.clear();
        prepTypesForSaving.add(tissuePT);

    } else {
        pt.addAll(prepTypesForSaving);
    }

    List<Preparation> preps = new Vector<Preparation>();
    Calendar prepDate = Calendar.getInstance();
    if (oldWay) {
        preps.add(createPreparation(pt.get(0), agents.get(0), collObjs.get(0), (Storage) locs.get(7),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(0), agents.get(0), collObjs.get(1), (Storage) locs.get(7),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(0), agents.get(1), collObjs.get(2), (Storage) locs.get(7),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(0), agents.get(1), collObjs.get(3), (Storage) locs.get(7),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(0), agents.get(2), collObjs.get(4), (Storage) locs.get(8),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(0), agents.get(2), collObjs.get(5), (Storage) locs.get(8),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(0), agents.get(3), collObjs.get(6), (Storage) locs.get(8),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(0), agents.get(3), collObjs.get(7), (Storage) locs.get(8),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(1), agents.get(1), collObjs.get(0), (Storage) locs.get(11),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(1), agents.get(1), collObjs.get(1), (Storage) locs.get(11),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(1), agents.get(1), collObjs.get(2), (Storage) locs.get(10),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(1), agents.get(2), collObjs.get(3), (Storage) locs.get(9),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(1), agents.get(3), collObjs.get(4), (Storage) locs.get(9),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(1), agents.get(0), collObjs.get(5), (Storage) locs.get(9),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(2), agents.get(1), collObjs.get(6), (Storage) locs.get(9),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(2), agents.get(1), collObjs.get(7), (Storage) locs.get(9),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(2), agents.get(1), collObjs.get(2), (Storage) locs.get(8),
                rand.nextInt(20) + 1, prepDate));

        preps.add(createPreparation(pt.get(3), agents.get(1), collObjs.get(0), (Storage) locs.get(7),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(3), agents.get(1), collObjs.get(1), (Storage) locs.get(7),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(3), agents.get(1), collObjs.get(2), (Storage) locs.get(8),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(3), agents.get(1), collObjs.get(3), (Storage) locs.get(8),
                rand.nextInt(20) + 1, prepDate));
        preps.add(createPreparation(pt.get(3), agents.get(1), collObjs.get(4), (Storage) locs.get(9),
                rand.nextInt(20) + 1, prepDate));
    } else {
        for (CollectionObject co : collObjs) {
            preps.add(createPreparation(pt.get(0), agents.get(rand.nextInt(4)), co,
                    (Storage) locs.get(rand.nextInt(6) + 7), rand.nextInt(20) + 1, prepDate));
        }
    }

    dataObjects.add(collection);
    dataObjects.addAll(prepTypesForSaving);
    dataObjects.addAll(preps);

    //startTx();
    persist(dataObjects);
    //commitTx();
    dataObjects.clear();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // accessions (accession agents)
    ////////////////////////////////
    log.info("Creating accessions and accession agents");
    calendar.set(2006, 10, 27, 23, 59, 59);
    int yr = 2000 + (int) (rand.nextDouble() * 7);
    Accession acc1 = createAccession(division, "gift", "complete", yr + "-IC-001",
            DateFormat.getInstance().format(calendar.getTime()), calendar, calendar);
    acc1.setText1("Ichthyology");
    acc1.setRepositoryAgreement(repoAg);

    Agent donor = agents.get(4);
    Agent receiver = agents.get(1);
    Agent reviewer = agents.get(2);

    List<AccessionAgent> accAgents = new Vector<AccessionAgent>();

    accAgents.add(createAccessionAgent("donor", donor, acc1, null));
    accAgents.add(createAccessionAgent("receiver", receiver, acc1, null));
    accAgents.add(createAccessionAgent("reviewer", reviewer, acc1, null));

    Accession acc2 = createAccession(division, "field_work", "inprocess", yr + "-IC-002",
            DateFormat.getInstance().format(calendar.getTime()), calendar, calendar);

    Agent donor2 = agents.get(5);
    Agent receiver2 = agents.get(3);
    Agent reviewer2 = agents.get(1);

    accAgents.add(createAccessionAgent("donor", donor2, acc2, null));
    accAgents.add(createAccessionAgent("receiver", receiver2, acc2, null));
    accAgents.add(createAccessionAgent("reviewer", reviewer2, acc2, null));

    dataObjects.add(acc1);
    dataObjects.add(acc2);
    dataObjects.addAll(accAgents);

    //startTx();
    persist(dataObjects);
    //commitTx();
    dataObjects.clear();

    frame.setProcess(++createStep);

    createLoanExamples(preps, agents, dataObjects);
    frame.setProcess(++createStep);

    //startTx();
    persist(dataObjects);
    //commitTx();
    dataObjects.clear();

    /*if (false)
    {
    TaxonCitation taxonCitation = new TaxonCitation();
    taxonCitation.initialize();
    Taxon taxon11 = (Taxon)taxa.get(11);
    taxonCitation.setTaxon(taxon11);
    taxonCitation.setReferenceWork(rwList.get(0));
    rwList.get(0).addTaxonCitations(taxonCitation);
    taxon11.getTaxonCitations().add(taxonCitation);
    dataObjects.add(taxonCitation);
            
            
    LocalityCitation localityCitation = new LocalityCitation();
    localityCitation.initialize();
    localityCitation.setLocality(ce1.getLocality());
    ce1.getLocality().getLocalityCitations().add(localityCitation);
    localityCitation.setReferenceWork(rwList.get(1));
    rwList.get(1).addLocalityCitations(localityCitation);
    dataObjects.add(localityCitation);
    }*/

    ////////////////////////////////
    // Workbench
    ////////////////////////////////

    // setup a template and its mapping items
    String name = discipline.getName() + " DataSet";
    WorkbenchTemplate wbTemplate = createWorkbenchTemplate(user, name, "These are the remarks");
    WorkbenchTemplateMappingItem wbtmi0 = createWorkbenchMappingItem("CollectionObject", 1, "fieldNumber",
            "Field Number", 25, 0, 0, wbTemplate);
    WorkbenchTemplateMappingItem wbtmi1 = createWorkbenchMappingItem("CollectionObject", 1, "catalogedDate",
            "Cataloged Date", 25, 1, 1, wbTemplate);
    WorkbenchTemplateMappingItem wbtmi2 = createWorkbenchMappingItem("CollectionObject", 1, "catalogNumber",
            "Catalog Number", 25, 2, 2, wbTemplate);
    WorkbenchTemplateMappingItem wbtmi3 = createWorkbenchMappingItem("CollectionObject", 1,
            "collectionObjectYesNo1", "Yes/No", 8, 3, 3, wbTemplate);

    dataObjects.add(wbTemplate);
    dataObjects.add(wbtmi0);
    dataObjects.add(wbtmi1);
    dataObjects.add(wbtmi2);
    dataObjects.add(wbtmi3);

    // setup a workbench based on that template
    Workbench workBench = createWorkbench(user, name, "These are the remarks", "field_notebook.cvs",
            wbTemplate);
    dataObjects.add(workBench);

    // create a bunch of rows for the workbench
    for (int i = 1; i <= 14; ++i) {
        WorkbenchRow wbRow = workBench.addRow();
        WorkbenchDataItem wbdi0 = createWorkbenchDataItem(wbRow, "RS-10" + i, 0);

        // just to make the dates look a little random
        int date = (i * 547) % 31 + 1;
        String dateStr = "0" + Integer.toString(date);
        dateStr = dateStr.substring(dateStr.length() - 2);
        WorkbenchDataItem wbdi1 = createWorkbenchDataItem(wbRow, "03/" + dateStr + "/2007", 1);
        WorkbenchDataItem wbdi2 = createWorkbenchDataItem(wbRow, "CN-10" + i, 2);

        String boolValAsStr = null;
        switch (i % 3) {
        case 0: {
            boolValAsStr = "true";
            break;
        }
        case 1: {
            boolValAsStr = "false";
            break;
        }
        case 2: {
            boolValAsStr = "";
            break;
        }
        }
        boolValAsStr = "";
        WorkbenchDataItem wbdi3 = createWorkbenchDataItem(wbRow, boolValAsStr, 3);

        WorkbenchRowImage wbRowImage = null;

        File f = new File(getDefaultWorkingPath() + File.separator + "demo_files" + File.separator + "card" + i
                + (i == 2 ? ".png" : ".jpg"));
        if (f.exists()) {
            try {
                int imageIndex = wbRow.addImage(f);
                if (imageIndex > -1) {
                    wbRowImage = wbRow.getRowImage(imageIndex);
                }
            } catch (IOException e) {
                String msg = getResourceString("WB_IMG_ERR_LOAD");
                getStatusBar().setErrorMessage(msg, e);
                log.error(msg, e);
            }
        }

        dataObjects.add(wbRow);
        dataObjects.add(wbdi0);
        dataObjects.add(wbdi1);
        dataObjects.add(wbdi2);
        if (wbRowImage != null) {
            dataObjects.add(wbRowImage);
        }

        // since some of these values will be "", the data item might be null
        if (wbdi3 != null) {
            dataObjects.add(wbdi3);
        }
    }

    //        // create a workbench that uses the old, single-image capabilities
    //        Workbench         workBench2  = createWorkbench(user, name + " (pre-conversion)", "These are the remarks", "field_notebook.cvs", wbTemplate);
    //        dataObjects.add(workBench2);
    //
    //        // create a bunch of rows for the workbench
    //        for (int i = 1; i <= 14; ++i)
    //        {
    //            WorkbenchRow wbRow = workBench2.addRow();
    //            WorkbenchDataItem wbdi0 = createWorkbenchDataItem(wbRow, "RS-10" + i, 0);
    //            
    //            // just to make the dates look a little random
    //            int date = (i*547) % 31 + 1;
    //            String dateStr = "0" + Integer.toString(date);
    //            dateStr = dateStr.substring(dateStr.length()-2);
    //            WorkbenchDataItem wbdi1 = createWorkbenchDataItem(wbRow, "03/" + dateStr + "/2007", 1);
    //            WorkbenchDataItem wbdi2 = createWorkbenchDataItem(wbRow, "CN-10" + i, 2);
    //            
    //            String boolValAsStr = null;
    //            switch (i % 3)
    //            {
    //                case 0:
    //                {
    //                    boolValAsStr = "true";
    //                    break;
    //                }
    //                case 1:
    //                {
    //                    boolValAsStr = "false";
    //                    break;
    //                }
    //                case 2:
    //                {
    //                    boolValAsStr = "";
    //                    break;
    //                }
    //            }
    //            boolValAsStr = "";
    //            WorkbenchDataItem wbdi3 = createWorkbenchDataItem(wbRow, boolValAsStr, 3);
    //            
    //            WorkbenchRowImage wbRowImage = null;
    //            
    //            File f = new File("demo_files" + File.separator + "card" + i + (i == 2 ? ".png" : ".jpg"));
    //            if (f.exists())
    //            {
    //                try
    //                {
    //                    // NOTE: this is not scaling the images to the proper sizes.  Since this is just sample DB/test code, this isn't a problem.
    //                    byte[] imageData = FileUtils.readFileToByteArray(f);
    //                    wbRow.setCardImageData(imageData);
    //                    wbRow.setCardImageFullPath(f.getAbsolutePath());
    //                }
    //                catch (IOException e)
    //                {
    //                    log.error("Unable to add card image to workbench row", e);
    //                }
    //            }
    //
    //            dataObjects.add(wbRow);
    //            dataObjects.add(wbdi0);
    //            dataObjects.add(wbdi1);
    //            dataObjects.add(wbdi2);
    //            if (wbRowImage != null)
    //            {
    //                dataObjects.add(wbRowImage);
    //            }
    //            
    //            // since some of these values will be "", the data item might be null
    //            if (wbdi3 != null)
    //            {
    //                dataObjects.add(wbdi3);
    //            }
    //        }

    startTx();
    persist(dataObjects);
    commitTx();

    dataObjects.clear();

    frame.setProcess(++createStep);

    ////////////////////////////////
    // attachments (attachment metadata)
    ////////////////////////////////
    startTx();

    log.info("Creating attachments and attachment metadata");
    try {
        String attachmentFilesLoc = getDefaultWorkingPath() + File.separator + "demo_files" + File.separator;

        //                String bigEyeFilePath = attachmentFilesLoc + "bigeye.jpg";
        //                Attachment bigEye = createAttachment(bigEyeFilePath, "image/jpeg", 0);
        //                bigEye.setLoan(closedLoan);

        String[] names = { "Beach", "Smyth", "Spears", "Kumin", "Bentley" };
        String[] photos = { "beach.jpg", "rod.jpg", "rod.jpg", "meg.jpg", "andy.jpg" };
        for (Agent agent : agents) {
            for (int i = 0; i < names.length; i++) {
                if (agent.getLastName() != null && agent.getLastName().startsWith(names[i])) {
                    String photoPath = attachmentFilesLoc + photos[i];

                    File file = new File(attachmentFilesLoc + photos[i]);
                    if (!file.exists()) {
                        continue;
                    }

                    // create the attachment record
                    Attachment photoAttachment = createAttachment(photoPath, "image/jpeg",
                            Agent.getClassTableId());
                    dataObjects.add(photoAttachment);

                    // link the attachment to the agent
                    AgentAttachment agentAttach = new AgentAttachment();
                    agentAttach.initialize();
                    agentAttach.setAgent(agent);
                    agentAttach.setAttachment(photoAttachment);
                    agentAttach.setOrderIndex(0);
                    dataObjects.add(agentAttach);

                    // add some metadata to the attachment record
                    AttachmentMetadata copyrightData = new AttachmentMetadata();
                    copyrightData.initialize();
                    copyrightData.setName("Copyright");
                    copyrightData.setValue("2008");
                    photoAttachment.getMetadata().add(copyrightData);
                    copyrightData.setAttachment(photoAttachment);
                    dataObjects.add(copyrightData);

                    AttachmentMetadata defPhotoIndicator = new AttachmentMetadata();
                    defPhotoIndicator.initialize();
                    defPhotoIndicator.setName("Default Photo");
                    defPhotoIndicator.setValue("yes");
                    photoAttachment.getMetadata().add(defPhotoIndicator);
                    defPhotoIndicator.setAttachment(photoAttachment);
                    dataObjects.add(defPhotoIndicator);

                    // store the actual file into the attachment storage system
                    AttachmentUtils.getAttachmentManager().setStorageLocationIntoAttachment(photoAttachment,
                            true);
                    photoAttachment.storeFile(false); // false means do not display an error dialog
                }
            }
        }

        //                String giftPdfPath = attachmentFilesLoc + "2004-18.pdf";
        //                Attachment giftPDF = createAttachment(giftPdfPath, "application/pdf", 0);
        //                giftPDF.setLoan(closedLoan);
        //                
        //                String accessionPdfPath = attachmentFilesLoc + "Seychelles.pdf";
        //                Attachment accPDF = createAttachment(accessionPdfPath, "application/pdf", 0);
        //                // TODO: change this to setAccession()
        //                accPDF.setPermit(permit);
        //                
        //                String sharkVideoPath = attachmentFilesLoc + "shark5.mpg";
        //                Attachment sharkVideo = createAttachment(sharkVideoPath, "video/mpeg4", 0);
        //                sharkVideo.setLoan(closedLoan);
        //    
        //                Attachment sharkVideo2 = createAttachment(sharkVideoPath, "video/mpeg4", 0);
        //                sharkVideo2.setCollectingEvent(ce1);
        //    
        //                String beakerPath = attachmentFilesLoc + "beaker.jpg";
        //                Attachment beakerAsBeach = createAttachment(beakerPath, "image/jpg", 1);
        //                beakerAsBeach.setAgent(agents.get(1));
        //                
        //                dataObjects.add(bigEye);
        //                dataObjects.add(giftPDF);
        //                dataObjects.add(accPDF);
        //                dataObjects.add(sharkVideo);
        //                dataObjects.add(sharkVideo2);
        //                dataObjects.add(beakerAsBeach);
    } catch (Exception e) {
        log.error("Could not create attachments", e);
    }

    addConservatorData(agents, collObjs);

    commitTx();

    frame.setProcess(++createStep);

    if (true) {
        List<Taxon> taxa2 = session.createQuery("SELECT t FROM Taxon t WHERE t.name = 'Ammocrypta'").list();
        List<ReferenceWork> rwList = new Vector<ReferenceWork>();

        startTx();
        rwList.addAll(journal.getReferenceWorks());

        TaxonCitation taxonCitation = new TaxonCitation();
        taxonCitation.initialize();
        Taxon ammocrypta = taxa2.get(0);
        taxonCitation.setTaxon(ammocrypta);
        taxonCitation.setReferenceWork(rwList.get(0));
        rwList.get(0).addTaxonCitations(taxonCitation);
        ammocrypta.getTaxonCitations().add(taxonCitation);
        dataObjects.add(taxonCitation);
        persist(taxonCitation);

        if (isVoucherCol) {
            Locality locality = localities.get(0);
            LocalityCitation localityCitation = new LocalityCitation();
            localityCitation.initialize();
            localityCitation.setLocality(locality);
            locality.getLocalityCitations().add(localityCitation);
            localityCitation.setReferenceWork(rwList.get(1));
            rwList.get(1).addLocalityCitations(localityCitation);
            dataObjects.add(localityCitation);
            persist(localityCitation);
        }
        commitTx();
    }
    frame.setProcess(++createStep);

    //SpecifyDeleteHelper.showTableCounts("EmptyDB.txt", true);

    return collection;
}

From source file:edu.ku.brc.af.ui.forms.FormViewObj.java

/**
 * This method enables us to loop when there is a duplicate key
 * @param dataObj the data object to be saved
 * @return the merged object, or null if there was an error.
 *//*from  w  w  w  . j a v  a2  s .  c o  m*/
protected SAVE_STATE saveToDB(final Object dataObjArg) {
    if (dataObjArg == null) {
        if (saveControl != null) {
            saveControl.setEnabled(false);
        }
        return SAVE_STATE.SaveOK;
    }

    SAVE_STATE saveState = SAVE_STATE.Initial;

    boolean isDuplicateError = false;
    boolean tryAgain = false;
    int numTries = 0;

    Vector<Object> deletedItems = mvParent != null ? mvParent.getDeletedItems() : null;
    Vector<Object> toBeSavedItems = mvParent != null ? mvParent.getToBeSavedItems() : null;

    Object dObj = null;
    do {
        try {
            numTries++;

            Integer dataObjId = ((FormDataObjIFace) dataObjArg).getId();
            if (dataObjId != null) {
                DataProviderSessionIFace session1 = DataProviderFactory.getInstance().createSession();
                Integer count = null;
                try {
                    count = session1.getDataCount(dataObjArg.getClass(), "id", dataObjId,
                            DataProviderSessionIFace.CompareType.Equals);
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    if (session1 != null)
                        session1.close();
                }
                if (count == null || count == 0) {
                    UIRegistry.showLocalizedError("FormViewObj.DATA_OBJ_MISSING");
                    setHasNewData(false);
                    removeObject(true);
                    return SAVE_STATE.Error;
                }
            }

            // First get data so business Rules can be checked
            this.getDataFromUI();
            traverseToGetDataFromForms(mvParent);

            //log.debug("saveObject checking businessrules for [" + (dataObjArg != null ? dataObjArg.getClass(): "null") + "]");
            //if (businessRules != null && businessRules.processBusinessRules(dataObjArg) == BusinessRulesIFace.STATUS.Error)
            BusinessRulesIFace busRuleInError = recurseProcessBR(mvParent);
            if (busRuleInError != null) {
                UIRegistry.showError(busRuleInError.getMessagesAsString());
                return null;
            }

            // Now update the auto number fields and re-get all the data
            // we can't update the auto number fields before we run the business rules.
            mvParent.updateAutoNumbers();

            this.getDataFromUI();
            traverseToGetDataFromForms(mvParent);

            // XXX FINAL RELEASE - Need to walk the form tree and set them manually
            //FormHelper.updateLastEdittedInfo(dataObjArg);
            traverseToSetModified(getMVParent());

            session.beginTransaction();

            if (numTries == 1 && deletedItems != null && deletedItems.size() > 0) {

                //As far as I can tell it is not necessary to delete the items by hand, hibernate will delete them automatically
                //when the parent object is saved. EXCEPT if constraint violations are present due to user actions:
                //Say a user deletes Jim Jones from the collector list, and then changes mind and adds Jim Jones, and saves.
                //Then it is necessary to delete here -- I think because hibernate doesn't work.   

                //If not for the merging done by business rules for embedded collectingevents it would be possible
                //to only delete manually if numTries was 2, i.e. hibernate failed the first try, but the merging generates
                //exceptions for duplicate keys that are not thrown up to this method but mess up the session.
                deleteItemsInDelList(session, deletedItems);
                try {
                    //need to flush so later actions in the transaction know about the deletes.
                    session.flush();
                } catch (org.hibernate.ObjectDeletedException odex) {
                    //for some reason, for authors (apparently ONLY authors, even though the annotations look the same as for collector, groupmember), 
                    //hibernate will complain that the object "will be re-saved by cascade rules". If we just ignore the exception hibernate cascade deletes the object later. 
                    log.warn(odex.getMessage());
                }
            }

            if (numTries == 1 && toBeSavedItems != null) {
                //see remarks above for deletes.
                //No problems here so far, so just do it the first time around.
                saveItemsInToBeSavedList(session, toBeSavedItems);
            }

            if (businessRules != null) {
                businessRules.startProcessingBeforeAfterRules();

                businessRules.beforeMerge(dataObjArg, session);
            }

            dObj = session.merge(dataObjArg);

            if (businessRules != null) {
                businessRules.beforeSave(dObj, session);
            }

            session.saveOrUpdate(dObj);
            if (businessRules != null) {
                if (!businessRules.beforeSaveCommit(dObj, session)) {
                    throw new Exception("Business rules processing failed");
                }
            }

            session.commit();
            session.flush();

            if (mvParent != null) {
                mvParent.clearItemsToBeSaved();
            }

            if (deletedItems != null) {
                // notify the business rules object that a deletion has occurred
                for (Object obj : deletedItems) {
                    BusinessRulesIFace delBusRules = DBTableIdMgr.getInstance().getBusinessRule(obj);
                    if (delBusRules != null) {
                        delBusRules.afterDeleteCommit(obj);
                    }
                }
                deletedItems.clear();
            }

            tryAgain = false;

            isNewlyCreatedDataObj = isCreatingNewObject; // shouldn't be needed, but just in case
            if (rsController != null) {
                rsController.setNewObj(isNewlyCreatedDataObj);
            }
            if (formValidator != null) {
                formValidator.setNewObj(isNewlyCreatedDataObj);
            }

            saveState = SAVE_STATE.SaveOK;

            dataObj = dObj;

            for (FVOFieldInfo fi : controlsByName.values()) {
                if (fi.getComp() instanceof EditViewCompSwitcherPanel) {
                    ((EditViewCompSwitcherPanel) fi.getComp()).putIntoViewMode();
                }
            }

        } catch (StaleObjectException e) // was StaleObjectStateException
        {
            session.rollback();
            recoverFromStaleObject("UPDATE_DATA_STALE", null);
            tryAgain = false;
            dObj = dataObj;
            saveState = SAVE_STATE.StaleRecovery;
            //e.printStackTrace();

        } catch (ConstraintViolationException e) {
            //edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            //edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(FormViewObj.class, e);
            log.error(e);
            log.error(e.getSQLException());
            log.error(e.getSQLException().getSQLState());

            // This check here works for MySQL in English "Duplicate entry"
            // we can add other Databases as we go
            // The idea of this code is that if we are certain it failed on a constraint because
            // of a duplicate key error then we will try a couple of more times.
            //
            // The number 5 and 3 below are completely arbitrary, I just choose them
            // because they seemed right.
            //
            String errMsg = e.getSQLException().toString();
            if (StringUtils.isNotEmpty(errMsg) && errMsg.indexOf("Duplicate entry") > -1) {
                isDuplicateError = true;
            }

            tryAgain = (isDuplicateError && numTries < 5) || (!isDuplicateError && numTries < 3);

            isDuplicateError = false;

            // Ok, we tried a couple of times and have decided to give up.
            if (!tryAgain) {
                session.rollback();

                recoverFromStaleObject("DUPLICATE_KEY_ERROR", null);
                dObj = dataObj;
                saveState = SAVE_STATE.StaleRecovery;
            }

        } catch (org.hibernate.ObjectNotFoundException ex) {
            //edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            //edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(FormViewObj.class, ex);
            String errMsg = null;
            String msg = ex.toString();
            if (StringUtils.contains(msg, "No row with the given identifier exists")) {
                int sInx = msg.indexOf('[');
                int eInx = msg.lastIndexOf(']');
                if (sInx > -1 && eInx > -1) {
                    msg = msg.substring(sInx + 1, eInx);
                    eInx = msg.lastIndexOf('#');
                    if (eInx > -1) {
                        msg = msg.substring(0, eInx);
                        DBTableInfo ti = DBTableIdMgr.getInstance().getByClassName(msg);
                        if (ti != null) {
                            errMsg = String.format(getResourceString("FormViewObj.FIELD_STALE_TITLE"),
                                    ti.getTitle());
                        }
                    }
                }
            }

            if (errMsg == null) {
                errMsg = getResourceString("FormViewObj.FIELD_STALE");
            }

            session.rollback();

            recoverFromStaleObject("UNRECOVERABLE_DB_ERROR", errMsg);
            saveState = SAVE_STATE.StaleRecovery;
        } catch (Exception e) {
            session.rollback();

            // This happens when MySQL doesn't have permissions
            // to INSERT, UPDATE, OR DELETE
            if (e instanceof SQLGrammarException) {
                String msg = e.getCause().getMessage();
                if (StringUtils.contains(msg.toLowerCase(), "denied")) {
                    UIRegistry.showLocalizedError("FormViewObj.MISSING_DB_PERMS");

                }
            } else {
                edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
                edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(FormViewObj.class, e);
                log.error("******* " + e);
                e.printStackTrace();
            }

            recoverFromStaleObject("UNRECOVERABLE_DB_ERROR", null);
            saveState = SAVE_STATE.StaleRecovery;
        }

    } while (tryAgain);

    return saveState;
}

From source file:de.mpg.mpdl.inge.pubman.web.easySubmission.EasySubmission.java

/**
 * Handles the import from an external ingestion sources.
 * //from w w  w  . j  av  a2 s  . com
 * @return navigation String
 */
public String harvestData() {
    FileVO fileVO = new FileVO();
    Vector<FileVO> fileVOs = new Vector<FileVO>();
    String fetchedItem = null;
    String service = this.getEasySubmissionSessionBean().getCurrentExternalServiceType();
    PubItemVO itemVO = null;
    byte[] fetchedItemByte = null;
    DataHandlerBean dataHandler = new DataHandlerBean();
    // Fetch data from external system
    if (EasySubmissionSessionBean.IMPORT_METHOD_EXTERNAL
            .equals(this.getEasySubmissionSessionBean().getImportMethod())) {
        if (getServiceID() == null || "".equals(getServiceID())) {
            warn(getMessage("easy_submission_external_service_no_id"));
            return null;
        }
        try {
            // Harvest metadata
            logger.debug("HarvestData: " + this.getEasySubmissionSessionBean().getCurrentExternalServiceType()
                    + ": " + getServiceID());
            fetchedItemByte = dataHandler.doFetch(service, getServiceID(), this.INTERNAL_MD_FORMAT);
            fetchedItem = new String(fetchedItemByte, 0, fetchedItemByte.length, "UTF8");
            // Harvest full text
            if (this.getEasySubmissionSessionBean().isFulltext() && ((!this.getEasySubmissionSessionBean()
                    .getRadioSelectFulltext().equals(this.getEasySubmissionSessionBean().FULLTEXT_NONE))
                    && !fetchedItem.equals("")) && !service.equalsIgnoreCase("escidoc")) {
                DataSourceVO source = this.dataSourceHandler.getSourceByName(service);
                Vector<FullTextVO> ftFormats = source.getFtFormats();
                FullTextVO fulltext = new FullTextVO();
                Vector<String> formats = new Vector<String>();
                // Get DEFAULT full text version from source
                if (this.getEasySubmissionSessionBean().getRadioSelectFulltext()
                        .equals(this.getEasySubmissionSessionBean().FULLTEXT_DEFAULT)) {
                    for (int x = 0; x < ftFormats.size(); x++) {
                        fulltext = ftFormats.get(x);
                        if (fulltext.isFtDefault()) {
                            formats.add(fulltext.getName());
                            break;
                        }
                    }
                }
                // Get ALL full text versions from source
                if (this.getEasySubmissionSessionBean().getRadioSelectFulltext()
                        .equals(this.getEasySubmissionSessionBean().FULLTEXT_ALL)) {
                    for (int x = 0; x < ftFormats.size(); x++) {
                        fulltext = ftFormats.get(x);
                        formats.add(fulltext.getName());
                    }
                }
                String[] arrFormats = new String[formats.size()];
                byte[] ba = dataHandler.doFetch(
                        this.getEasySubmissionSessionBean().getCurrentExternalServiceType(), getServiceID(),
                        formats.toArray(arrFormats));
                LoginHelper loginHelper = (LoginHelper) this.getBean(LoginHelper.class);
                ByteArrayInputStream in = new ByteArrayInputStream(ba);
                URL fileURL = this.uploadFile(in, dataHandler.getContentType(),
                        loginHelper.getESciDocUserHandle());
                if (fileURL != null && !fileURL.toString().trim().equals("")) {
                    fileVO = dataHandler.getComponentVO();
                    MdsFileVO fileMd = fileVO.getDefaultMetadata();
                    fileVO.setStorage(FileVO.Storage.INTERNAL_MANAGED);
                    fileVO.setVisibility(dataHandler.getVisibility());
                    fileVO.setDefaultMetadata(fileMd);
                    fileVO.getDefaultMetadata()
                            .setTitle(this.replaceSlashes(getServiceID().trim() + dataHandler.getFileEnding()));
                    fileVO.setMimeType(dataHandler.getContentType());
                    fileVO.setName(this.replaceSlashes(getServiceID().trim() + dataHandler.getFileEnding()));
                    FormatVO formatVO = new FormatVO();
                    formatVO.setType("dcterms:IMT");
                    formatVO.setValue(dataHandler.getContentType());
                    fileVO.getDefaultMetadata().getFormats().add(formatVO);
                    fileVO.setContent(fileURL.toString());
                    fileVO.getDefaultMetadata().setSize(ba.length);
                    fileVO.getDefaultMetadata().setDescription(
                            "File downloaded from " + service + " at " + CommonUtils.currentDate());
                    fileVO.setContentCategory(dataHandler.getContentCategory());
                    fileVOs.add(fileVO);
                }
            }
        } catch (AccessException inre) {
            logger.error("Error fetching from external import source", inre);
            error(getMessage("easy_submission_import_from_external_service_access_denied_error")
                    + getServiceID());
            return null;
        } catch (IdentifierNotRecognisedException inre) {
            logger.error("Error fetching from external import source", inre);
            error(getMessage("easy_submission_import_from_external_service_identifier_error") + getServiceID());
            return null;
        } catch (SourceNotAvailableException anae) {
            logger.error("Import source currently not available", anae);
            long millis = anae.getRetryAfter().getTime() - (new Date()).getTime();
            if (millis < 1) {
                millis = 1;
            }
            error(getMessage("easy_submission_external_source_not_available_error").replace("$1",
                    Math.ceil(millis / 1000) + ""));
            return null;
        } catch (FormatNotAvailableException e) {
            error(getMessage("formatNotAvailable_FromFetchingSource").replace("$1", e.getMessage())
                    .replace("$2", service));
            this.getEasySubmissionSessionBean()
                    .setRadioSelectFulltext(this.getEasySubmissionSessionBean().FULLTEXT_NONE);
        } catch (Exception e) {
            logger.error("Error fetching from external import source", e);
            error(getMessage("easy_submission_import_from_external_service_error"));
            return null;
        }
        // Generate item ValueObject
        if (fetchedItem != null && !fetchedItem.trim().equals("")) {
            try {
                itemVO = this.xmlTransforming.transformToPubItem(fetchedItem);
                // Upload fulltexts from other escidoc repositories to current repository
                if (this.getEasySubmissionSessionBean().isFulltext()
                        && this.getEasySubmissionSessionBean().getRadioSelectFulltext() != null
                        && this.getEasySubmissionSessionBean().getRadioSelectFulltext()
                                .equals(this.getEasySubmissionSessionBean().FULLTEXT_ALL)
                        && service.equalsIgnoreCase("escidoc")) {
                    boolean hasFile = false;
                    List<FileVO> fetchedFileList = itemVO.getFiles();
                    for (int i = 0; i < fetchedFileList.size(); i++) {
                        FileVO file = fetchedFileList.get(i);
                        if (file.getStorage().equals(FileVO.Storage.INTERNAL_MANAGED)) {
                            try {
                                FileVO newFile = new FileVO();
                                byte[] content = dataHandler.retrieveComponentContent(this.getServiceID(),
                                        file.getContent());
                                LoginHelper loginHelper = (LoginHelper) this.getBean(LoginHelper.class);
                                ByteArrayInputStream in = new ByteArrayInputStream(content);
                                URL fileURL;
                                fileURL = this.uploadFile(in, dataHandler.getContentType(),
                                        loginHelper.getESciDocUserHandle());
                                if (fileURL != null && !fileURL.toString().trim().equals("")
                                        && file.getVisibility().equals(FileVO.Visibility.PUBLIC)) {
                                    hasFile = true;
                                    newFile.setStorage(FileVO.Storage.INTERNAL_MANAGED);
                                    newFile.setVisibility(file.getVisibility());
                                    newFile.setDefaultMetadata(new MdsFileVO());
                                    newFile.getDefaultMetadata().setTitle(
                                            this.replaceSlashes(file.getDefaultMetadata().getTitle()));
                                    newFile.setMimeType(file.getMimeType());
                                    newFile.setName(this.replaceSlashes(file.getName()));
                                    FormatVO formatVO = new FormatVO();
                                    formatVO.setType("dcterms:IMT");
                                    formatVO.setValue(file.getMimeType());
                                    newFile.getDefaultMetadata().getFormats().add(formatVO);
                                    newFile.setContent(fileURL.toString());
                                    newFile.getDefaultMetadata().setSize(content.length);
                                    if (file.getDescription() != null) {
                                        newFile.getDefaultMetadata()
                                                .setDescription(file.getDescription() + " File downloaded from "
                                                        + service + " at " + CommonUtils.currentDate());
                                    } else {
                                        newFile.getDefaultMetadata().setDescription("File downloaded from "
                                                + service + " at " + CommonUtils.currentDate());
                                    }
                                    newFile.setContentCategory(file.getContentCategory());
                                    fileVOs.add(newFile);
                                }
                            } catch (Exception e) {
                                logger.error("Error fetching file from coreservice", e);
                            }
                        } else if (file.getStorage().equals(FileVO.Storage.EXTERNAL_URL)
                                && file.getVisibility().equals(FileVO.Visibility.PUBLIC)) {
                            // Locator is just added as is
                            fileVOs.add(file);
                        }
                    }
                    if (!hasFile) {
                        info(getMessage("easy_submission_import_from_external_service_identifier_info"));
                    }
                }
                itemVO.getFiles().clear();
                itemVO.setContext(getItem().getContext());
                if (dataHandler.getItemUrl() != null) {
                    IdentifierVO id = new IdentifierVO();
                    id.setType(IdType.URI);
                    try {
                        id.setId(java.net.URLDecoder.decode(dataHandler.getItemUrl().toString(), "UTF-8"));
                        itemVO.getMetadata().getIdentifiers().add(id);
                    } catch (UnsupportedEncodingException e) {
                        logger.warn("Item URL could not be decoded");
                    }
                }
                if (this.getEasySubmissionSessionBean().isFulltext() && !this.getEasySubmissionSessionBean()
                        .getRadioSelectFulltext().equals(this.getEasySubmissionSessionBean().FULLTEXT_NONE)) {
                    for (int i = 0; i < fileVOs.size(); i++) {
                        FileVO tmp = fileVOs.get(i);
                        itemVO.getFiles().add(tmp);
                    }
                    fileVOs.clear();
                }
                this.getItemControllerSessionBean().getCurrentPubItem().setMetadata(itemVO.getMetadata());
                this.getItemControllerSessionBean().getCurrentPubItem().getFiles().clear();
                this.getItemControllerSessionBean().getCurrentPubItem().getFiles().addAll(itemVO.getFiles());
                // Reset info for next call
                // this.setImportSourcesInfo(); Commented out because of browser back button problem.
                this.setBibTexInfo();
            } catch (TechnicalException e) {
                logger.warn("Error transforming item to pubItem.");
                error(getMessage("easy_submission_import_from_external_service_error"));
                return null;
            }
        } else {
            logger.warn("Empty fetched Item.");
            error(getMessage("easy_submission_import_from_external_service_error"));
            return null;
        }
    }
    // Fetch data from provided file
    else if (EasySubmissionSessionBean.IMPORT_METHOD_BIBTEX
            .equals(this.getEasySubmissionSessionBean().getImportMethod())) {
        String uploadResult = uploadBibtexFile();
        if (uploadResult == null) {
            return null;
        }
    }
    this.getEditItemSessionBean().clean();
    return "loadEditItem";
}