Example usage for org.joda.time.format ISODateTimeFormat dateTimeParser

List of usage examples for org.joda.time.format ISODateTimeFormat dateTimeParser

Introduction

In this page you can find the example usage for org.joda.time.format ISODateTimeFormat dateTimeParser.

Prototype

public static DateTimeFormatter dateTimeParser() 

Source Link

Document

Returns a generic ISO datetime parser which parses either a date or a time or both.

Usage

From source file:org.apache.marmotta.kiwi.model.rdf.KiWiDateLiteral.java

License:Apache License

/**
 * Set the content of the literal to the content provided as parameter.
 *
 * @param content//from   w ww . j  a v  a2s  .  c  o m
 */
@Override
public void setContent(String content) {
    setDateContent(ISODateTimeFormat.dateTimeParser().parseDateTime(content));
}

From source file:org.apache.marmotta.kiwi.sail.KiWiValueFactory.java

License:Apache License

/**
 * Internal createLiteral method for different datatypes. This method distinguishes based on the Java class
 * type and the type argument passed as argument how to load and possibly create the new literal.
 *
 * @param value//  w  w  w . j  a  v  a2  s.co m
 * @param lang
 * @param type
 * @param <T>
 * @return
 */
private <T> KiWiLiteral createLiteral(T value, String lang, String type) {
    Locale locale;
    if (lang != null) {
        try {
            Locale.Builder builder = new Locale.Builder();
            builder.setLanguageTag(lang);
            locale = builder.build();
        } catch (IllformedLocaleException ex) {
            log.warn("malformed language literal (language: {})", lang);
            locale = null;
            lang = null;
        }
    } else {
        locale = null;
    }

    KiWiLiteral result;

    final KiWiUriResource rtype = type == null ? null : (KiWiUriResource) createURI(type);

    final KiWiConnection connection = aqcuireConnection();
    try {

        try {
            // differentiate between the different types of the value
            if (type == null) {
                // FIXME: MARMOTTA-39 (this is to avoid a NullPointerException in the following if-clauses)
                result = connection.loadLiteral(value.toString(), lang, rtype);

                if (result == null) {
                    result = new KiWiStringLiteral(value.toString(), locale, rtype);
                }
            } else if (value instanceof Date || value instanceof DateTime
                    || type.equals(Namespaces.NS_XSD + "dateTime") || type.equals(Namespaces.NS_XSD + "date")
                    || type.equals(Namespaces.NS_XSD + "time")) {
                // parse if necessary
                final DateTime dvalue;
                if (value instanceof DateTime) {
                    dvalue = ((DateTime) value).withMillisOfDay(0);
                } else if (value instanceof Date || value instanceof Calendar) {
                    dvalue = new DateTime(value);
                } else {
                    dvalue = ISODateTimeFormat.dateTimeParser().withOffsetParsed()
                            .parseDateTime(value.toString()).withMillisOfSecond(0);
                }

                result = connection.loadLiteral(dvalue);

                if (result == null) {
                    result = new KiWiDateLiteral(dvalue, rtype);
                }
            } else if (Integer.class.equals(value.getClass()) || int.class.equals(value.getClass())
                    || Long.class.equals(value.getClass()) || long.class.equals(value.getClass())
                    || type.equals(Namespaces.NS_XSD + "integer") || type.equals(Namespaces.NS_XSD + "long")) {
                long ivalue = 0;
                if (Integer.class.equals(value.getClass()) || int.class.equals(value.getClass())) {
                    ivalue = (Integer) value;
                } else if (Long.class.equals(value.getClass()) || long.class.equals(value.getClass())) {
                    ivalue = (Long) value;
                } else {
                    ivalue = Long.parseLong(value.toString());
                }

                result = connection.loadLiteral(ivalue);

                if (result == null) {
                    result = new KiWiIntLiteral(ivalue, rtype);
                }
            } else if (Double.class.equals(value.getClass()) || double.class.equals(value.getClass())
                    || Float.class.equals(value.getClass()) || float.class.equals(value.getClass())
                    || type.equals(Namespaces.NS_XSD + "double") || type.equals(Namespaces.NS_XSD + "float")
                    || type.equals(Namespaces.NS_XSD + "decimal")) {
                double dvalue = 0.0;
                if (Float.class.equals(value.getClass()) || float.class.equals(value.getClass())) {
                    dvalue = (Float) value;
                } else if (Double.class.equals(value.getClass()) || double.class.equals(value.getClass())) {
                    dvalue = (Double) value;
                } else {
                    dvalue = Double.parseDouble(value.toString());
                }

                result = connection.loadLiteral(dvalue);

                if (result == null) {
                    result = new KiWiDoubleLiteral(dvalue, rtype);
                }
            } else if (Boolean.class.equals(value.getClass()) || boolean.class.equals(value.getClass())
                    || type.equals(Namespaces.NS_XSD + "boolean")) {
                boolean bvalue = false;
                if (Boolean.class.equals(value.getClass()) || boolean.class.equals(value.getClass())) {
                    bvalue = (Boolean) value;
                } else {
                    bvalue = Boolean.parseBoolean(value.toString());
                }

                result = connection.loadLiteral(bvalue);

                if (result == null) {
                    result = new KiWiBooleanLiteral(bvalue, rtype);
                }
            } else {
                result = connection.loadLiteral(value.toString(), lang, rtype);

                if (result == null) {
                    result = new KiWiStringLiteral(value.toString(), locale, rtype);
                }
            }
        } catch (IllegalArgumentException ex) {
            // malformed number or date
            log.warn("malformed argument for typed literal of type {}: {}", rtype.stringValue(), value);
            KiWiUriResource mytype = (KiWiUriResource) createURI(Namespaces.NS_XSD + "string");

            result = connection.loadLiteral(value.toString(), lang, mytype);

            if (result == null) {
                result = new KiWiStringLiteral(value.toString(), locale, mytype);
            }

        }

        if (result.getId() < 0) {
            connection.storeNode(result);
        }

        return result;

    } catch (SQLException e) {
        log.error("database error, could not load literal", e);
        throw new IllegalStateException("database error, could not load literal", e);
    } finally {
        releaseConnection(connection);
    }
}

From source file:org.apache.pig.builtin.JsonLoader.java

License:Apache License

