Example usage for java.io InputStream reset

List of usage examples for java.io InputStream reset

Introduction

In this page you can find the example usage for java.io InputStream reset.

Prototype

public synchronized void reset() throws IOException 

Source Link

Document

Repositions this stream to the position at the time the mark method was last called on this input stream.

Usage

From source file:org.docx4j.openpackaging.parts.JaxbXmlPart.java

/**
* Unmarshal XML data from the specified InputStream and return the
* resulting content tree. Validation event location information may be
* incomplete when using this form of the unmarshal API.
* 
* <p>//from w  ww .  j av a2 s .  com
* Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.
* 
* @param is
*            the InputStream to unmarshal XML data from
* @return the newly created root object of the java content tree
* 
* @throws JAXBException
*             If any unexpected errors occur while unmarshalling
*/
public E unmarshal(java.io.InputStream is) throws JAXBException {

    try {
        /* To avoid possible XML External Entity Injection attack,
         * we need to configure the processor.
         * 
         * We use STAX XMLInputFactory to do that.
         * 
         * createXMLStreamReader(is) is 40% slower than unmarshal(is).
         * 
         * But it seems to be the best we can do ... 
         * 
         *   org.w3c.dom.Document doc = XmlUtils.getNewDocumentBuilder().parse(is)
         *   unmarshal(doc)
         * 
         * ie DOM is 5x slower than unmarshal(is)
         * 
         */

        XMLInputFactory xif = XMLInputFactory.newInstance();
        xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); // a DTD is merely ignored, its presence doesn't cause an exception
        XMLStreamReader xsr = xif.createXMLStreamReader(is);

        Unmarshaller u = jc.createUnmarshaller();

        JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler();
        if (is.markSupported()) {
            // Only fail hard if we know we can restart
            eventHandler.setContinue(false);
        }
        u.setEventHandler(eventHandler);

        try {
            jaxbElement = (E) XmlUtils.unwrap(u.unmarshal(xsr));
        } catch (UnmarshalException ue) {

            if (ue.getLinkedException() != null && ue.getLinkedException().getMessage().contains("entity")) {

                /*
                   Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[10,19]
                   Message: The entity "xxe" was referenced, but not declared.
                      at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
                      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(Unknown Source)
                    */
                log.error(ue.getMessage(), ue);
                throw ue;
            }

            if (is.markSupported()) {
                // When reading from zip, we use a ByteArrayInputStream,
                // which does support this.

                log.info("encountered unexpected content; pre-processing");
                eventHandler.setContinue(true);

                try {
                    Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor();
                    is.reset();
                    JAXBResult result = XmlUtils.prepareJAXBResult(jc);
                    XmlUtils.transform(new StreamSource(is), mcPreprocessorXslt, null, result);
                    jaxbElement = (E) XmlUtils.unwrap(result.getResult());
                } catch (Exception e) {
                    throw new JAXBException("Preprocessing exception", e);
                }

            } else {
                log.error(ue.getMessage(), ue);
                log.error(".. and mark not supported");
                throw ue;
            }
        }

    } catch (XMLStreamException e1) {
        log.error(e1.getMessage(), e1);
        throw new JAXBException(e1);
    }

    return jaxbElement;

}

From source file:ca.sqlpower.architect.swingui.SwingUIProjectLoader.java

public void load(InputStream in, DataSourceCollection<? extends SPDataSource> dataSources,
        ArchitectSession messageDelegate) throws IOException, SQLObjectException {
    olapPaneLoadIdMap = new HashMap<String, OLAPPane<?, ?>>();

    UnclosableInputStream uin = new UnclosableInputStream(in);
    olapObjectLoadIdMap = new HashMap<String, OLAPObject>();

    // sqlObjectLoadIdMap is not ready yet when parsing the olap objects
    // so this keeps track of the id of the SQLDatabase that OLAPSessions reference.
    Map<OLAPSession, String> sessionDbMap = new HashMap<OLAPSession, String>();

    try {//from ww  w .j a  v  a 2  s .co  m
        getSession().getUndoManager().setLoading(true);
        if (uin.markSupported()) {
            uin.mark(Integer.MAX_VALUE);
        } else {
            throw new IllegalStateException("Failed to load with an input stream that does not support mark!");
        }

        // parse the Mondrian business model parts first because the olap id
        // map is needed in the digester for parsing the olap gui
        try {
            MondrianXMLReader.parse(uin, getSession().getOLAPRootObject(), sessionDbMap, olapObjectLoadIdMap);
        } catch (SAXException e) {
            logger.error("Error parsing project file's olap schemas!", e);
            throw new SQLObjectException("SAX Exception in project file olap schemas parse!", e);
        } catch (Exception ex) {
            logger.error("General Exception in project file olap schemas parse!", ex);
            throw new SQLObjectException("Unexpected Exception", ex);
        }

        in.reset();

        super.load(in, dataSources, messageDelegate);
    } finally {
        getSession().getUndoManager().setLoading(false);
        uin.forceClose();
    }

    // now that the sqlObjectLoadIdMap is populated, we can set the
    // OLAPSessions' database.
    for (Map.Entry<OLAPSession, String> entry : sessionDbMap.entrySet()) {
        OLAPSession oSession = entry.getKey();
        SQLDatabase db = (SQLDatabase) sqlObjectLoadIdMap.get(entry.getValue());
        oSession.setDatabase(db);
    }

    // set the view positions again in the case that the viewport was invalid earlier.
    getSession().getPlayPen().setInitialViewPosition();
    for (OLAPEditSession editSession : getSession().getOLAPEditSessions()) {
        editSession.getOlapPlayPen().setInitialViewPosition();
    }

    // TODO change this to load the undo history from a file
    getSession().getUndoManager().discardAllEdits();
}

From source file:org.codelibs.fess.crawler.extractor.impl.TikaExtractor.java

@Override
public ExtractData getText(final InputStream inputStream, final Map<String, String> params) {
    if (inputStream == null) {
        throw new CrawlerSystemException("The inputstream is null.");
    }/*  ww w .ja  va 2 s  .co m*/

    final File tempFile;
    final boolean isByteStream = inputStream instanceof ByteArrayInputStream;
    if (isByteStream) {
        inputStream.mark(0);
        tempFile = null;
    } else {
        try {
            tempFile = File.createTempFile("tikaExtractor-", ".out");
        } catch (final IOException e) {
            throw new ExtractException("Could not create a temp file.", e);
        }
    }

    try {
        final PrintStream originalOutStream = System.out;
        final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outStream, true));
        final PrintStream originalErrStream = System.err;
        final ByteArrayOutputStream errStream = new ByteArrayOutputStream();
        System.setErr(new PrintStream(errStream, true));
        try {
            final String resourceName = params == null ? null : params.get(TikaMetadataKeys.RESOURCE_NAME_KEY);
            final String contentType = params == null ? null : params.get(HttpHeaders.CONTENT_TYPE);
            String contentEncoding = params == null ? null : params.get(HttpHeaders.CONTENT_ENCODING);

            // password for pdf
            String pdfPassword = params == null ? null : params.get(ExtractData.PDF_PASSWORD);
            if (pdfPassword == null && params != null) {
                pdfPassword = getPdfPassword(params.get(ExtractData.URL), resourceName);
            }

            final Metadata metadata = createMetadata(resourceName, contentType, contentEncoding, pdfPassword);

            final Parser parser = new DetectParser();
            final ParseContext parseContext = new ParseContext();
            parseContext.set(Parser.class, parser);

            String content = getContent(writer -> {
                InputStream in = null;
                try {
                    if (!isByteStream) {
                        try (OutputStream out = new FileOutputStream(tempFile)) {
                            CopyUtil.copy(inputStream, out);
                        }
                        in = new FileInputStream(tempFile);
                    } else {
                        in = inputStream;
                    }
                    parser.parse(in, new BodyContentHandler(writer), metadata, parseContext);
                } finally {
                    IOUtils.closeQuietly(in);
                }
            }, contentEncoding);
            if (StringUtil.isBlank(content)) {
                if (resourceName != null) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("retry without a resource name: {}", resourceName);
                    }
                    final Metadata metadata2 = createMetadata(null, contentType, contentEncoding, pdfPassword);
                    content = getContent(writer -> {
                        InputStream in = null;
                        try {
                            if (isByteStream) {
                                inputStream.reset();
                                in = inputStream;
                            } else {
                                in = new FileInputStream(tempFile);
                            }
                            parser.parse(in, new BodyContentHandler(writer), metadata2, parseContext);
                        } finally {
                            IOUtils.closeQuietly(in);
                        }
                    }, contentEncoding);
                }
                if (StringUtil.isBlank(content) && contentType != null) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("retry without a content type: {}", contentType);
                    }
                    final Metadata metadata3 = createMetadata(null, null, contentEncoding, pdfPassword);
                    content = getContent(writer -> {
                        InputStream in = null;
                        try {
                            if (isByteStream) {
                                inputStream.reset();
                                in = inputStream;
                            } else {
                                in = new FileInputStream(tempFile);
                            }
                            parser.parse(in, new BodyContentHandler(writer), metadata3, parseContext);
                        } finally {
                            IOUtils.closeQuietly(in);
                        }
                    }, contentEncoding);
                }

                if (readAsTextIfFailed && StringUtil.isBlank(content)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("read the content as a text.");
                    }
                    if (contentEncoding == null) {
                        contentEncoding = Constants.UTF_8;
                    }
                    final String enc = contentEncoding;
                    content = getContent(writer -> {
                        BufferedReader br = null;
                        try {
                            if (isByteStream) {
                                inputStream.reset();
                                br = new BufferedReader(new InputStreamReader(inputStream, enc));
                            } else {
                                br = new BufferedReader(
                                        new InputStreamReader(new FileInputStream(tempFile), enc));
                            }
                            String line;
                            while ((line = br.readLine()) != null) {
                                writer.write(line);
                            }
                        } catch (final Exception e) {
                            logger.warn(
                                    "Could not read "
                                            + (tempFile != null ? tempFile.getAbsolutePath() : "a byte stream"),
                                    e);
                        } finally {
                            IOUtils.closeQuietly(br);
                        }
                    }, contentEncoding);
                }
            }
            final ExtractData extractData = new ExtractData(content);

            final String[] names = metadata.names();
            Arrays.sort(names);
            for (final String name : names) {
                extractData.putValues(name, metadata.getValues(name));
            }

            if (logger.isDebugEnabled()) {
                logger.debug("Result: metadata: {}", metadata);
            }

            return extractData;
        } catch (final TikaException e) {
            if (e.getMessage().indexOf("bomb") >= 0) {
                throw e;
            }
            final Throwable cause = e.getCause();
            if (cause instanceof SAXException) {
                final Extractor xmlExtractor = crawlerContainer.getComponent("xmlExtractor");
                if (xmlExtractor != null) {
                    InputStream in = null;
                    try {
                        if (isByteStream) {
                            inputStream.reset();
                            in = inputStream;
                        } else {
                            in = new FileInputStream(tempFile);
                        }
                        return xmlExtractor.getText(in, params);
                    } finally {
                        IOUtils.closeQuietly(in);
                    }
                }
            }
            throw e;
        } finally {
            if (originalOutStream != null) {
                System.setOut(originalOutStream);
            }
            if (originalErrStream != null) {
                System.setErr(originalErrStream);
            }
            try {
                if (logger.isInfoEnabled()) {
                    final byte[] bs = outStream.toByteArray();
                    if (bs.length != 0) {
                        logger.info(new String(bs, outputEncoding));
                    }
                }
                if (logger.isWarnEnabled()) {
                    final byte[] bs = errStream.toByteArray();
                    if (bs.length != 0) {
                        logger.warn(new String(bs, outputEncoding));
                    }
                }
            } catch (final Exception e) {
                // NOP
            }
        }
    } catch (final Exception e) {
        throw new ExtractException("Could not extract a content.", e);
    } finally {
        if (tempFile != null && !tempFile.delete()) {
            logger.warn("Failed to delete " + tempFile.getAbsolutePath());
        }
    }
}