private Object readField(JsonParser p, ResourceFieldSchema field, int fieldnum) throws IOException {
    // Read the next token
    JsonToken tok = p.nextToken();//from   w  ww .  ja  v a2 s. c  o  m
    if (tok == null) {
        warn("Early termination of record, expected " + schema.getFields().length + " fields bug found "
                + fieldnum, PigWarning.UDF_WARNING_1);
        return null;
    }

    // Check to see if this value was null
    if (tok == JsonToken.VALUE_NULL)
        return null;

    // Read based on our expected type
    switch (field.getType()) {
    case DataType.BOOLEAN:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        return p.getBooleanValue();

    case DataType.INTEGER:
        // Read the field name
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        return p.getIntValue();

    case DataType.LONG:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        return p.getLongValue();

    case DataType.FLOAT:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        return p.getFloatValue();

    case DataType.DOUBLE:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        return p.getDoubleValue();

    case DataType.DATETIME:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        DateTimeFormatter formatter = ISODateTimeFormat.dateTimeParser();
        return formatter.withOffsetParsed().parseDateTime(p.getText());

    case DataType.BYTEARRAY:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        byte[] b = p.getText().getBytes();
        // Use the DBA constructor that copies the bytes so that we own
        // the memory
        return new DataByteArray(b, 0, b.length);

    case DataType.CHARARRAY:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        return p.getText();

    case DataType.BIGINTEGER:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        return p.getBigIntegerValue();

    case DataType.BIGDECIMAL:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        return p.getDecimalValue();

    case DataType.MAP:
        // Should be a start of the map object
        if (p.nextToken() != JsonToken.START_OBJECT) {
            warn("Bad map field, could not find start of object, field " + fieldnum, PigWarning.UDF_WARNING_1);
            return null;
        }
        Map<String, String> m = new HashMap<String, String>();
        while (p.nextToken() != JsonToken.END_OBJECT) {
            String k = p.getCurrentName();
            String v = p.getCurrentToken() == JsonToken.VALUE_NULL ? null : p.getText();
            m.put(k, v);
        }
        return m;

    case DataType.TUPLE:
        if (p.nextToken() != JsonToken.START_OBJECT) {
            warn("Bad tuple field, could not find start of object, " + "field " + fieldnum,
                    PigWarning.UDF_WARNING_1);
            return null;
        }

        ResourceSchema s = field.getSchema();
        ResourceFieldSchema[] fs = s.getFields();
        Tuple t = tupleFactory.newTuple(fs.length);

        for (int j = 0; j < fs.length; j++) {
            t.set(j, readField(p, fs[j], j));
        }

        if (p.nextToken() != JsonToken.END_OBJECT) {
            warn("Bad tuple field, could not find end of object, " + "field " + fieldnum,
                    PigWarning.UDF_WARNING_1);
            return null;
        }
        return t;

    case DataType.BAG:
        if (p.nextToken() != JsonToken.START_ARRAY) {
            warn("Bad bag field, could not find start of array, " + "field " + fieldnum,
                    PigWarning.UDF_WARNING_1);
            return null;
        }

        s = field.getSchema();
        fs = s.getFields();
        // Drill down the next level to the tuple's schema.
        s = fs[0].getSchema();
        fs = s.getFields();

        DataBag bag = bagFactory.newDefaultBag();

        JsonToken innerTok;
        while ((innerTok = p.nextToken()) != JsonToken.END_ARRAY) {
            if (innerTok != JsonToken.START_OBJECT) {
                warn("Bad bag tuple field, could not find start of " + "object, field " + fieldnum,
                        PigWarning.UDF_WARNING_1);
                return null;
            }

            t = tupleFactory.newTuple(fs.length);
            for (int j = 0; j < fs.length; j++) {
                t.set(j, readField(p, fs[j], j));
            }

            if (p.nextToken() != JsonToken.END_OBJECT) {
                warn("Bad bag tuple field, could not find end of " + "object, field " + fieldnum,
                        PigWarning.UDF_WARNING_1);
                return null;
            }
            bag.add(t);
        }
        return bag;
    default:
        throw new IOException("Unknown type in input schema: " + field.getType());
    }

}

From source file:org.apache.pig.piggybank.evaluation.datetime.truncate.ISOHelper.java

License:Apache License

/**
* @param input a non-null, non-empty Tuple,
*  whose first element is a ISO 8601 string representation of a date, time, or dateTime;
*  with optional time zone.//  w  ww  .  j av  a  2s  .c o m
* @return a DateTime representing the date, 
*  with DateTimeZone set to the time zone parsed from the string,
*  or else DateTimeZone.defaultTimeZone() if one is set,
*  or else UTC.
* @throws ExecException if input is a malformed or empty tuple.
* This method is public so that it can be tested in TestTruncateDateTime. 
* Otherwise, it would have "package" visibility.
*/
public static DateTime parseDateTime(Tuple input) throws ExecException {

    // Save previous default time zone for restore later.
    DateTimeZone previousDefaultTimeZone = DateTimeZone.getDefault();

    // Temporarily set default time zone to UTC, for this parse.
    DateTimeZone.setDefault(DEFAULT_DATE_TIME_ZONE);

    String isoDateString = input.get(0).toString();
    DateTime dt = ISODateTimeFormat.dateTimeParser().withOffsetParsed().parseDateTime(isoDateString);

    // restore previous default TimeZone.
    DateTimeZone.setDefault(previousDefaultTimeZone);

    return dt;
}

From source file:org.apache.tamaya.jodatime.InstantConverter.java

License:Apache License

@Override
public Instant convert(String value, ConversionContext context) {
    context.addSupportedFormats(InstantConverter.class, PARSER_FORMATS);

    String trimmed = Objects.requireNonNull(value).trim();
    try {/*from  ww  w . ja  v a 2s  .c o m*/
        return ISODateTimeFormat.dateTimeParser().parseDateTime(trimmed).toInstant();
    } catch (RuntimeException e) {
        // Ok, go on and try the next parser
    }
    return null;
}

From source file:org.archfirst.bfoms.interfacein.exchange.MarketPriceListener.java

License:Apache License

private MarketPrice toMarketPrice(String marketPriceString) {
    Properties properties = new Properties();
    try {//from w w w.  j  a  v  a 2 s  . c om
        properties.load(new StringReader(marketPriceString));
    } catch (IOException e) {
        throw new IllegalArgumentException(marketPriceString, e);
    }

    DateTime effective = ISODateTimeFormat.dateTimeParser().parseDateTime(properties.getProperty("effective"));
    String symbol = properties.getProperty("symbol");
    BigDecimal price = new BigDecimal(properties.getProperty("price"));
    Currency currency = Currency.getInstance(properties.getProperty("currency"));

    return new MarketPrice(symbol, new Money(price, currency), effective);
}

From source file:org.cinchapi.concourse.Timestamp.java

License:Open Source License

/**
 * Parses a {@code Timestamp} from the specified string.
 * <p>//from   ww  w  . ja v  a2 s .  c o m
 * This uses {@link ISODateTimeFormat#dateTimeParser()}.
 * 
 * @param str the string to parse, not null
 * @return the parsed timestamp
 */
public static Timestamp parse(String str) {
    return new Timestamp(DateTime.parse(str, ISODateTimeFormat.dateTimeParser().withOffsetParsed()));
}

From source file:org.ecocean.servlet.importer.ImportExcelMetadata.java

License:Open Source License

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    //set up for response
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    boolean locked = false;

    String context = "context0";
    context = ServletUtilities.getContext(request);
    Shepherd myShepherd = new Shepherd(context);
    myShepherd.beginDBTransaction();//from w  w  w.  j a  v  a2s . c  om
    AssetStore assetStore = AssetStore.getDefault(myShepherd);
    myShepherd.commitDBTransaction();

    System.out.println("\n\nStarting ImportExcelMetadata servlet...");

    //setup data dir
    String rootWebappPath = getServletContext().getRealPath("/");
    File webappsDir = new File(rootWebappPath).getParentFile();
    File shepherdDataDir = new File(webappsDir, CommonConfiguration.getDataDirectoryName(context));
    if (!shepherdDataDir.exists()) {
        shepherdDataDir.mkdirs();
    }
    File tempSubdir = new File(webappsDir, "temp");
    if (!tempSubdir.exists()) {
        tempSubdir.mkdirs();
    }
    System.out.println("\n\n     Finished directory creation...");

    String fileName = "None";

    StringBuffer messages = new StringBuffer();

    boolean successfullyWroteFile = false;

    File finalFile = new File(tempSubdir, "temp.csv");

    try {
        MultipartParser mp = new MultipartParser(request,
                (CommonConfiguration.getMaxMediaSizeInMegabytes(context) * 1048576));
        Part part;
        while ((part = mp.readNextPart()) != null) {
            String name = part.getName();
            if (part.isParam()) {

                // it's a parameter part
                ParamPart paramPart = (ParamPart) part;
                String value = paramPart.getStringValue();

            }

            if (part.isFile()) {
                FilePart filePart = (FilePart) part;
                fileName = ServletUtilities.cleanFileName(filePart.getFileName());
                if (fileName != null) {
                    System.out.println("ImportExcelMetadata is trying to upload file " + fileName);
                    //File thisSharkDir = new File(encountersDir.getAbsolutePath() +"/"+ encounterNumber);
                    //if(!thisSharkDir.exists()){thisSharkDir.mkdirs();}
                    finalFile = new File(tempSubdir, fileName);
                    filePart.writeTo(finalFile);
                    successfullyWroteFile = true;
                    System.out.println("\n\nImportExcelMetadata successfully uploaded the file!");
                }
            }
        }

        try {
            if (successfullyWroteFile) {

                System.out.println("\n\n     Starting Excel Metadata content import");

                //OK, we have our CSV file
                //let's import
                CSVReader reader = new CSVReader(new FileReader(finalFile));
                List<String[]> allLines = reader.readAll();
                System.out.println("\n\n     Read in the file!");

                //let's detect the size of this array by reading the number of header columns in row 0
                String[] headerNames = allLines.get(0);
                int numColumns = headerNames.length;
                int numRows = allLines.size();

                //determine the Occurrence_ID column as it is at the end
                int occurrenceIDColumnNumber = -1;
                for (int g = 0; g < numColumns; g++) {
                    if (headerNames[g].equals("Occurrence_ID")) {
                        occurrenceIDColumnNumber = g;
                    }
                }

                for (int i = 1; i < numRows; i++) {

                    System.out.println("\n\n     Processing row " + i);
                    boolean newEncounter = true;
                    boolean newShark = true;
                    String[] line = allLines.get(i);

                    boolean ok2import = true;

                    Encounter enc = new Encounter();

                    myShepherd.beginDBTransaction();

                    //line[0] is the sample_ID
                    String encNumber = line[0].trim();
                    if ((encNumber != null) && (!encNumber.equals(""))) {
                        if (myShepherd.isEncounter(encNumber)) {
                            enc = myShepherd.getEncounter(encNumber);
                            newEncounter = false;
                        } else {
                            enc.setCatalogNumber(encNumber);
                            enc.setState("approved");
                        }
                    } else {
                        ok2import = false;
                        messages.append("<li>Row " + i
                                + ": could not find sample/encounter ID in the first column of row " + i
                                + ".</li>");
                        System.out.println(
                                "          Could not find sample/encounter ID in the first column of row " + i
                                        + ".");
                    }

                    //line[1] is the IndividualID
                    String individualID = line[1].trim();
                    if (individualID != null) {

                        enc.addComments("<p><em>" + request.getRemoteUser() + " on "
                                + (new java.util.Date()).toString() + "</em><br>"
                                + "Import SRGD process set marked individual to " + individualID + ".</p>");

                        //enc.setIndividualID(individualID);
                        System.out.println(
                                "          Setting Individual ID for row " + i + ". Value: " + individualID);

                    }

                    //line[2] is the latitude
                    String latitude = line[2].trim();
                    if ((latitude != null) && (!latitude.equals(""))) {
                        try {

                            enc.addComments("<p><em>" + request.getRemoteUser() + " on "
                                    + (new java.util.Date()).toString() + "</em><br>"
                                    + "Import SRGD process set latitude to " + latitude + ".</p>");

                            Double lat = new Double(latitude);
                            enc.setDecimalLatitude(lat);
                            System.out.println(
                                    "          Setting latitude for row " + i + ". Value: " + latitude);

                        } catch (NumberFormatException nfe) {
                            messages.append("<li>Row " + i + " for sample ID " + enc.getCatalogNumber()
                                    + ": Latitude hit a NumberFormatException in row " + i
                                    + " and could not be imported. The listed value was: " + latitude
                                    + "</li>");
                        }
                    }

                    //line[3] is the latitude
                    String longitude = line[3].trim();
                    if ((longitude != null) && (!longitude.equals(""))) {
                        try {

                            enc.addComments("<p><em>" + request.getRemoteUser() + " on "
                                    + (new java.util.Date()).toString() + "</em><br>"
                                    + "Import SRGD process set longitude to " + longitude + ".</p>");

                            Double longie = new Double(longitude);
                            enc.setDecimalLongitude(longie);
                            System.out.println(
                                    "          Setting longitude for row " + i + ". Value: " + longitude);

                        } catch (NumberFormatException nfe) {
                            nfe.printStackTrace();
                            messages.append("<li>Row " + i + " for sample ID " + enc.getCatalogNumber()
                                    + ": Longitude hit a NumberFormatException in row " + i
                                    + " and could not be imported. The listed value was: " + longitude
                                    + "</li>");
                        }
                    }

                    //line[4] is the date_time
                    String isoDate = line[4].trim();
                    if ((isoDate != null) && (!isoDate.equals(""))) {

                        StringTokenizer tks = new StringTokenizer(isoDate, "-");
                        int numTokens = tks.countTokens();
                        DateTimeFormatter parser2 = ISODateTimeFormat.dateTimeParser();

                        enc.setMonth(-1);
                        enc.setDay(-1);
                        enc.setYear(-1);
                        enc.setHour(-1);
                        enc.setMinutes("00");

                        try {
                            DateTime time = parser2.parseDateTime(isoDate);
                            enc.setYear(time.getYear());

                            if (numTokens >= 2) {
                                enc.setMonth(time.getMonthOfYear());
                            }
                            if (numTokens >= 3) {
                                enc.setDay(time.getDayOfMonth());
                            }

                            if (isoDate.indexOf("T") != -1) {
                                int minutes = time.getMinuteOfHour();
                                String minutes2 = (new Integer(minutes)).toString();
                                if ((time.getHourOfDay() != 0) && (minutes != 0)) {
                                    enc.setHour(time.getHourOfDay());
                                    if (isoDate.indexOf(":") != -1) {
                                        enc.setMinutes(minutes2);
                                    }
                                }
                            }

                            enc.addComments("<p><em>" + request.getRemoteUser() + " on "
                                    + (new java.util.Date()).toString() + "</em><br>"
                                    + "Import SRGD process set date to " + enc.getDate() + ".</p>");

                            System.out.println("          Set date for encounter: " + enc.getDate());

                        } catch (IllegalArgumentException iae) {
                            iae.printStackTrace();
                            messages.append("<li>Row " + i + ": could not import the date and time for row: "
                                    + i + ". Cancelling the import for this row.</li>");
                            ok2import = false;

                        }
                    }

                    //line[5] get locationID
                    String locationID = line[5].trim();
                    if (line.length >= 6) {
                        if ((locationID != null) && (!locationID.equals(""))) {
                            enc.setLocationID(locationID);
                            enc.addComments("<p><em>" + request.getRemoteUser() + " on "
                                    + (new java.util.Date()).toString() + "</em><br>"
                                    + "Import SRGD process set location ID to " + locationID + ".</p>");

                            System.out.println(
                                    "          Setting location ID for row " + i + ". Value: " + locationID);
                        }
                    }

                    //line[6] get sex
                    String sex = line[6].trim();
                    if (line.length >= 7) {
                        if ((sex != null) && (!sex.equals(""))) {

                            if (sex.equals("M")) {
                                enc.setSex("male");
                            } else if (sex.equals("F")) {
                                enc.setSex("female");
                            } else {
                                enc.setSex("unknown");
                            }

                            System.out.println("          Setting sex for row " + i + ". Value: " + sex);
                            enc.addComments("<p><em>" + request.getRemoteUser() + " on "
                                    + (new java.util.Date()).toString() + "</em><br>"
                                    + "Import SRGD process set sex to " + enc.getSex() + ".</p>");

                        }

                        //line[occurrenceIDColumnNumber] get Occurrence_ID
                        Occurrence occur = new Occurrence();
                        if (occurrenceIDColumnNumber != -1) {
                            String occurID = line[occurrenceIDColumnNumber];

                            if (myShepherd.isOccurrence(occurID)) {
                                occur = myShepherd.getOccurrence(occurID);
                                boolean isNew = occur.addEncounter(enc);
                                if (isNew) {
                                    occur.addComments("<p><em>" + request.getRemoteUser() + " on "
                                            + (new java.util.Date()).toString() + "</em><br>"
                                            + "Import SRGD process added encounter " + enc.getCatalogNumber()
                                            + ".</p>");
                                }

                            } else {
                                occur = new Occurrence(occurID, enc);
                                occur.addComments("<p><em>" + request.getRemoteUser() + " on "
                                        + (new java.util.Date()).toString() + "</em><br>"
                                        + "Import SRGD process added encounter " + enc.getCatalogNumber()
                                        + ".</p>");

                                myShepherd.getPM().makePersistent(occur);

                            }
                        }

                    }

                    if (ok2import) {

                        System.out.println("          ok2import");

                        myShepherd.commitDBTransaction();
                        if (newEncounter) {
                            myShepherd.storeNewEncounter(enc, enc.getCatalogNumber());
                        }

                        //before proceeding with haplotype and loci importing, we need to create the tissue sample
                        myShepherd.beginDBTransaction();
                        Encounter enc3 = myShepherd.getEncounter(encNumber);
                        TissueSample ts = new TissueSample(encNumber, ("sample_" + encNumber));

                        if (myShepherd.isTissueSample(("sample_" + encNumber), encNumber)) {
                            ts = myShepherd.getTissueSample(("sample_" + encNumber), encNumber);
                        } else {
                            myShepherd.getPM().makePersistent(ts);
                            enc3.addTissueSample(ts);
                        }
                        System.out.println("          Added TissueSample.");

                        //let's set genetic Sex
                        if ((sex != null) && (!sex.equals(""))) {
                            SexAnalysis sexDNA = new SexAnalysis(
                                    ("analysis_" + enc3.getCatalogNumber() + "_sex"), sex,
                                    enc3.getCatalogNumber(), ("sample_" + enc3.getCatalogNumber()));
                            if (myShepherd.isGeneticAnalysis(ts.getSampleID(), encNumber,
                                    ("analysis_" + enc3.getCatalogNumber() + "_sex"), "SexAnalysis")) {
                                sexDNA = myShepherd.getSexAnalysis(ts.getSampleID(), encNumber,
                                        ("analysis_" + enc3.getCatalogNumber() + "_sex"));
                                sexDNA.setSex(sex);
                            } else {
                                ts.addGeneticAnalysis(sexDNA);
                                myShepherd.getPM().makePersistent(sexDNA);
                            }
                            enc3.addComments("<p><em>" + request.getRemoteUser() + " on "
                                    + (new java.util.Date()).toString() + "</em><br />"
                                    + "Import SRGD process added or updated genetic sex analysis "
                                    + sexDNA.getAnalysisID() + " for tissue sample " + ts.getSampleID()
                                    + ".<br />" + sexDNA.getHTMLString());
                        }
                        System.out.println("          Added genetic sex.");

                        //line[7] get haplotype
                        if (line.length >= 8) {
                            String haplo = line[7].trim();
                            if ((haplo != null) && (!haplo.equals(""))) {
                                //TBD check id this analysis already exists
                                System.out.println("          Starting haplotype.");

                                MitochondrialDNAAnalysis mtDNA = new MitochondrialDNAAnalysis(
                                        ("analysis_" + enc3.getCatalogNumber()), haplo, enc3.getCatalogNumber(),
                                        ("sample_" + enc3.getCatalogNumber()));
                                if (myShepherd.isGeneticAnalysis(ts.getSampleID(), encNumber,
                                        ("analysis_" + enc3.getCatalogNumber()), "MitochondrialDNA")) {
                                    mtDNA = myShepherd.getMitochondrialDNAAnalysis(ts.getSampleID(), encNumber,
                                            ("analysis_" + enc3.getCatalogNumber()));
                                    mtDNA.setHaplotype(haplo);
                                    System.out.println("                  Haplotype reset.");

                                } else {
                                    ts.addGeneticAnalysis(mtDNA);
                                    myShepherd.getPM().makePersistent(mtDNA);
                                    System.out.println("          Added new haplotype.");

                                }
                                enc3.addComments("<p><em>" + request.getRemoteUser() + " on "
                                        + (new java.util.Date()).toString() + "</em><br />"
                                        + "Import SRGD process added or updated mitochondrial DNA analysis (haplotype) "
                                        + mtDNA.getAnalysisID() + " for tissue sample " + ts.getSampleID()
                                        + ".<br />" + mtDNA.getHTMLString());
                                System.out.println("          Added haplotype.");
                            } else {
                                System.out.println("          Did NOT add haplotype.");
                            }
                        }

                        ArrayList<Locus> loci = new ArrayList<Locus>();

                        //loci value import
                        if (line.length >= 9) {

                            for (int f = 8; f < numColumns; f++) {
                                if (line.length > (f + 2)) {
                                    String l1 = line[f].trim();
                                    String l2 = line[f + 1].trim();
                                    String locusName = headerNames[f].replaceAll("L_", "");

                                    System.out.println("          Loaded loci name.");

                                    //verify that we're looking at the right loci and everything matches up nicely
                                    if ((l1 != null) && (l2 != null) && (!l1.equals("")) && (!l2.equals(""))
                                            && (!locusName.equals(""))
                                            && (headerNames[f].trim().toLowerCase().startsWith("l_"))
                                            && (headerNames[f + 1].trim().toLowerCase().startsWith("l_"))
                                            && (headerNames[f].trim().toLowerCase()
                                                    .equals(headerNames[f + 1].trim().toLowerCase()))) {

                                        //get allele values
                                        Integer intA = new Integer(l1);
                                        Integer intB = new Integer(l2);

                                        Locus myLocus = new Locus(locusName, intA, intB);
                                        loci.add(myLocus);

                                    }

                                    f++;
                                }
                            }
                        }

                        //TBD check if this analysis already exists
                        if (loci.size() > 0) {

                            System.out.println("          Found msMarkers!!!!!!!!!!!!1");

                            MicrosatelliteMarkersAnalysis microAnalysis = new MicrosatelliteMarkersAnalysis(
                                    (ts.getSampleID() + "_msMarkerAnalysis"), ts.getSampleID(),
                                    enc.getCatalogNumber(), loci);

                            if (myShepherd.isGeneticAnalysis(ts.getSampleID(), encNumber,
                                    (ts.getSampleID() + "_msMarkerAnalysis"), "MicrosatelliteMarkers")) {
                                microAnalysis = myShepherd.getMicrosatelliteMarkersAnalysis(ts.getSampleID(),
                                        encNumber, (ts.getSampleID() + "_msMarkerAnalysis"));
                                microAnalysis.setLoci(loci);
                            } else {
                                ts.addGeneticAnalysis(microAnalysis);
                                myShepherd.getPM().makePersistent(microAnalysis);
                            }
                            System.out.println("          Added ms markers.");

                            enc3.addComments("<p><em>" + request.getRemoteUser() + " on "
                                    + (new java.util.Date()).toString() + "</em><br />"
                                    + "Import SRGD process added or updated microsatellite markers of analysis "
                                    + microAnalysis.getAnalysisID() + " for tissue sample " + ts.getSampleID()
                                    + ".<br />" + microAnalysis.getHTMLString());

                        }

                        myShepherd.commitDBTransaction();

                        if (!individualID.equals("")) {
                            MarkedIndividual indie = new MarkedIndividual();
                            myShepherd.beginDBTransaction();

                            Encounter enc2 = myShepherd.getEncounter(encNumber);

                            if (myShepherd.isMarkedIndividual(individualID)) {
                                indie = myShepherd.getMarkedIndividual(individualID);
                                newShark = false;
                            } else {
                                //indie.setIndividualID(individualID);  FIXME  !!!
                            }

                            //OK to generically add it as the addEncounter() method will ignore it if already added to marked individual
                            indie.addEncounter(enc2);

                            if ((indie.getSex() == null)
                                    || ((enc2.getSex() != null) && (indie.getSex() != enc2.getSex()))) {
                                indie.setSex(enc2.getSex());
                                indie.addComments("<p><em>" + request.getRemoteUser() + " on "
                                        + (new java.util.Date()).toString() + "</em><br>"
                                        + "Import SRGD process set sex to " + enc2.getSex() + ".</p>");

                            }

                            if ((indie.getHaplotype() == null) && (enc2.getHaplotype() != null)) {
                                indie.doNotSetLocalHaplotypeReflection(enc2.getHaplotype());
                            }

                            indie.refreshDependentProperties();
                            indie.addComments("<p><em>" + request.getRemoteUser() + " on "
                                    + (new java.util.Date()).toString() + "</em><br>"
                                    + "Import SRGD process added encounter " + enc2.getCatalogNumber()
                                    + ".</p>");

                            myShepherd.commitDBTransaction();
                            if (newShark) {
                                myShepherd.storeNewMarkedIndividual(indie);
                            }
                        }

                    } else {
                        myShepherd.rollbackDBTransaction();
                    }

                    //out.println("Imported row: "+line);

                }

            } else {
                locked = true;
                System.out.println("ImportSRGD: For some reason the import failed without exception.");
            }

        } catch (Exception le) {
            locked = true;
            myShepherd.rollbackDBTransaction();
            myShepherd.closeDBTransaction();
            le.printStackTrace();
        }

        if (!locked) {
            myShepherd.commitDBTransaction();
            myShepherd.closeDBTransaction();
            out.println(ServletUtilities.getHeader(request));
            out.println(
                    "<p><strong>Success!</strong> I have successfully uploaded and imported your SRGD CSV file.</p>");

            if (messages.toString().equals("")) {
                messages.append("None");
            }
            out.println("<p>The following error messages were reported during the import process:<br /><ul>"
                    + messages + "</ul></p>");

            out.println("<p><a href=\"appadmin/import.jsp\">Return to the import page</a></p>");

            out.println(ServletUtilities.getFooter(context));
        }

    } catch (IOException lEx) {
        lEx.printStackTrace();
        out.println(ServletUtilities.getHeader(request));
        out.println(
                "<strong>Error:</strong> I was unable to upload your SRGD CSV. Please contact the webmaster about this message.");
        out.println(ServletUtilities.getFooter(context));
    } catch (NullPointerException npe) {
        npe.printStackTrace();
        out.println(ServletUtilities.getHeader(request));
        out.println("<strong>Error:</strong> I was unable to import SRGD data as no file was specified.");
        out.println(ServletUtilities.getFooter(context));
    }
    out.close();
}