From source file:au.org.theark.study.util.DataUploader.java

/**
 * Imports the subject data file to the database tables, and creates report on the process Assumes the file is in the default "matrix" file format:
 * SUBJECTUID,FIELD1,FIELD2,FIELDN... 1,01/01/1900,99.99,99.99,, ...
 * /*w  w  w.  jav a 2s .c  om*/
 * Where N is any number of columns
 * 
 * @param fileInputStream
 *           is the input stream of a file
 * @param inLength
 *           is the length of a file
 * @throws FileFormatException
 *            file format Exception
 * @throws ArkBaseException
 *            general ARK Exception
 * @return the upload report detailing the upload process
 **/
@SuppressWarnings("unchecked")
public StringBuffer uploadAndReportMatrixSubjectFile(InputStream fileInputStream, long inLength,
        String inFileFormat, char inDelimChr, List<String> uidsWhichNeedUpdating)
        throws FileFormatException, ArkSystemException {
    List<LinkSubjectStudy> insertSubjects = new ArrayList<LinkSubjectStudy>();
    List<LinkSubjectStudy> updateSubjects = new ArrayList<LinkSubjectStudy>();
    long rowCount = 0;
    long subjectCount = 0;
    long insertCount = 0;
    long updateCount = 0;
    long srcLength = -1; // -1 means nothing being processed
    delimiterCharacter = inDelimChr;
    uploadReport = new StringBuffer();

    InputStreamReader inputStreamReader = null;
    CsvReader csvReader = null;
    DecimalFormat decimalFormat = new DecimalFormat("0.00");

    // If Excel, convert to CSV for validation
    if (inFileFormat.equalsIgnoreCase("XLS")) {
        Workbook w;
        try {
            w = Workbook.getWorkbook(fileInputStream);
            delimiterCharacter = ',';
            XLStoCSV xlsToCsv = new XLStoCSV(delimiterCharacter);
            fileInputStream = xlsToCsv.convertXlsToCsv(w);
            fileInputStream.reset();
        } catch (BiffException e) {
            log.error(e.getMessage());
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }

    try {
        inputStreamReader = new InputStreamReader(fileInputStream);
        csvReader = new CsvReader(inputStreamReader, delimiterCharacter);
        String[] stringLineArray;

        // this is a list of all our somewhat enum-like reference tables...
        // much better to call this once than each one n times in the for loop...plus each ones default is n times
        // should save 200,000-250,000 selects for a 17K insert. may still wish to evaluate whats best here
        Collection<MaritalStatus> maritalStatiiPossible = iArkCommonService.getMaritalStatus();
        Collection<EmailStatus> emailStatiiPossible = iArkCommonService.getAllEmailStatuses();
        Collection<SubjectStatus> subjectStatiiPossible = iArkCommonService.getSubjectStatus();
        Collection<GenderType> genderTypesPossible = iArkCommonService.getGenderTypes();
        Collection<TitleType> titleTypesPossible = iArkCommonService.getTitleType();
        List<AddressType> addressTypesPossible = iArkCommonService.getAddressTypes();
        List<AddressStatus> addressStatiiPossible = iArkCommonService.getAddressStatuses();
        List<PhoneType> phoneTypesPossible = iArkCommonService.getPhoneTypes();
        List<PhoneStatus> phoneStatiiPossible = iArkCommonService.getPhoneStatuses();
        List<Country> countriesPossible = iArkCommonService.getCountries();
        // List<State> statesPossible = iArkCommonService.getStates(country);

        Collection<VitalStatus> vitalStatiiPossible = iArkCommonService.getVitalStatus();
        Collection<PersonContactMethod> personContactMethodPossible = iArkCommonService
                .getPersonContactMethodList();
        // Collection<MaritalStatus> yesNoList = iArkCommonService.getYesNoList(); //TODO would boolean not be better?
        YesNo yes = iArkCommonService.getYes();// TODO: boolean
        YesNo no = iArkCommonService.getNo();

        // things inherant... "CONSENT_ID", "CONSENT_STUDY_ID", "CONSENT_LINK_SUBJECT_STUDY_ID",
        // things needed from file... "CONSENT_STUDY_COMP_ID", "CONSENT_STUDY_COMP_STATUS_ID", "CONSENT_CONSENT_STATUS_ID",
        // "CONSENT_CONSENT_TYPE_ID", "CONSENT_CONSENT_DATE",
        // "CONSENT_CONSENTED_BY", "CONSENT_COMMENTS", "CONSENT_REQUESTED_DATE", "CONSENT_RECEIVED_DATE", "CONSENT_COMPLETED_DATE",
        // "CONSENT_CONSENT_DOWNLOADED_ID"?
        /*
         * int consentComponentIndex = csvReader.getIndex("CONSENT_STUDY_COMP_ID"); int consentComponentStatusIndex =
         * csvReader.getIndex("CONSENT_STUDY_COMP_STATUS_ID"); int consentStatusIndex = csvReader.getIndex("CONSENT_CONSENT_STATUS_ID"); int
         * consentTypeIndex = csvReader.getIndex("CONSENT_CONSENT_TYPE_ID"); int consentDateIndex = csvReader.getIndex("CONSENT_CONSENT_DATE"); int
         * consentByIndex = csvReader.getIndex("CONSENT_CONSENTED_BY"); int consentCommentsIndex = csvReader.getIndex("CONSENT_COMMENTS"); int
         * consentRequestedDateIndex = csvReader.getIndex("CONSENT_REQUESTED_DATE"); int consentReceivedDateIndex =
         * csvReader.getIndex("CONSENT_RECEIVED_DATE"); int consentCompletedDateIndex = csvReader.getIndex("CONSENT_COMPLETED_DATE"); //???
         * "CONSENT_CONSENT_DOWNLOADED_ID";
         * 
         * 
         * 
         * 
         * "CONSENT_TO_ACTIVE_CONTACT_ID", "CONSENT_TO_PASSIVE_DATA_GATHERING_ID", "CONSENT_TO_USE_DATA_ID", "CONSENT_STATUS_ID", "CONSENT_TYPE_ID",
         * "CONSENT_DATE", "CONSENT_DOWNLOADED" consent_option c_o c_o c_status c_type date study.yes_no each of these appears to have a link to
         * consent_option table which has a number 1-6 for id each representing a status...there appears to be no default...therefore may need
         * validation
         * 
         * the diffrence being consent statuys and consent type link to consent_status and consent_type tables...so look ups and validation needed
         */

        boolean autoConsent = study.getAutoConsent();
        SubjectStatus defaultSubjectStatus = iStudyService.getDefaultSubjectStatus();
        TitleType defaultTitleType = iStudyService.getDefaultTitleType();
        AddressType defaultAddressType = iStudyService.getDefaultAddressType();
        AddressStatus defaultAddressStatus = iStudyService.getDefaultAddressStatus();
        PhoneType defaultPhoneType = iStudyService.getDefaultPhoneType();
        PhoneStatus defaultPhoneStatus = iStudyService.getDefaultPhoneStatus();
        GenderType defaultGenderType = iStudyService.getDefaultGenderType();
        VitalStatus defaultVitalStatus = iStudyService.getDefaultVitalStatus();
        MaritalStatus defaultMaritalStatus = iStudyService.getDefaultMaritalStatus();
        EmailStatus defaultEmailStatus = iStudyService.getDefaultEmailStatus();
        ConsentOption concentOptionOfYes = iStudyService.getConsentOptionForBoolean(true);// sounds a lot like boolean blah = true????
        ConsentStatus consentStatusOfConsented = iStudyService.getConsentStatusByName("Consented");
        ConsentType consentTypeOfElectronic = iStudyService.getConsentTypeByName("Electronic");

        List<ConsentOption> consentOptionsPossible = iStudyService.getConsentOptions();
        List<ConsentStatus> consentStatusPossible = iStudyService.getConsentStatus();
        List<ConsentType> consentTypePossible = iStudyService.getConsentType();

        List<LinkSubjectStudy> allSubjectWhichWillBeUpdated = null;
        if (uidsWhichNeedUpdating.size() > 0) {
            // TODO analyse performance of bringing all back and having to iterate everytime, vs conditional query + looping through less
            // TODO analyze performance of getting that big list of UIDs and doing a .contains(subjectuid) VS getting all the entities and doing a
            // .getSubjectUID.equals(subjectUID)
            allSubjectWhichWillBeUpdated = iArkCommonService.getUniqueSubjectsWithTheseUIDs(study,
                    uidsWhichNeedUpdating);
        } else {
            allSubjectWhichWillBeUpdated = new ArrayList();
        }

        srcLength = inLength;
        if (srcLength <= 0) {
            uploadReport.append("ERROR:  The input size was not greater than 0. Actual length reported: ");
            uploadReport.append(srcLength);
            uploadReport.append("\n");
            throw new FileFormatException(
                    "The input size was not greater than 0. Actual length reported: " + srcLength);
        }

        csvReader.readHeaders();
        srcLength = inLength - csvReader.getHeaders().toString().length();
        int firstNameIndex = csvReader.getIndex("FIRST_NAME");
        int middleNameIndex = csvReader.getIndex("MIDDLE_NAME");
        int lastNameIndex = csvReader.getIndex("LAST_NAME");
        int previousLastNameIndex = csvReader.getIndex("PREVIOUS_LAST_NAME");
        int preferredNameIndex = csvReader.getIndex("PREFERRED_NAME");
        int preferredEmailIndex = csvReader.getIndex("EMAIL");
        int preferredEmailStatusIndex = csvReader.getIndex("EMAIL_STATUS");
        int otherEmailIndex = csvReader.getIndex("OTHER_EMAIL");
        int otherEmailStatusIndex = csvReader.getIndex("OTHER_EMAIL_STATUS");
        int heardAboutStudyIndex = csvReader.getIndex("HEARD_ABOUT_STUDY");
        int commentsIndex = csvReader.getIndex("COMMENTS");
        int titleIndex = csvReader.getIndex("TITLE");
        int vitalStatusIndex = csvReader.getIndex("VITAL_STATUS");
        int maritalStatusIndex = csvReader.getIndex("MARITAL_STATUS");
        int statusIndex = csvReader.getIndex("STATUS");

        int addressLine1Index = csvReader.getIndex("BUILDING_NAME");
        int addressLine2Index = csvReader.getIndex("STREET_ADDRESS");
        int suburbIndex = csvReader.getIndex("SUBURB");
        int stateIndex = csvReader.getIndex("STATE");
        int countryIndex = csvReader.getIndex("COUNTRY");
        int postCodeIndex = csvReader.getIndex("POST_CODE");
        int addressSourceIndex = csvReader.getIndex("ADDRESS_SOURCE");
        int addressStatusIndex = csvReader.getIndex("ADDRESS_STATUS");
        int addressTypeIndex = csvReader.getIndex("ADDRESS_TYPE");
        int addressReceivedIndex = csvReader.getIndex("ADDRESS_DATE_RECEIVED");
        int addressCommentsIndex = csvReader.getIndex("ADDRESS_COMMENTS");
        int isPreferredMailingIndex = csvReader.getIndex("IS_PREFERRED_MAILING_ADDRESS");

        int phoneNumberIndex = csvReader.getIndex("PHONE_NUMBER");
        int areaCodeIndex = csvReader.getIndex("PHONE_AREA_CODE");
        int phoneStatusIndex = csvReader.getIndex("PHONE_STATUS");
        int phoneTypeIndex = csvReader.getIndex("PHONE_TYPE");
        int phoneSourceIndex = csvReader.getIndex("PHONE_SOURCE");
        int phoneCommentsIndex = csvReader.getIndex("PHONE_COMMENTS");
        int phoneDateReceivedIndex = csvReader.getIndex("PHONE_DATE_RECEIVED");
        int phoneSilentIndex = csvReader.getIndex("SILENT");

        // if(PERSON_CONTACT_METHOD is in headers, use it,
        // else, if CONTACT_METHOD, us IT, else, just set to -1
        int personContactIndex = ((csvReader.getIndex("PERSON_CONTACT_METHOD") > 0)
                ? csvReader.getIndex("PERSON_CONTACT_METHOD")
                : ((csvReader.getIndex("CONTACT_METHOD") > 0) ? csvReader.getIndex("CONTACT_METHOD") : -1));
        int dateOfBirthIndex = ((csvReader.getIndex("DATE_OF_BIRTH") > 0) ? csvReader.getIndex("DATE_OF_BIRTH")
                : ((csvReader.getIndex("DOB") > 0) ? csvReader.getIndex("DOB") : -1));
        int dateOfDeathIndex = ((csvReader.getIndex("DATE_OF_DEATH") > 0) ? csvReader.getIndex("DATE_OF_DEATH")
                : ((csvReader.getIndex("DODEATH") > 0) ? csvReader.getIndex("DODEATH") : -1));
        int dateLastKnownAliveIndex = ((csvReader.getIndex("DATE_LAST_KNOWN_ALIVE") > 0)
                ? csvReader.getIndex("DATE_LAST_KNOWN_ALIVE")
                : ((csvReader.getIndex("LAST_KNOWN_ALIVE") > 0) ? csvReader.getIndex("LAST_KNOWN_ALIVE") : -1));
        int causeOfDeathIndex = ((csvReader.getIndex("CAUSE_OF_DEATH") > 0)
                ? csvReader.getIndex("CAUSE_OF_DEATH")
                : ((csvReader.getIndex("CODEATH") > 0) ? csvReader.getIndex("CODEATH") : -1));
        // in reality, validation doesnt permit this yet anyway...but probably not bad to align it over in validation
        int genderIndex = ((csvReader.getIndex("GENDER_TYPE") > 0) ? csvReader.getIndex("GENDER_TYPE")
                : ((csvReader.getIndex("GENDER") > 0) ? csvReader.getIndex("GENDER")
                        : ((csvReader.getIndex("SEX") > 0) ? csvReader.getIndex("SEX") : -1)));

        boolean isAutoGen = study.getAutoGenerateSubjectUid();

        while (csvReader.readRecord()) {
            rowCount++;
            LinkSubjectStudy subject = null;

            // Hack to ensure XLS rows contain all cells (ie empty strings for cells after the right-hand non-null value
            List<String> stringList = new ArrayList<String>(csvReader.getHeaders().length);
            for (int i = 0; i < csvReader.getHeaders().length; i++) {
                stringList.add(csvReader.get(i));
            }
            stringLineArray = stringList.toArray(new String[csvReader.getHeaders().length]);
            String subjectUID = stringLineArray[0];

            boolean hasSomeData = false;
            for (String next : stringLineArray) {
                if (next != null && !next.isEmpty()) {
                    hasSomeData = true;
                }
            }

            if (!isAutoGen && (subjectUID == null || subjectUID.isEmpty())) {
                if (!hasSomeData) {
                    uploadReport.append("Warning/Info: Row " + rowCount
                            + ":  There appeared to be no data on this row, so we ignored this line");
                } else {
                    // THIS SHOULD NEVER EVER HAPPEN IF VALIDATION IS CORRECT
                    uploadReport.append("Error: Row " + rowCount + ":  There is no subject UID on this row, "
                            + "yet the study is not set up to auto generate subject UIDs.  This line was ignored.  Please remove this line or provide an ID");
                }
            } else if (isAutoGen && (subjectUID == null || subjectUID.isEmpty()) && !hasSomeData) {
                uploadReport.append("Warning/Info: Row " + rowCount
                        + ":  There appeared to be no data on this row, so we ignored this line");
            } else {

                subject = getSubjectByUIDFromExistList(allSubjectWhichWillBeUpdated, subjectUID);
                boolean thisSubjectAlreadyExists = (subject != null);

                Person person = null;
                if (thisSubjectAlreadyExists) {
                    person = subject.getPerson();
                } else {
                    subject = new LinkSubjectStudy();
                    subject.setSubjectUID(subjectUID);// note: this will be overwritten IF study.isautogenerate
                    subject.setStudy(study);
                    person = new Person();
                }

                if (firstNameIndex > 0)
                    person.setFirstName(stringLineArray[firstNameIndex]);

                if (heardAboutStudyIndex > 0)
                    subject.setHeardAboutStudy(stringLineArray[heardAboutStudyIndex]);

                if (commentsIndex > 0)
                    subject.setComment(stringLineArray[commentsIndex]);

                if (middleNameIndex > 0)
                    person.setMiddleName(stringLineArray[middleNameIndex]);

                if (lastNameIndex > 0) {
                    String lastnameFromFile = stringLineArray[lastNameIndex];

                    if (thisSubjectAlreadyExists && lastnameFromFile != null && !lastnameFromFile.isEmpty()
                            && !lastnameFromFile.equalsIgnoreCase(person.getLastName())
                            && person.getLastName() != null) {
                        PersonLastnameHistory personLastNameHistory = new PersonLastnameHistory();
                        personLastNameHistory.setPerson(person);
                        personLastNameHistory.setLastName(person.getLastName());
                        person.getPersonLastnameHistory().add(personLastNameHistory);// TODO analyze this
                    } else if (!thisSubjectAlreadyExists) {

                        if (previousLastNameIndex > 0) {
                            String previousLastName = (stringLineArray[previousLastNameIndex]);

                            if (previousLastName != null && !previousLastName.isEmpty()) {
                                PersonLastnameHistory personLastNameHistory = new PersonLastnameHistory();
                                personLastNameHistory.setPerson(person);
                                personLastNameHistory.setLastName(previousLastName);
                                person.getPersonLastnameHistory().add(personLastNameHistory);
                            }
                        }
                    }

                    person.setLastName(lastnameFromFile);
                } else {
                    // just in the odd instance of no last name but has previous lastname known
                    if (!thisSubjectAlreadyExists) {
                        if (previousLastNameIndex > 0) {
                            String previousLastName = (stringLineArray[previousLastNameIndex]);
                            if (previousLastName != null && !previousLastName.isEmpty()) {
                                PersonLastnameHistory personLastNameHistory = new PersonLastnameHistory();
                                personLastNameHistory.setPerson(person);
                                personLastNameHistory.setLastName(previousLastName);
                                person.getPersonLastnameHistory().add(personLastNameHistory);
                            }
                        }
                    }
                }

                if (preferredNameIndex > 0) {
                    person.setPreferredName(stringLineArray[preferredNameIndex]);
                }

                if (genderIndex > 0) {
                    if (stringLineArray[genderIndex] != null && stringLineArray[genderIndex].length() > 0) {
                        for (GenderType boygirl : genderTypesPossible) {
                            if (boygirl.getName().equalsIgnoreCase(stringLineArray[genderIndex])) {
                                person.setGenderType(boygirl);
                            }
                        }
                    }
                    if (person.getGenderType() == null
                            || StringUtils.isBlank(person.getGenderType().getName())) {
                        person.setGenderType(defaultGenderType);
                    }
                }
                if (person.getGenderType() == null) {
                    person.setGenderType(defaultGenderType);
                }

                if (dateOfBirthIndex > 0) {
                    Date dateOfBirth = new Date();

                    if (stringLineArray[dateOfBirthIndex] != null
                            && stringLineArray[dateOfBirthIndex].length() > 0) {
                        dateOfBirth = simpleDateFormat.parse(stringLineArray[dateOfBirthIndex]);
                        person.setDateOfBirth(dateOfBirth);
                    }
                }

                if (dateOfDeathIndex > 0) {
                    Date dateOfDeath = new Date();
                    if (stringLineArray[dateOfDeathIndex] != null
                            && stringLineArray[dateOfDeathIndex].length() > 0) {
                        dateOfDeath = simpleDateFormat.parse(stringLineArray[dateOfDeathIndex]);
                        person.setDateOfDeath(dateOfDeath);
                    }
                }

                if (dateLastKnownAliveIndex > 0) {
                    Date dateLastKnownAlive = new Date();
                    if (stringLineArray[dateLastKnownAliveIndex] != null
                            && stringLineArray[dateLastKnownAliveIndex].length() > 0) {
                        dateLastKnownAlive = simpleDateFormat.parse(stringLineArray[dateLastKnownAliveIndex]);
                        person.setDateLastKnownAlive(dateLastKnownAlive);
                    }
                }

                if (causeOfDeathIndex > 0) {
                    if (stringLineArray[causeOfDeathIndex] != null
                            && stringLineArray[causeOfDeathIndex].length() > 0) {
                        person.setCauseOfDeath(stringLineArray[causeOfDeathIndex]);
                    }
                }

                if (vitalStatusIndex > 0) {
                    String vitalStatusStr = (stringLineArray[vitalStatusIndex]);
                    for (VitalStatus vitalStat : vitalStatiiPossible) {
                        if (vitalStat.getName().equalsIgnoreCase(vitalStatusStr)) {
                            person.setVitalStatus(vitalStat);
                        }
                    }
                    if (person.getVitalStatus() == null
                            || StringUtils.isBlank(person.getVitalStatus().getName())) {
                        person.setVitalStatus(defaultVitalStatus);
                    }
                }
                if (person.getVitalStatus() == null) {
                    person.setVitalStatus(defaultVitalStatus);
                }

                if (preferredEmailIndex > 0) {
                    person.setPreferredEmail(stringLineArray[preferredEmailIndex]);
                }

                if (otherEmailIndex > 0) {
                    person.setOtherEmail(stringLineArray[otherEmailIndex]);
                }

                if (preferredEmailStatusIndex > 0) {
                    String preferredEmailStatusStr = (stringLineArray[preferredEmailStatusIndex]);
                    for (EmailStatus possibleEmailStat : emailStatiiPossible) {
                        if (possibleEmailStat.getName().equalsIgnoreCase(preferredEmailStatusStr)) {
                            person.setPreferredEmailStatus(possibleEmailStat);
                        }
                    }
                    if (person.getPreferredEmailStatus() == null
                            || StringUtils.isBlank(person.getPreferredEmailStatus().getName())) {
                        person.setPreferredEmailStatus(defaultEmailStatus);
                    }
                }
                if (person.getPreferredEmailStatus() == null) {
                    person.setPreferredEmailStatus(defaultEmailStatus);
                }

                if (otherEmailStatusIndex > 0) {
                    String OtherEmailStatusStr = (stringLineArray[otherEmailStatusIndex]);
                    for (EmailStatus possibleEmailStat : emailStatiiPossible) {
                        if (possibleEmailStat.getName().equalsIgnoreCase(OtherEmailStatusStr)) {
                            person.setOtherEmailStatus(possibleEmailStat);
                        }
                    }
                    if (person.getOtherEmailStatus() == null
                            || StringUtils.isBlank(person.getOtherEmailStatus().getName())) {
                        person.setOtherEmailStatus(defaultEmailStatus);
                    }
                }
                if (person.getOtherEmailStatus() == null) {
                    person.setOtherEmailStatus(defaultEmailStatus);
                }

                if (titleIndex > 0) {
                    String titleStr = (stringLineArray[titleIndex]);
                    for (TitleType titleType : titleTypesPossible) {
                        if (titleType.getName().equalsIgnoreCase(titleStr)) {
                            person.setTitleType(titleType);
                        }
                    }
                    if (person.getTitleType() == null || StringUtils.isBlank(person.getTitleType().getName())) {
                        person.setTitleType(defaultTitleType);
                    }
                }
                if (person.getTitleType() == null) {
                    person.setTitleType(defaultTitleType);
                }

                if (maritalStatusIndex > 0) {
                    String maritalStatusStr = (stringLineArray[maritalStatusIndex]);
                    for (MaritalStatus maritalStat : maritalStatiiPossible) {
                        if (maritalStat.getName().equalsIgnoreCase(maritalStatusStr)) {
                            person.setMaritalStatus(maritalStat);
                        }
                    }
                    if (person.getMaritalStatus() == null
                            || StringUtils.isBlank(person.getMaritalStatus().getName())) {
                        person.setMaritalStatus(defaultMaritalStatus);
                    }
                }
                if (person.getMaritalStatus() == null) {
                    person.setMaritalStatus(defaultMaritalStatus);
                }

                if (personContactIndex > 0) {
                    String personContactMethodStr = null;
                    personContactMethodStr = (stringLineArray[personContactIndex]);
                    for (PersonContactMethod possibleMethod : personContactMethodPossible) {
                        if (possibleMethod.getName().equalsIgnoreCase(personContactMethodStr)) {
                            person.setPersonContactMethod(possibleMethod);
                        }
                    } // TODO if we get to the end and personcontactmethod doesnt exist...what do we do? do we want a default or does it get ignored
                }

                if (statusIndex > 0) {
                    String statusStr = (stringLineArray[statusIndex]);
                    for (SubjectStatus subjectStat : subjectStatiiPossible) {
                        if (subjectStat.getName().equalsIgnoreCase(statusStr)) {
                            subject.setSubjectStatus(subjectStat);
                        }
                    }
                    if (subject.getSubjectStatus() == null
                            || StringUtils.isBlank(subject.getSubjectStatus().getName())) {
                        subject.setSubjectStatus(defaultSubjectStatus);
                    }

                } else {
                    subject.setSubjectStatus(defaultSubjectStatus);
                }

                // if the study is autoconsent...then there are some defaults we have to set TODO get rid of hardcoding
                subject.setUpdateConsent(false);
                if (autoConsent && subject.getSubjectStatus().getName().equalsIgnoreCase("Subject")) {
                    subject.setConsentDate(new Date());
                    subject.setConsentStatus(consentStatusOfConsented);
                    subject.setConsentType(consentTypeOfElectronic);

                    ConsentOption defaultConsentOption = concentOptionOfYes;
                    subject.setConsentToActiveContact(defaultConsentOption);
                    subject.setConsentToPassiveDataGathering(defaultConsentOption);
                    subject.setConsentToUseData(defaultConsentOption);
                } else {
                    // Manual Consent details
                    String consentDate = csvReader.get("CONSENT_DATE");
                    String consentStatusStr = csvReader.get("CONSENT_STATUS");
                    String consentTypeStr = csvReader.get("CONSENT_TYPE");
                    String passiveDataStr = csvReader.get("CONSENT_TO_PASSIVE_DATA_GATHERING");
                    String activeContactStr = csvReader.get("CONSENT_TO_ACTIVE_CONTACT");
                    String useDataStr = csvReader.get("CONSENT_TO_USE_DATA");

                    if (!consentDate.isEmpty() || !consentStatusStr.isEmpty() || !consentTypeStr.isEmpty()
                            || !passiveDataStr.isEmpty() || !activeContactStr.isEmpty()
                            || !useDataStr.isEmpty()) {
                        LinkSubjectStudy newSubject = new LinkSubjectStudy();

                        if (!consentDate.isEmpty()) {
                            newSubject.setConsentDate(simpleDateFormat.parse(consentDate));
                        }

                        if (!consentStatusStr.isEmpty()) {
                            for (ConsentStatus consentStatus : consentStatusPossible) {
                                if (consentStatus.getName().equalsIgnoreCase(consentStatusStr)) {
                                    newSubject.setConsentStatus(consentStatus);
                                }
                            }
                        }

                        if (!consentTypeStr.isEmpty()) {
                            for (ConsentType consentType : consentTypePossible) {
                                if (consentType.getName().equalsIgnoreCase(consentTypeStr)) {
                                    newSubject.setConsentType(consentType);
                                }
                            }
                        }

                        if (!passiveDataStr.isEmpty() || !activeContactStr.isEmpty() || !useDataStr.isEmpty()) {
                            for (ConsentOption consentOption : consentOptionsPossible) {
                                if (consentOption.getName().equalsIgnoreCase(passiveDataStr)) {
                                    newSubject.setConsentToPassiveDataGathering(consentOption);
                                }

                                if (consentOption.getName().equalsIgnoreCase(activeContactStr)) {
                                    newSubject.setConsentToActiveContact(consentOption);
                                }

                                if (consentOption.getName().equalsIgnoreCase(useDataStr)) {
                                    newSubject.setConsentToUseData(consentOption);
                                }
                            }
                        }

                        if (thisSubjectAlreadyExists) {
                            // Existing Subject to compare if consent actually changed (inherently handles when no consent previously)
                            LinkSubjectStudyConsentHistoryComparator comparator = new LinkSubjectStudyConsentHistoryComparator();
                            if (comparator.compare(subject, newSubject) != 0) {
                                subject.setUpdateConsent(true);
                                subject.setConsentDate(newSubject.getConsentDate());
                                subject.setConsentStatus(newSubject.getConsentStatus());
                                subject.setConsentType(newSubject.getConsentType());
                                subject.setConsentToPassiveDataGathering(
                                        newSubject.getConsentToPassiveDataGathering());
                                subject.setConsentToActiveContact(newSubject.getConsentToActiveContact());
                                subject.setConsentToUseData(newSubject.getConsentToUseData());
                            } else {
                                subject.setUpdateConsent(false);
                            }
                        } else {
                            // New Subject with consent details
                            subject.setConsentDate(newSubject.getConsentDate());
                            subject.setConsentStatus(newSubject.getConsentStatus());
                            subject.setConsentType(newSubject.getConsentType());
                            subject.setConsentToPassiveDataGathering(
                                    newSubject.getConsentToPassiveDataGathering());
                            subject.setConsentToActiveContact(newSubject.getConsentToActiveContact());
                            subject.setConsentToUseData(newSubject.getConsentToUseData());
                        }
                    }
                }

                // if no address info - ignore
                if (addressLine1Index > 0 || addressLine1Index > 0) {
                    boolean updateAddress = false;
                    String address1String = stringLineArray[addressLine1Index];
                    String address2String = stringLineArray[addressLine2Index];

                    String suburb = stringLineArray[suburbIndex];
                    String countryString = stringLineArray[countryIndex];
                    String stateString = stringLineArray[stateIndex];
                    String postCode = stringLineArray[postCodeIndex];

                    if ((address1String == null || StringUtils.isBlank(address1String))
                            && (address2String == null || StringUtils.isBlank(address2String))
                            && (suburb == null || StringUtils.isBlank(suburb))
                            && (postCode == null || StringUtils.isBlank(postCode))
                            && (countryString == null || StringUtils.isBlank(countryString))
                            && (stateString == null || StringUtils.isBlank(stateString))) {
                        // then lets just jump out as there is no address to validate. lay down to user that they must have data if they want an update
                    } else {
                        boolean usingDefaultType = false;
                        boolean usingDefaultStatus = false;
                        Address addressToAttachToPerson = null;
                        if (thisSubjectAlreadyExists) {
                            String typeString = null;
                            String statusString = null;

                            if (addressTypeIndex > 0) {
                                typeString = stringLineArray[addressTypeIndex];
                                if (typeString == null || typeString.isEmpty()) {
                                    typeString = defaultAddressType.getName();
                                    usingDefaultType = true;
                                }

                            }
                            if (addressStatusIndex > 0) {
                                statusString = stringLineArray[addressStatusIndex];
                                if (statusString == null || statusString.isEmpty()) {
                                    statusString = defaultPhoneStatus.getName();
                                    usingDefaultStatus = true;
                                }
                            }

                            for (Address a : person.getAddresses()) {
                                if (a.getAddressStatus().getName().equalsIgnoreCase(statusString)
                                        && a.getAddressType().getName().equalsIgnoreCase(typeString)) {

                                    addressToAttachToPerson = a;
                                    updateAddress = true;

                                }
                            }
                        }

                        if (addressToAttachToPerson == null) {
                            log.info("address was null");
                            addressToAttachToPerson = new Address();
                        } else {
                            log.info("address was not null");
                        }

                        AddressType type = findAddressType(addressTypesPossible,
                                stringLineArray[addressTypeIndex]);
                        if (type == null) {
                            type = defaultAddressType;
                            usingDefaultType = true;
                        }
                        AddressStatus status = findAddressStatus(addressStatiiPossible,
                                stringLineArray[addressTypeIndex]);
                        if (status == null) {
                            status = defaultAddressStatus;
                            usingDefaultStatus = true;
                        }
                        String addressComments = stringLineArray[addressCommentsIndex];
                        Country country = findCountry(countriesPossible, countryString);
                        if (country != null) {
                            addressToAttachToPerson.setCountry(country);
                            // TODO one option: all possible states locally and test where it matches might work...or lets see how the entity goes first,
                            // and if it hits db again!
                            // State state = findState(statesPossible, stateString, country);
                            State state = findStateWithinThisCountry(stateString, country);
                            if (state == null) {
                                uploadReport.append("Warning/Info: could not find a state named '" + stateString
                                        + "' in " + country.getName() + " for row " + rowCount
                                        + ", but will proceed.\n");
                                addressToAttachToPerson.setOtherState(stateString);
                            } else {
                                addressToAttachToPerson.setState(state);
                            }
                        } else {
                            uploadReport.append("Warning/Info:  Could not find country '" + countryString
                                    + " for row " + rowCount + ", but will proceed.\n");
                        }

                        String addressSource = stringLineArray[addressSourceIndex];
                        String dateReceivedString = stringLineArray[addressReceivedIndex];
                        String isPreferredMailingString = stringLineArray[isPreferredMailingIndex];

                        addressToAttachToPerson.setAddressType(type);
                        addressToAttachToPerson.setAddressStatus(status);
                        if (postCode != null && !postCode.isEmpty())
                            addressToAttachToPerson.setPostCode(postCode);
                        if (address1String != null && !address1String.isEmpty())
                            addressToAttachToPerson.setAddressLineOne(address1String);
                        if (address2String != null && !address2String.isEmpty())
                            addressToAttachToPerson.setStreetAddress(address2String);// yeah..
                        if (dateReceivedString != null && !dateReceivedString.isEmpty()) {
                            // TODO dateconvert and set
                            Date dateReceived = new Date();
                            dateReceived = simpleDateFormat.parse(dateReceivedString);
                            addressToAttachToPerson.setDateReceived(dateReceived);
                        }
                        if (suburb != null && !suburb.isEmpty())
                            addressToAttachToPerson.setCity(suburb);
                        if (addressComments != null && !addressComments.isEmpty())
                            addressToAttachToPerson.setComments(addressComments);

                        if (DataConversionAndManipulationHelper
                                .isSomethingLikeABoolean(isPreferredMailingString)) {
                            if (DataConversionAndManipulationHelper
                                    .isSomethingLikeTrue(isPreferredMailingString)) { // isPreferredMailingString!=null &&
                                                                                                                             // !isPreferredMailingString.isEmpty()){
                                addressToAttachToPerson.setPreferredMailingAddress(true);
                            } else {
                                addressToAttachToPerson.setPreferredMailingAddress(false);
                            }
                        } else {
                            addressToAttachToPerson.setPreferredMailingAddress(false);
                        }

                        if (usingDefaultStatus && usingDefaultType) {
                            uploadReport
                                    .append("Info:  Using the default status '" + defaultAddressStatus.getName()
                                            + "' and the default type '" + defaultAddressType.getName()
                                            + " for row " + rowCount + ", but will proceed.\n");
                        } else if (usingDefaultType) {
                            uploadReport.append("Info:  Using the default type '" + defaultAddressType.getName()
                                    + "' for row " + rowCount + ", but will proceed.\n");
                        } else if (usingDefaultStatus) {
                            uploadReport
                                    .append("Info:  Using the default status '" + defaultAddressStatus.getName()
                                            + " for row " + rowCount + ", but will proceed.\n");
                        }

                        if (addressSource != null && !addressSource.isEmpty())
                            addressToAttachToPerson.setSource(addressSource);
                        if (!updateAddress) {
                            // TODO check this works in all cases
                            addressToAttachToPerson.setPerson(person);
                            person.getAddresses().add(addressToAttachToPerson);
                        }
                    }

                }

                // if no address info - ignore
                if (phoneNumberIndex > 0) {
                    boolean updatePhones = false;
                    boolean usingDefaultType = false;
                    boolean usingDefaultStatus = false;
                    String phoneNumberString = stringLineArray[phoneNumberIndex];

                    if (phoneNumberString == null || StringUtils.isBlank(phoneNumberString)) {
                        // then lets just jump out as there is no phone to validate. lay down to user that they must have data if they want an update
                    } else {
                        Phone phoneToAttachToPerson = null;
                        if (thisSubjectAlreadyExists) {
                            String typeString = null;
                            String statusString = null;

                            if (phoneTypeIndex > 0) {
                                typeString = stringLineArray[phoneTypeIndex];
                                if (typeString == null || typeString.isEmpty()) {
                                    typeString = defaultPhoneType.getName();
                                    usingDefaultType = true;
                                }
                            }
                            if (phoneStatusIndex > 0) {
                                statusString = stringLineArray[phoneStatusIndex];
                                if (statusString == null || statusString.isEmpty()) {
                                    statusString = defaultPhoneStatus.getName();
                                    usingDefaultStatus = true;
                                }
                            }
                            for (Phone phone : person.getPhones()) {
                                if (phone.getPhoneStatus().getName().equalsIgnoreCase(statusString)
                                        && phone.getPhoneType().getName().equalsIgnoreCase(typeString)) {
                                    phoneToAttachToPerson = phone;
                                    updatePhones = true;
                                }
                            }
                        }
                        if (phoneToAttachToPerson == null) {
                            phoneToAttachToPerson = new Phone();
                        }

                        PhoneType type = findPhoneTypeOrSetDefault(phoneTypesPossible, defaultPhoneType,
                                stringLineArray[phoneTypeIndex]);
                        PhoneStatus status = findPhoneStatusOrSetDefault(phoneStatiiPossible,
                                defaultPhoneStatus, stringLineArray[phoneTypeIndex]);
                        String phoneComments = stringLineArray[phoneCommentsIndex];

                        String areaCode = stringLineArray[areaCodeIndex];
                        String silentString = stringLineArray[phoneSilentIndex];
                        String phoneSource = stringLineArray[phoneSourceIndex];
                        String phoneDateReceivedString = stringLineArray[phoneDateReceivedIndex];
                        // log.warn("phone Date Reveived = " + phoneDateReceivedString + " for index = " + phoneDateReceivedIndex);

                        phoneToAttachToPerson.setPhoneType(type);
                        phoneToAttachToPerson.setPhoneStatus(status);
                        if (areaCode != null && !areaCode.isEmpty())
                            phoneToAttachToPerson.setAreaCode(areaCode);
                        if (phoneNumberString != null && !phoneNumberString.isEmpty())
                            phoneToAttachToPerson.setPhoneNumber(phoneNumberString);
                        if (phoneDateReceivedString != null && !phoneDateReceivedString.isEmpty()) {
                            // TODO dateconvert and set
                            Date dateReceived = new Date();
                            dateReceived = simpleDateFormat.parse(phoneDateReceivedString);
                            phoneToAttachToPerson.setDateReceived(dateReceived);
                        }

                        if (DataConversionAndManipulationHelper.isSomethingLikeABoolean(silentString)) {
                            if (DataConversionAndManipulationHelper.isSomethingLikeTrue(silentString)) {
                                phoneToAttachToPerson.setSilentMode(yes);
                            } else {
                                phoneToAttachToPerson.setSilentMode(no);
                            }
                        }
                        if (phoneComments != null && !phoneComments.isEmpty())
                            phoneToAttachToPerson.setComment(phoneComments);
                        if (phoneSource != null && !phoneSource.isEmpty())
                            phoneToAttachToPerson.setSource(phoneSource);

                        if (usingDefaultStatus && usingDefaultType) {
                            uploadReport
                                    .append("Info:  Using the default status '" + defaultAddressStatus.getName()
                                            + "' and the default type '" + defaultAddressType.getName()
                                            + " for row " + rowCount + ", but will proceed.\n");
                        } else if (usingDefaultType) {
                            uploadReport.append("Info:  Using the default type '" + defaultAddressType.getName()
                                    + "' for row " + rowCount + ", but will proceed.\n");
                        } else if (usingDefaultStatus) {
                            uploadReport
                                    .append("Info:  Using the default status '" + defaultAddressStatus.getName()
                                            + " for row " + rowCount + ", but will proceed.\n");
                        }

                        if (!updatePhones) {
                            // TODO check this works in all cases
                            phoneToAttachToPerson.setPerson(person);
                            person.getPhones().add(phoneToAttachToPerson);
                        }
                    }
                }

                /*
                 * 
                 * 
                 * //if no address info - ignore if(consentByIndex >0 && consentCompletedDateIndex >0 && consentDateIndex >0 &&
                 * consentCompletedDateIndex >0 && consentCompletedDateIndex >0 && consentCompletedDateIndex >0 && consentCompletedDateIndex >0 &&
                 * consentCompletedDateIndex >0 ){
                 * 
                 * 
                 * 
                 * 
                 * boolean updatePhones= false; boolean usingDefaultType = false; boolean usingDefaultStatus = false; String phoneNumberString =
                 * stringLineArray[phoneNumberIndex];
                 * 
                 * if(phoneNumberString == null || StringUtils.isBlank(phoneNumberString)){ //then lets just jump out as there is no phone to validate.
                 * lay down to user that they must have data if they want an update } else{ Phone phoneToAttachToPerson = null;
                 * if(thisSubjectAlreadyExists){ String typeString = null; String statusString = null;
                 * 
                 * if (phoneTypeIndex > 0){ typeString = stringLineArray[phoneTypeIndex]; if(typeString==null || typeString.isEmpty()){ typeString =
                 * defaultPhoneType.getName(); usingDefaultType = true; } } if (phoneStatusIndex > 0){ statusString =
                 * stringLineArray[phoneStatusIndex]; if(statusString==null || statusString.isEmpty()){ statusString = defaultPhoneStatus.getName();
                 * usingDefaultStatus = true; } } for(Phone phone : person.getPhones()){
                 * if(phone.getPhoneStatus().getName().equalsIgnoreCase(statusString) && phone.getPhoneType().getName().equalsIgnoreCase(typeString)){
                 * phoneToAttachToPerson = phone; updatePhones = true; } } } if(phoneToAttachToPerson==null){ phoneToAttachToPerson = new Phone(); }
                 * 
                 * PhoneType type = findPhoneTypeOrSetDefault(phoneTypesPossible, defaultPhoneType, stringLineArray[phoneTypeIndex]); PhoneStatus
                 * status = findPhoneStatusOrSetDefault(phoneStatiiPossible, defaultPhoneStatus, stringLineArray[phoneTypeIndex]); String phoneComments
                 * = stringLineArray[phoneCommentsIndex];
                 * 
                 * String areaCode = stringLineArray[areaCodeIndex]; String silentString = stringLineArray[phoneSilentIndex]; String phoneSource =
                 * stringLineArray[phoneSourceIndex]; String phoneDateReceivedString = stringLineArray[phoneDateReceivedIndex];
                 * //log.warn("phone Date Reveived = " + phoneDateReceivedString + " for index = " + phoneDateReceivedIndex);
                 * 
                 * phoneToAttachToPerson.setPhoneType(type); phoneToAttachToPerson.setPhoneStatus(status); if(areaCode!=null && !areaCode.isEmpty())
                 * phoneToAttachToPerson.setAreaCode(areaCode); if(phoneNumberString !=null && !phoneNumberString.isEmpty())
                 * phoneToAttachToPerson.setPhoneNumber(phoneNumberString); if(phoneDateReceivedString!=null && !phoneDateReceivedString.isEmpty()){ //
                 * TODO dateconvert and set Date dateReceived = new Date(); dateReceived = simpleDateFormat.parse(phoneDateReceivedString);
                 * phoneToAttachToPerson.setDateReceived(dateReceived); }
                 * 
                 * if(DataConversionAndManipulationHelper.isSomethingLikeABoolean(silentString)){
                 * if(DataConversionAndManipulationHelper.isSomethingLikeTrue(silentString)){ phoneToAttachToPerson.setSilentMode(yes); } else{
                 * phoneToAttachToPerson.setSilentMode(no); } } if(phoneComments!=null && !phoneComments.isEmpty())
                 * phoneToAttachToPerson.setComment(phoneComments); if(phoneSource!=null && !phoneSource.isEmpty())
                 * phoneToAttachToPerson.setSource(phoneSource);
                 * 
                 * 
                 * if(usingDefaultStatus && usingDefaultType){ uploadReport.append("Info:  Using the default status '" + defaultAddressStatus.getName()
                 * + "' and the default type '" + defaultAddressType.getName() + " for row " + rowCount + ", but will proceed.\n"); } else
                 * if(usingDefaultType){ uploadReport.append("Info:  Using the default type '" + defaultAddressType.getName() + "' for row " + rowCount
                 * + ", but will proceed.\n"); } else if(usingDefaultStatus){ uploadReport.append("Info:  Using the default status '" +
                 * defaultAddressStatus.getName() + " for row " + rowCount + ", but will proceed.\n"); }
                 * 
                 * if(!updatePhones){ //TODO check this works in all cases phoneToAttachToPerson.setPerson(person);
                 * person.getPhones().add(phoneToAttachToPerson); } } }
                 */

                subject.setPerson(person);

                if (subject.getId() == null || subject.getPerson().getId() == 0) {
                    insertSubjects.add(subject);
                    /*
                     * StringBuffer sb = new StringBuffer(); //does this report have to happen? ... and in reality it hasnt had success yet
                     * sb.append("\nCreated subject from original Subject UID: "); sb.append(subject.getSubjectUID());
                     * //sb.append(" has been created successfully and linked to the study: "); //sb.append(study.getName()); //sb.append("\n");
                     * uploadReport.append(sb);
                     */
                    insertCount++;
                } else {
                    // iStudyService.updateSubject(subjectVo);
                    updateSubjects.add(subject);
                    /*
                     * StringBuffer sb = new StringBuffer(); sb.append("\nUpdate subject with Subject UID: "); sb.append(subject.getSubjectUID());
                     * //sb.append(" has been updated successfully and linked to the study: "); //sb.append(study.getName()); //sb.append("\n");
                     * uploadReport.append(sb);
                     */
                    updateCount++;
                }

                subjectCount++;

            }
        }
    } catch (IOException ioe) {
        uploadReport.append("System Error:   Unexpected I/O exception whilst reading the subject data file\n");
        log.error("processMatrixSubjectFile IOException stacktrace:", ioe);
        throw new ArkSystemException("Unexpected I/O exception whilst reading the subject data file");
    } catch (Exception ex) {
        uploadReport.append("System Error:  Unexpected exception whilst reading the subject data file\n");
        log.error("processMatrixSubjectFile Exception stacktrace:", ex);
        throw new ArkSystemException("Unexpected exception occurred when trying to process subject data file");
    } finally {
        uploadReport.append("Total file size: ");
        uploadReport.append(decimalFormat.format(inLength / 1024.0 / 1024.0));
        uploadReport.append(" MB");
        uploadReport.append("\n");

        if (csvReader != null) {
            try {
                csvReader.close();
            } catch (Exception ex) {
                log.error("Cleanup operation failed: csvRdr.close()", ex);
            }
        }
        if (inputStreamReader != null) {
            try {
                inputStreamReader.close();
            } catch (Exception ex) {
                log.error("Cleanup operation failed: isr.close()", ex);
            }
        }
        // Restore the state of variables
        srcLength = -1;
    } /*
      * uploadReport.append("Processed "); uploadReport.append(subjectCount); uploadReport.append(" rows for "); uploadReport.append(subjectCount);
      * uploadReport.append(" subjects."); uploadReport.append("\n"); uploadReport.append(insertCount);
      * uploadReport.append(" fields were inserted."); uploadReport.append("\n"); uploadReport.append(updateCount);
      * uploadReport.append(" fields were updated."); uploadReport.append("\n");
      */

    uploadReport.append("Processed ");
    uploadReport.append(subjectCount);
    uploadReport.append(" rows.");
    uploadReport.append("\n");
    uploadReport.append("Inserted ");
    uploadReport.append(insertCount);
    uploadReport.append(" subjects.");
    uploadReport.append("\n");
    uploadReport.append("Updated ");
    uploadReport.append(updateCount);
    uploadReport.append(" subjects.");
    uploadReport.append("\n");

    // TODO better exceptionhandling
    iStudyService.processBatch(insertSubjects, study, updateSubjects);

    return uploadReport;
}

From source file:s3.com.qiniu.services.s3.AmazonS3Client.java

/**
 * Calculate the content length of a mark supported input stream.
 *
 * @param is input stream/*from w  w  w . j  ava2 s .c o m*/
 * @return length of the input stream
 */
private long calculateContentLength(InputStream is) {
    long len = 0;
    byte[] buf = new byte[8 * 1024];
    int read;
    is.mark(-1);
    try {
        while ((read = is.read(buf)) != -1) {
            len += read;
        }
        is.reset();
    } catch (IOException ioe) {
        throw new AmazonClientException("Could not calculate content length.", ioe);
    }
    return len;
}

From source file:org.lockss.servlet.ServeContent.java

/**
 * Check whether the input stream is to a publisher's html permission page.
 * Throws a CacheExcepiton.PermissionException if so; otherwise returns an
 * input stream positioned at the same position. The original input stream
 * will be closed if a new one is created.
 *
 * @param input an input stream//from w  ww  . ja va 2 s  .c  o m
 * @param headers a set of header properties
 * @return an input stream positioned at the same position as the original
 *  one if the original stream is not a login page
 * @throws CacheException.PermissionException if the connection is to a 
 *  login page or the LoginPageChecker for the plugin reported an error
 * @throws IOException if IO error while checking the stream
 */
private InputStream checkLoginPage(InputStream input, CIProperties headers)
        throws CacheException.PermissionException, IOException {

    LoginPageChecker checker = au.getLoginPageChecker();
    if (checker != null) {
        InputStream oldInput = input;

        // buffer html page stream to allow login page checker to read it
        int limit = CurrentConfig.getIntParam(BaseUrlFetcher.PARAM_LOGIN_CHECKER_MARK_LIMIT,
                BaseUrlFetcher.DEFAULT_LOGIN_CHECKER_MARK_LIMIT);
        DeferredTempFileOutputStream dos = new DeferredTempFileOutputStream(limit);
        try {
            StreamUtil.copy(input, dos);
            if (dos.isInMemory()) {
                input = new ByteArrayInputStream(dos.getData());
            } else {
                // create an input stream whose underlying file is deleted
                // when the input stream closes.
                File tempFile = dos.getFile();
                try {
                    input = new FileInputStream(tempFile);
                } catch (FileNotFoundException fnfe) {
                    FileUtils.deleteQuietly(tempFile);
                    throw fnfe;
                }
            }
        } finally {
            IOUtil.safeClose(oldInput);
            IOUtil.safeClose(dos);
        }

        // create reader for input stream
        String ctype = headers.getProperty("Content-Type");
        String charset = HeaderUtil.getCharsetOrDefaultFromContentType(ctype);

        try {
            Reader reader = new InputStreamReader(input, charset);

            if (checker.isLoginPage(headers, reader)) {
                IOUtil.safeClose(input);
                input = null;
                throw new CacheException.PermissionException("Found a login page");
            }
        } catch (PluginException e) {
            CacheException.PermissionException ex = new CacheException.PermissionException(
                    "Error checking login page");
            ex.initCause(e);
            IOUtil.safeClose(input);
            input = null;
            throw ex;
        } finally {
            if (input == null) {
                // delete the temp file immediately if not returning an input
                // stream open on it
                dos.deleteTempFile();
            } else if (dos.isInMemory()) {
                input.reset();
            } else {
                // special treatment because cannot reset FileInputStream,
                // so must close and re-open input stream from temp file
                IOUtil.safeClose(input);
                input = new DeleteFileOnCloseInputStream(dos.getFile());
            }
        }
    }

    return input;
}

From source file:org.sakaiproject.tool.assessment.facade.AssessmentGradingFacadeQueries.java

private byte[] getMediaStream(Long mediaId) {
    byte[] b = new byte[4000];
    Session session = null;//from  w w w  . j a  va  2  s.c o  m
    Connection conn = null;
    InputStream in = null;
    ResultSet rs = null;
    PreparedStatement statement = null;
    try {
        session = getSessionFactory().openSession();
        conn = session.connection();
        log.debug("****Connection=" + conn);
        String query = "select MEDIA from SAM_MEDIA_T where MEDIAID=?";
        statement = conn.prepareStatement(query);
        statement.setLong(1, mediaId.longValue());
        rs = statement.executeQuery();
        if (rs.next()) {
            java.lang.Object o = rs.getObject("MEDIA");
            if (o != null) {
                in = rs.getBinaryStream("MEDIA");
                in.mark(0);
                int ch;
                int len = 0;
                while ((ch = in.read()) != -1) {
                    len++;
                }
                b = new byte[len];
                in.reset();
                in.read(b, 0, len);
            }
        }
    } catch (Exception e) {
        log.warn(e.getMessage());
    }

    finally {
        if (session != null) {
            try {
                session.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }

    return b;
}

From source file:org.regenstrief.util.Util.java

/**
 * Retrieves a Reader for the given InputStream, using the correct character encoding
 * //  w  w  w  . java 2s  . c o  m
 * @param is the InputStream
 * @return the Reader
 * @throws Exception if an I/O problem occurs
 **/
public static final Reader getReader(InputStream is) throws Exception {
    is = getMarkableInputStream(is, 11);
    final String enc = getEncoding(is, false);
    is.reset();

    if (is instanceof LabeledInputStream) {
        return new LabeledReader((LabeledInputStream) is, enc);
    }
    return new BomReader(is, enc);
}

From source file:org.docx4j.openpackaging.io3.Load3.java

/**
 * Get a Part (except a relationships part), but not its relationships part
 * or related parts.  Useful if you need quick access to just this part.
 * This can be called directly from outside the library, in which case 
 * the Part will not be owned by a Package until the calling code makes it so.  
 * @see  To get a Part and all its related parts, and add all to a package, use
 * getPart.//from   w  ww .  j a v a  2s  .  c o  m
 * @param partByteArrays
 * @param ctm
 * @param resolvedPartUri
 * @param rel
 * @return
 * @throws Docx4JException including if result is null
 */
public Part getRawPart(ContentTypeManager ctm, String resolvedPartUri, Relationship rel)
        throws Docx4JException {

    Part part = null;

    InputStream is = null;
    try {
        try {
            log.debug("resolved uri: " + resolvedPartUri);

            // Get a subclass of Part appropriate for this content type   
            // This will throw UnrecognisedPartException in the absence of
            // specific knowledge. Hence it is important to get the is
            // first, as we do above.
            part = ctm.getPart("/" + resolvedPartUri, rel);

            log.debug("ctm returned " + part.getClass().getName());

            if (part instanceof org.docx4j.openpackaging.parts.ThemePart
                    || part instanceof org.docx4j.openpackaging.parts.DocPropsCorePart
                    || part instanceof org.docx4j.openpackaging.parts.DocPropsCustomPart
                    || part instanceof org.docx4j.openpackaging.parts.DocPropsExtendedPart
                    || part instanceof org.docx4j.openpackaging.parts.CustomXmlDataStoragePropertiesPart
                    || part instanceof org.docx4j.openpackaging.parts.digitalsignature.XmlSignaturePart
                    || part instanceof org.docx4j.openpackaging.parts.JaxbXmlPart) {

                // Nothing to do here

            } else if (part instanceof org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) {

                log.debug("Detected BinaryPart " + part.getClass().getName());

                // Note that this is done lazily, since the below lines are commented out

                //               is = partStore.loadPart( resolvedPartUri);
                //               ((BinaryPart)part).setBinaryData(is);

            } else if (part instanceof org.docx4j.openpackaging.parts.CustomXmlDataStoragePart) {
                // ContentTypeManager initially detects them as CustomXmlDataStoragePart;
                // the below changes as necessary 

                // Is it a part we know?
                is = partStore.loadPart(resolvedPartUri);
                try {

                    XMLInputFactory xif = XMLInputFactory.newInstance();
                    xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
                    xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); // a DTD is merely ignored, its presence doesn't cause an exception
                    XMLStreamReader xsr = xif.createXMLStreamReader(is);

                    Unmarshaller u = Context.jc.createUnmarshaller();
                    Object o = u.unmarshal(xsr);
                    log.debug(o.getClass().getName());

                    PartName name = part.getPartName();
                    if (o instanceof CoverPageProperties) {

                        part = new DocPropsCoverPagePart(name);
                        ((DocPropsCoverPagePart) part).setJaxbElement((CoverPageProperties) o);

                    } else if (o instanceof org.opendope.conditions.Conditions) {

                        part = new ConditionsPart(name);
                        ((ConditionsPart) part).setJaxbElement((org.opendope.conditions.Conditions) o);

                    } else if (o instanceof org.opendope.xpaths.Xpaths) {

                        part = new XPathsPart(name);
                        ((XPathsPart) part).setJaxbElement((org.opendope.xpaths.Xpaths) o);

                    } else if (o instanceof org.opendope.questions.Questionnaire) {

                        part = new QuestionsPart(name);
                        ((QuestionsPart) part).setJaxbElement((org.opendope.questions.Questionnaire) o);

                    } else if (o instanceof org.opendope.answers.Answers) {

                        part = new StandardisedAnswersPart(name);
                        ((StandardisedAnswersPart) part).setJaxbElement((org.opendope.answers.Answers) o);

                    } else if (o instanceof org.opendope.components.Components) {

                        part = new ComponentsPart(name);
                        ((ComponentsPart) part).setJaxbElement((org.opendope.components.Components) o);

                    } else if (o instanceof JAXBElement<?>
                            && XmlUtils.unwrap(o) instanceof org.docx4j.bibliography.CTSources) {
                        part = new BibliographyPart(name);
                        ((BibliographyPart) part)
                                .setJaxbElement((JAXBElement<org.docx4j.bibliography.CTSources>) o);

                    } else {

                        log.error("TODO: handle known CustomXmlPart part  " + o.getClass().getName());

                        CustomXmlDataStorage data = getCustomXmlDataStorageClass().factory();
                        is.reset();
                        data.setDocument(is); // Not necessarily JAXB, that's just our method name
                        ((org.docx4j.openpackaging.parts.CustomXmlDataStoragePart) part).setData(data);

                    }

                } catch (javax.xml.bind.UnmarshalException ue) {

                    log.warn("No JAXB model for this CustomXmlDataStorage part; " + ue.getMessage());

                    CustomXmlDataStorage data = getCustomXmlDataStorageClass().factory();
                    is.reset();
                    data.setDocument(is); // Not necessarily JAXB, that's just our method name
                    ((org.docx4j.openpackaging.parts.CustomXmlDataStoragePart) part).setData(data);
                }

            } else if (part instanceof org.docx4j.openpackaging.parts.XmlPart) {

                is = partStore.loadPart(resolvedPartUri);

                //               try {
                ((XmlPart) part).setDocument(is);

                // Experimental 22/6/2011; don't fall back to binary (which we used to) 

                //               } catch (Docx4JException d) {
                //                  // This isn't an XML part after all,
                //                  // even though ContentTypeManager detected it as such
                //                  // So get it as a binary part
                //                  part = getBinaryPart(partByteArrays, ctm, resolvedPartUri);
                //                  log.warn("Could not parse as XML, so using BinaryPart for " 
                //                        + resolvedPartUri);                  
                //                  ((BinaryPart)part).setBinaryData(is);
                //               }

            } else {
                // Shouldn't happen, since ContentTypeManagerImpl should
                // return an instance of one of the above, or throw an
                // Exception.

                log.error("No suitable part found for: " + resolvedPartUri);
                part = null;
            }

        } catch (PartUnrecognisedException e) {
            log.error("PartUnrecognisedException shouldn't happen anymore!", e);
            // Try to get it as a binary part
            part = getBinaryPart(ctm, resolvedPartUri);
            log.warn("Using BinaryPart for " + resolvedPartUri);

            //            is = partStore.loadPart( resolvedPartUri);
            //            ((BinaryPart)part).setBinaryData(is);
        }
    } catch (Exception ex) {
        // IOException, URISyntaxException
        ex.printStackTrace();
        throw new Docx4JException("Failed to getPart", ex);

    } finally {
        IOUtils.closeQuietly(is);
    }

    if (part == null) {
        throw new Docx4JException(
                "cannot find part " + resolvedPartUri + " from rel " + rel.getId() + "=" + rel.getTarget());
    }

    return part;
}