From source file:org.ecocean.servlet.importer.ImportSRGD.java

License:Open Source License

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String context = "context0";
    context = ServletUtilities.getContext(request);
    Shepherd myShepherd = new Shepherd(context);
    myShepherd.setAction("ImportSRGD");
    System.out.println("\n\nStarting ImportSRGD servlet...");

    //setup data dir
    String rootWebappPath = getServletContext().getRealPath("/");
    File webappsDir = new File(rootWebappPath).getParentFile();
    File shepherdDataDir = new File(webappsDir, CommonConfiguration.getDataDirectoryName(context));
    if (!shepherdDataDir.exists()) {
        shepherdDataDir.mkdirs();/*w ww  . j  ava  2s  . co  m*/
    }
    File tempSubdir = new File(webappsDir, "temp");
    if (!tempSubdir.exists()) {
        tempSubdir.mkdirs();
    }
    System.out.println("\n\n     Finished directory creation...");

    //set up for response
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    boolean locked = false;

    String fileName = "None";

    StringBuffer messages = new StringBuffer();

    boolean successfullyWroteFile = false;

    File finalFile = new File(tempSubdir, "temp.csv");

    try {
        MultipartParser mp = new MultipartParser(request,
                (CommonConfiguration.getMaxMediaSizeInMegabytes(context) * 1048576));
        Part part;
        while ((part = mp.readNextPart()) != null) {
            String name = part.getName();
            if (part.isParam()) {

                // it's a parameter part
                ParamPart paramPart = (ParamPart) part;
                String value = paramPart.getStringValue();

            }

            if (part.isFile()) {
                FilePart filePart = (FilePart) part;
                fileName = ServletUtilities.cleanFileName(filePart.getFileName());
                if (fileName != null) {
                    System.out.println("     Trying to upload file: " + fileName);
                    //File thisSharkDir = new File(encountersDir.getAbsolutePath() +"/"+ encounterNumber);
                    //if(!thisSharkDir.exists()){thisSharkDir.mkdirs();}
                    finalFile = new File(tempSubdir, fileName);
                    filePart.writeTo(finalFile);
                    successfullyWroteFile = true;
                    System.out.println("\n\n     I successfully uploaded the file!");
                }
            }
        }

        try {
            if (successfullyWroteFile) {

                System.out.println("\n\n     Starting CSV content import...");

                //OK, we have our CSV file
                //let's import
                CSVReader reader = new CSVReader(new FileReader(finalFile));
                List<String[]> allLines = reader.readAll();
                System.out.println("\n\n     Read in the CSV file!");

                //let's detect the size of this array by reading the number of header columns in row 0
                String[] headerNames = allLines.get(0);
                int numColumns = headerNames.length;
                int numRows = allLines.size();

                //determine the Occurrence_ID column as it is at the end
                int occurrenceIDColumnNumber = -1;
                for (int g = 0; g < numColumns; g++) {
                    if (headerNames[g].equals("Occurrence_ID")) {
                        occurrenceIDColumnNumber = g;
                    }
                }

                for (int i = 1; i < numRows; i++) {

                    System.out.println("\n\n     Processing row " + i);
                    boolean newEncounter = true;
                    boolean newShark = true;
                    String[] line = allLines.get(i);

                    boolean ok2import = true;

                    Encounter enc = new Encounter();

                    myShepherd.beginDBTransaction();

                    //line[0] is the sample_ID
                    String encNumber = line[0].trim();
                    if ((encNumber != null) && (!encNumber.equals(""))) {
                        if (myShepherd.isEncounter(encNumber)) {
                            enc = myShepherd.getEncounter(encNumber);
                            newEncounter = false;
                        } else {
                            enc.setCatalogNumber(encNumber);
                            enc.setState("approved");
                        }
                    } else {
                        ok2import = false;
                        messages.append("<li>Row " + i
                                + ": could not find sample/encounter ID in the first column of row " + i
                                + ".</li>");
                        System.out.println(
                                "          Could not find sample/encounter ID in the first column of row " + i
                                        + ".");
                    }

                    //line[1] is the IndividualID
                    String individualID = line[1].trim();
                    if (individualID != null) {

                        enc.addComments("<p><em>" + request.getRemoteUser() + " on "
                                + (new java.util.Date()).toString() + "</em><br>"
                                + "Import SRGD process set marked individual to " + individualID + ".</p>");

                        //enc.setIndividualID(individualID);
                        System.out.println(
                                "          Setting Individual ID for row " + i + ". Value: " + individualID);

                    }

                    //line[2] is the latitude
                    String latitude = line[2].trim();
                    if ((latitude != null) && (!latitude.equals(""))) {
                        try {

                            enc.addComments("<p><em>" + request.getRemoteUser() + " on "
                                    + (new java.util.Date()).toString() + "</em><br>"
                                    + "Import SRGD process set latitude to " + latitude + ".</p>");

                            Double lat = new Double(latitude);
                            enc.setDecimalLatitude(lat);
                            System.out.println(
                                    "          Setting latitude for row " + i + ". Value: " + latitude);

                        } catch (NumberFormatException nfe) {
                            messages.append("<li>Row " + i + " for sample ID " + enc.getCatalogNumber()
                                    + ": Latitude hit a NumberFormatException in row " + i
                                    + " and could not be imported. The listed value was: " + latitude
                                    + "</li>");
                        }
                    }

                    //line[3] is the latitude
                    String longitude = line[3].trim();
                    if ((longitude != null) && (!longitude.equals(""))) {
                        try {

                            enc.addComments("<p><em>" + request.getRemoteUser() + " on "
                                    + (new java.util.Date()).toString() + "</em><br>"
                                    + "Import SRGD process set longitude to " + longitude + ".</p>");

                            Double longie = new Double(longitude);
                            enc.setDecimalLongitude(longie);
                            System.out.println(
                                    "          Setting longitude for row " + i + ". Value: " + longitude);

                        } catch (NumberFormatException nfe) {
                            nfe.printStackTrace();
                            messages.append("<li>Row " + i + " for sample ID " + enc.getCatalogNumber()
                                    + ": Longitude hit a NumberFormatException in row " + i
                                    + " and could not be imported. The listed value was: " + longitude
                                    + "</li>");
                        }
                    }

                    //line[4] is the date_time
                    String isoDate = line[4].trim();
                    if ((isoDate != null) && (!isoDate.equals(""))) {

                        StringTokenizer tks = new StringTokenizer(isoDate, "-");
                        int numTokens = tks.countTokens();
                        DateTimeFormatter parser2 = ISODateTimeFormat.dateTimeParser();

                        enc.setMonth(-1);
                        enc.setDay(-1);
                        enc.setYear(-1);
                        enc.setHour(-1);
                        enc.setMinutes("00");

                        try {
                            DateTime time = parser2.parseDateTime(isoDate);
                            enc.setYear(time.getYear());

                            if (numTokens >= 2) {
                                enc.setMonth(time.getMonthOfYear());
                            }
                            if (numTokens >= 3) {
                                enc.setDay(time.getDayOfMonth());
                            }

                            if (isoDate.indexOf("T") != -1) {
                                int minutes = time.getMinuteOfHour();
                                String minutes2 = (new Integer(minutes)).toString();
                                if ((time.getHourOfDay() != 0) && (minutes != 0)) {
                                    enc.setHour(time.getHourOfDay());
                                    if (isoDate.indexOf(":") != -1) {
                                        enc.setMinutes(minutes2);
                                    }
                                }
                            }

                            enc.addComments("<p><em>" + request.getRemoteUser() + " on "
                                    + (new java.util.Date()).toString() + "</em><br>"
                                    + "Import SRGD process set date to " + enc.getDate() + ".</p>");

                            System.out.println("          Set date for encounter: " + enc.getDate());

                        } catch (IllegalArgumentException iae) {
                            iae.printStackTrace();
                            messages.append("<li>Row " + i + ": could not import the date and time for row: "
                                    + i + ". Cancelling the import for this row.</li>");
                            ok2import = false;

                        }
                    }

                    //line[5] get locationID
                    String locationID = line[5].trim();
                    if (line.length >= 6) {
                        if ((locationID != null) && (!locationID.equals(""))) {
                            enc.setLocationID(locationID);
                            enc.addComments("<p><em>" + request.getRemoteUser() + " on "
                                    + (new java.util.Date()).toString() + "</em><br>"
                                    + "Import SRGD process set location ID to " + locationID + ".</p>");

                            System.out.println(
                                    "          Setting location ID for row " + i + ". Value: " + locationID);
                        }
                    }

                    //line[6] get sex
                    String sex = line[6].trim();
                    if (line.length >= 7) {
                        if ((sex != null) && (!sex.equals(""))) {

                            if (sex.equals("M")) {
                                enc.setSex("male");
                            } else if (sex.equals("F")) {
                                enc.setSex("female");
                            } else {
                                enc.setSex("unknown");
                            }

                            System.out.println("          Setting sex for row " + i + ". Value: " + sex);
                            enc.addComments("<p><em>" + request.getRemoteUser() + " on "
                                    + (new java.util.Date()).toString() + "</em><br>"
                                    + "Import SRGD process set sex to " + enc.getSex() + ".</p>");

                        }

                        //line[occurrenceIDColumnNumber] get Occurrence_ID
                        Occurrence occur = new Occurrence();
                        if (occurrenceIDColumnNumber != -1) {
                            String occurID = line[occurrenceIDColumnNumber];

                            if (myShepherd.isOccurrence(occurID)) {
                                occur = myShepherd.getOccurrence(occurID);
                                boolean isNew = occur.addEncounter(enc);
                                if (isNew) {
                                    occur.addComments("<p><em>" + request.getRemoteUser() + " on "
                                            + (new java.util.Date()).toString() + "</em><br>"
                                            + "Import SRGD process added encounter " + enc.getCatalogNumber()
                                            + ".</p>");
                                }

                            } else {
                                occur = new Occurrence(occurID, enc);
                                occur.addComments("<p><em>" + request.getRemoteUser() + " on "
                                        + (new java.util.Date()).toString() + "</em><br>"
                                        + "Import SRGD process added encounter " + enc.getCatalogNumber()
                                        + ".</p>");

                                myShepherd.getPM().makePersistent(occur);

                            }
                        }

                    }

                    if (ok2import) {

                        System.out.println("          ok2import");

                        myShepherd.commitDBTransaction();
                        if (newEncounter) {
                            myShepherd.storeNewEncounter(enc, enc.getCatalogNumber());
                        }

                        //before proceeding with haplotype and loci importing, we need to create the tissue sample
                        myShepherd.beginDBTransaction();
                        Encounter enc3 = myShepherd.getEncounter(encNumber);
                        TissueSample ts = new TissueSample(encNumber, ("sample_" + encNumber));

                        if (myShepherd.isTissueSample(("sample_" + encNumber), encNumber)) {
                            ts = myShepherd.getTissueSample(("sample_" + encNumber), encNumber);
                        } else {
                            myShepherd.getPM().makePersistent(ts);
                            enc3.addTissueSample(ts);
                        }
                        System.out.println("          Added TissueSample.");

                        //let's set genetic Sex
                        if ((sex != null) && (!sex.equals(""))) {
                            SexAnalysis sexDNA = new SexAnalysis(
                                    ("analysis_" + enc3.getCatalogNumber() + "_sex"), sex,
                                    enc3.getCatalogNumber(), ("sample_" + enc3.getCatalogNumber()));
                            if (myShepherd.isGeneticAnalysis(ts.getSampleID(), encNumber,
                                    ("analysis_" + enc3.getCatalogNumber() + "_sex"), "SexAnalysis")) {
                                sexDNA = myShepherd.getSexAnalysis(ts.getSampleID(), encNumber,
                                        ("analysis_" + enc3.getCatalogNumber() + "_sex"));
                                sexDNA.setSex(sex);
                            } else {
                                ts.addGeneticAnalysis(sexDNA);
                                myShepherd.getPM().makePersistent(sexDNA);
                            }
                            enc3.addComments("<p><em>" + request.getRemoteUser() + " on "
                                    + (new java.util.Date()).toString() + "</em><br />"
                                    + "Import SRGD process added or updated genetic sex analysis "
                                    + sexDNA.getAnalysisID() + " for tissue sample " + ts.getSampleID()
                                    + ".<br />" + sexDNA.getHTMLString());
                        }
                        System.out.println("          Added genetic sex.");

                        //line[7] get haplotype
                        if (line.length >= 8) {
                            String haplo = line[7].trim();
                            if ((haplo != null) && (!haplo.equals(""))) {
                                //TBD check id this analysis already exists
                                System.out.println("          Starting haplotype.");

                                MitochondrialDNAAnalysis mtDNA = new MitochondrialDNAAnalysis(
                                        ("analysis_" + enc3.getCatalogNumber()), haplo, enc3.getCatalogNumber(),
                                        ("sample_" + enc3.getCatalogNumber()));
                                if (myShepherd.isGeneticAnalysis(ts.getSampleID(), encNumber,
                                        ("analysis_" + enc3.getCatalogNumber()), "MitochondrialDNA")) {
                                    mtDNA = myShepherd.getMitochondrialDNAAnalysis(ts.getSampleID(), encNumber,
                                            ("analysis_" + enc3.getCatalogNumber()));
                                    mtDNA.setHaplotype(haplo);
                                    System.out.println("                  Haplotype reset.");

                                } else {
                                    ts.addGeneticAnalysis(mtDNA);
                                    myShepherd.getPM().makePersistent(mtDNA);
                                    System.out.println("          Added new haplotype.");

                                }
                                enc3.addComments("<p><em>" + request.getRemoteUser() + " on "
                                        + (new java.util.Date()).toString() + "</em><br />"
                                        + "Import SRGD process added or updated mitochondrial DNA analysis (haplotype) "
                                        + mtDNA.getAnalysisID() + " for tissue sample " + ts.getSampleID()
                                        + ".<br />" + mtDNA.getHTMLString());
                                System.out.println("          Added haplotype.");
                            } else {
                                System.out.println("          Did NOT add haplotype.");
                            }
                        }

                        ArrayList<Locus> loci = new ArrayList<Locus>();

                        //loci value import       
                        if (line.length >= 9) {

                            for (int f = 8; f < numColumns; f++) {
                                if (line.length > (f + 2)) {
                                    String l1 = line[f].trim();
                                    String l2 = line[f + 1].trim();
                                    String locusName = headerNames[f].replaceAll("L_", "");

                                    System.out.println("          Loaded loci name.");

                                    //verify that we're looking at the right loci and everything matches up nicely
                                    if ((l1 != null) && (l2 != null) && (!l1.equals("")) && (!l2.equals(""))
                                            && (!locusName.equals(""))
                                            && (headerNames[f].trim().toLowerCase().startsWith("l_"))
                                            && (headerNames[f + 1].trim().toLowerCase().startsWith("l_"))
                                            && (headerNames[f].trim().toLowerCase()
                                                    .equals(headerNames[f + 1].trim().toLowerCase()))) {

                                        //get allele values
                                        Integer intA = new Integer(l1);
                                        Integer intB = new Integer(l2);

                                        Locus myLocus = new Locus(locusName, intA, intB);
                                        loci.add(myLocus);

                                    }

                                    f++;
                                }
                            }
                        }

                        //TBD check if this analysis already exists
                        if (loci.size() > 0) {

                            System.out.println("          Found msMarkers!!!!!!!!!!!!1");

                            MicrosatelliteMarkersAnalysis microAnalysis = new MicrosatelliteMarkersAnalysis(
                                    (ts.getSampleID() + "_msMarkerAnalysis"), ts.getSampleID(),
                                    enc.getCatalogNumber(), loci);

                            if (myShepherd.isGeneticAnalysis(ts.getSampleID(), encNumber,
                                    (ts.getSampleID() + "_msMarkerAnalysis"), "MicrosatelliteMarkers")) {
                                microAnalysis = myShepherd.getMicrosatelliteMarkersAnalysis(ts.getSampleID(),
                                        encNumber, (ts.getSampleID() + "_msMarkerAnalysis"));
                                microAnalysis.setLoci(loci);
                            } else {
                                ts.addGeneticAnalysis(microAnalysis);
                                myShepherd.getPM().makePersistent(microAnalysis);
                            }
                            System.out.println("          Added ms markers.");

                            enc3.addComments("<p><em>" + request.getRemoteUser() + " on "
                                    + (new java.util.Date()).toString() + "</em><br />"
                                    + "Import SRGD process added or updated microsatellite markers of analysis "
                                    + microAnalysis.getAnalysisID() + " for tissue sample " + ts.getSampleID()
                                    + ".<br />" + microAnalysis.getHTMLString());

                        }

                        myShepherd.commitDBTransaction();

                        if (!individualID.equals("")) {
                            MarkedIndividual indie = new MarkedIndividual();
                            myShepherd.beginDBTransaction();

                            Encounter enc2 = myShepherd.getEncounter(encNumber);

                            if (myShepherd.isMarkedIndividual(individualID)) {
                                indie = myShepherd.getMarkedIndividual(individualID);
                                newShark = false;
                            } else {
                                //indie.setIndividualID(individualID);
                            }

                            //OK to generically add it as the addEncounter() method will ignore it if already added to marked individual
                            indie.addEncounter(enc2);

                            if ((indie.getSex() == null)
                                    || ((enc2.getSex() != null) && (indie.getSex() != enc2.getSex()))) {
                                indie.setSex(enc2.getSex());
                                indie.addComments("<p><em>" + request.getRemoteUser() + " on "
                                        + (new java.util.Date()).toString() + "</em><br>"
                                        + "Import SRGD process set sex to " + enc2.getSex() + ".</p>");

                            }

                            if ((indie.getHaplotype() == null) && (enc2.getHaplotype() != null)) {
                                indie.doNotSetLocalHaplotypeReflection(enc2.getHaplotype());
                            }

                            indie.refreshDependentProperties();
                            indie.addComments("<p><em>" + request.getRemoteUser() + " on "
                                    + (new java.util.Date()).toString() + "</em><br>"
                                    + "Import SRGD process added encounter " + enc2.getCatalogNumber()
                                    + ".</p>");

                            myShepherd.commitDBTransaction();
                            if (newShark) {
                                myShepherd.storeNewMarkedIndividual(indie);
                            }
                        }

                    } else {
                        myShepherd.rollbackDBTransaction();
                    }

                    //out.println("Imported row: "+line);

                }

            } else {
                locked = true;
                System.out.println("ImportSRGD: For some reason the import failed without exception.");
            }

        } catch (Exception le) {
            locked = true;
            myShepherd.rollbackDBTransaction();
            myShepherd.closeDBTransaction();
            le.printStackTrace();
        }

        if (!locked) {
            myShepherd.commitDBTransaction();
            myShepherd.closeDBTransaction();
            out.println(ServletUtilities.getHeader(request));
            out.println(
                    "<p><strong>Success!</strong> I have successfully uploaded and imported your SRGD CSV file.</p>");

            if (messages.toString().equals("")) {
                messages.append("None");
            }
            out.println("<p>The following error messages were reported during the import process:<br /><ul>"
                    + messages + "</ul></p>");

            out.println("<p><a href=\"appadmin/import.jsp\">Return to the import page</a></p>");

            out.println(ServletUtilities.getFooter(context));
        }

    } catch (IOException lEx) {
        lEx.printStackTrace();
        out.println(ServletUtilities.getHeader(request));
        out.println(
                "<strong>Error:</strong> I was unable to upload your SRGD CSV. Please contact the webmaster about this message.");
        out.println(ServletUtilities.getFooter(context));
    } catch (NullPointerException npe) {
        npe.printStackTrace();
        out.println(ServletUtilities.getHeader(request));
        out.println("<strong>Error:</strong> I was unable to import SRGD data as no file was specified.");
        out.println(ServletUtilities.getFooter(context));
    }
    out.close();
}

From source file:org.emonocot.harvest.media.ImageMetadataExtractorImpl.java

License:Open Source License

public ImageMetadataExtractorImpl() {

    dateTimeFormatters.add(ISODateTimeFormat.dateTimeParser());
    dateTimeFormatters.add(DateTimeFormat.fullDate());
    dateTimeFormatters.add(DateTimeFormat.fullDateTime());
    dateTimeFormatters.add(DateTimeFormat.shortDate());
    dateTimeFormatters.add(DateTimeFormat.shortDateTime());
    dateTimeFormatters.add(DateTimeFormat.mediumDate());
    dateTimeFormatters.add(DateTimeFormat.mediumDateTime());

}