Example usage for java.util Date setTime

List of usage examples for java.util Date setTime

Introduction

In this page you can find the example usage for java.util Date setTime.

Prototype

public void setTime(long time) 

Source Link

Document

Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT.

Usage

From source file:fr.paris.lutece.plugins.directory.service.directorysearch.DirectorySearchService.java

/**
 * Return a list of record key return by the search
 * @param directory the directory//from  w w  w.  java2s  . c  o m
 * @param mapSearch a map which contains for each entry the list of
 *            recorField(value of field search) associate
 * @param dateCreation the creation date
 * @param dateCreationBegin the date begin to search for the creation date
 * @param dateCreationEnd the date end to search for the creation date
 * @param dateModification the modification date
 * @param dateModificationBegin the date begin to search for the
 *            modification date
 * @param dateModificationEnd the date end to search for the modification
 *            date
 * @param filter the filter
 * @param plugin the plugin
 * @return a list of record key return by the search
 */
public List<Integer> getSearchResults(Directory directory, HashMap<String, List<RecordField>> mapSearch,
        Date dateCreation, Date dateCreationBegin, Date dateCreationEnd, Date dateModification,
        Date dateModificationBegin, Date dateModificationEnd, RecordFieldFilter filter, Plugin plugin) {
    List<Integer> listRecordResult = new ArrayList<Integer>();

    IRecordService recordService = SpringContextService.getBean(RecordService.BEAN_SERVICE);
    listRecordResult = recordService.getListRecordId(filter, plugin);

    if (mapSearch != null) {
        List<Integer> listRecordResultTmp = null;
        List<RecordField> recordFieldSearch;
        HashMap<String, Object> mapSearchItemEntry;
        boolean bSearchRecordEmpty;
        boolean bSearchEmpty;

        try {
            _searcher = new IndexSearcher(DirectoryReader.open(_luceneDirectory));

            IDirectorySearchEngine engine = SpringContextService.getBean(BEAN_SEARCH_ENGINE);

            listRecordResultTmp = new ArrayList<Integer>();
            bSearchEmpty = true;

            for (Object entryMapSearch : mapSearch.entrySet()) {
                recordFieldSearch = ((Entry<String, List<RecordField>>) entryMapSearch).getValue();

                int nIdEntry = DirectoryUtils
                        .convertStringToInt(((Entry<String, List<RecordField>>) entryMapSearch).getKey());
                bSearchRecordEmpty = true;

                if (recordFieldSearch != null) {
                    mapSearchItemEntry = new HashMap<String, Object>();

                    boolean bIsArray = false;
                    boolean bFirstRecord = true;

                    for (RecordField recordField : recordFieldSearch) {
                        if (recordField.getEntry() instanceof EntryTypeArray) {
                            // for array, we do a search on content for each case
                            bIsArray = true;
                            mapSearchItemEntry = new HashMap<String, Object>();
                            recordField.getEntry().addSearchCriteria(mapSearchItemEntry, recordField);

                            if (mapSearchItemEntry.size() > 0) {
                                bSearchRecordEmpty = false;
                                bSearchEmpty = false;
                                mapSearchItemEntry.put(DirectorySearchItem.FIELD_ID_DIRECTORY,
                                        directory.getIdDirectory());
                                mapSearchItemEntry.put(DirectorySearchItem.FIELD_ID_DIRECTORY_ENTRY, nIdEntry);

                                List<Integer> ids = engine.getSearchResults(mapSearchItemEntry);

                                if (CollectionUtils.isEmpty(ids)) {
                                    listRecordResultTmp = new ArrayList<Integer>();

                                    break;
                                } else if (bFirstRecord) {
                                    listRecordResultTmp = ids;
                                    bFirstRecord = false;
                                } else {
                                    listRecordResultTmp = (List<Integer>) CollectionUtils
                                            .intersection(listRecordResultTmp, ids);
                                }
                            }
                        } else {
                            recordField.getEntry().addSearchCriteria(mapSearchItemEntry, recordField);
                        }
                    }

                    if (!bIsArray && (mapSearchItemEntry.size() > 0)) {
                        bSearchRecordEmpty = false;
                        bSearchEmpty = false;
                        mapSearchItemEntry.put(DirectorySearchItem.FIELD_ID_DIRECTORY,
                                directory.getIdDirectory());
                        mapSearchItemEntry.put(DirectorySearchItem.FIELD_ID_DIRECTORY_ENTRY, nIdEntry);
                        listRecordResultTmp.addAll(engine.getSearchResults(mapSearchItemEntry));
                    }

                    if (!bSearchRecordEmpty && !directory.isSearchOperatorOr()) {
                        // keeping order is important for display
                        listRecordResult = DirectoryUtils.retainAllIdsKeepingFirstOrder(listRecordResult,
                                listRecordResultTmp);
                        listRecordResultTmp = new ArrayList<Integer>();
                    }
                }
            }

            if (directory.isSearchOperatorOr() && !bSearchEmpty) {
                listRecordResult = DirectoryUtils.retainAllIdsKeepingFirstOrder(listRecordResult,
                        listRecordResultTmp);
            }

            //date creation of a record
            if (dateCreation != null) {
                listRecordResultTmp = new ArrayList<Integer>();
                mapSearchItemEntry = new HashMap<String, Object>();
                mapSearchItemEntry.put(DirectorySearchItem.FIELD_ID_DIRECTORY, directory.getIdDirectory());
                dateCreation.setTime(dateCreation.getTime() + CONSTANT_TIME_CORRECTION);

                mapSearchItemEntry.put(DirectorySearchItem.FIELD_DATE_CREATION, dateCreation);
                listRecordResultTmp.addAll(engine.getSearchResults(mapSearchItemEntry));

                // keeping order is important for display
                listRecordResult = DirectoryUtils.retainAllIdsKeepingFirstOrder(listRecordResult,
                        listRecordResultTmp);
            } else if ((dateCreationBegin != null) && (dateCreationEnd != null)) {
                dateCreationBegin.setTime(dateCreationBegin.getTime() + CONSTANT_TIME_CORRECTION);
                dateCreationEnd.setTime(dateCreationEnd.getTime() + CONSTANT_TIME_CORRECTION);

                listRecordResultTmp = new ArrayList<Integer>();
                mapSearchItemEntry = new HashMap<String, Object>();
                mapSearchItemEntry.put(DirectorySearchItem.FIELD_ID_DIRECTORY, directory.getIdDirectory());
                mapSearchItemEntry.put(DirectorySearchItem.FIELD_DATE_CREATION_BEGIN, dateCreationBegin);
                mapSearchItemEntry.put(DirectorySearchItem.FIELD_DATE_CREATION_END, dateCreationEnd);
                listRecordResultTmp.addAll(engine.getSearchResults(mapSearchItemEntry));

                // keeping order is important for display
                listRecordResult = DirectoryUtils.retainAllIdsKeepingFirstOrder(listRecordResult,
                        listRecordResultTmp);
            }

            //date modification of a record
            if (dateModification != null) {
                listRecordResultTmp = new ArrayList<Integer>();
                mapSearchItemEntry = new HashMap<String, Object>();
                mapSearchItemEntry.put(DirectorySearchItem.FIELD_ID_DIRECTORY, directory.getIdDirectory());
                dateModification.setTime(dateModification.getTime() + CONSTANT_TIME_CORRECTION);

                mapSearchItemEntry.put(DirectorySearchItem.FIELD_DATE_MODIFICATION, dateModification);
                listRecordResultTmp.addAll(engine.getSearchResults(mapSearchItemEntry));

                // keeping order is important for display
                listRecordResult = DirectoryUtils.retainAllIdsKeepingFirstOrder(listRecordResult,
                        listRecordResultTmp);
            } else if ((dateModificationBegin != null) && (dateModificationEnd != null)) {
                dateModificationBegin.setTime(dateModificationBegin.getTime() + CONSTANT_TIME_CORRECTION);
                dateModificationEnd.setTime(dateModificationEnd.getTime() + CONSTANT_TIME_CORRECTION);

                listRecordResultTmp = new ArrayList<Integer>();
                mapSearchItemEntry = new HashMap<String, Object>();
                mapSearchItemEntry.put(DirectorySearchItem.FIELD_ID_DIRECTORY, directory.getIdDirectory());
                mapSearchItemEntry.put(DirectorySearchItem.FIELD_DATE_MODIFICATION_BEGIN,
                        dateModificationBegin);
                mapSearchItemEntry.put(DirectorySearchItem.FIELD_DATE_MODIFICATION_END, dateModificationEnd);
                listRecordResultTmp.addAll(engine.getSearchResults(mapSearchItemEntry));

                // keeping order is important for display
                listRecordResult = DirectoryUtils.retainAllIdsKeepingFirstOrder(listRecordResult,
                        listRecordResultTmp);
            }
        } catch (Exception e) {
            AppLogService.error(e.getMessage(), e);
            // If an error occurred clean result list
            listRecordResult = new ArrayList<Integer>();
        }
    }

    return listRecordResult;
}

From source file:oscar.oscarDemographic.pageUtil.ImportDemographicDataAction4.java

String[] importXML(String xmlFile, ArrayList<String> warnings, HttpServletRequest request, int timeShiftInDays,
        Provider student, Program admitTo, int courseId) throws SQLException, Exception {
    ArrayList<String> err_demo = new ArrayList<String>(); //errors: duplicate demographics
    ArrayList<String> err_data = new ArrayList<String>(); //errors: discrete data
    ArrayList<String> err_summ = new ArrayList<String>(); //errors: summary
    ArrayList<String> err_othe = new ArrayList<String>(); //errors: other categories
    ArrayList<String> err_note = new ArrayList<String>(); //non-errors: notes

    String docDir = oscarProperties.getProperty("DOCUMENT_DIR");
    docDir = Util.fixDirName(docDir);
    if (!Util.checkDir(docDir)) {
        logger.debug("Error! Cannot write to DOCUMENT_DIR - Check oscar.properties or dir permissions.");
    }/*  w w w  .j ava2  s .  com*/

    File xmlF = new File(xmlFile);
    OmdCdsDocument.OmdCds omdCds = null;
    try {
        omdCds = OmdCdsDocument.Factory.parse(xmlF).getOmdCds();
    } catch (IOException ex) {
        logger.error("Error", ex);
    } catch (XmlException ex) {
        logger.error("Error", ex);
    }
    PatientRecord patientRec = omdCds.getPatientRecord();

    //DEMOGRAPHICS
    Demographics demo = patientRec.getDemographics();
    cdsDt.PersonNameStandard.LegalName legalName = demo.getNames().getLegalName();
    String lastName = "", firstName = "";
    String lastNameQualifier = null, firstNameQualifier = null;
    if (legalName != null) {
        if (legalName.getLastName() != null) {
            lastName = StringUtils.noNull(legalName.getLastName().getPart());
            if (legalName.getLastName().getPartQualifier() != null) {
                lastNameQualifier = legalName.getLastName().getPartQualifier().toString();
            }
        }
        if (legalName.getFirstName() != null) {
            firstName = StringUtils.noNull(legalName.getFirstName().getPart());
            if (legalName.getFirstName().getPartQualifier() != null) {
                firstNameQualifier = legalName.getFirstName().getPartQualifier().toString();
            }
        }
        patientName = lastName + "," + firstName;
    } else {
        err_data.add("Error! No Legal Name");
    }

    //other names
    String legalOtherNameTxt = null, otherNameTxt = null;

    LegalName.OtherName[] legalOtherNames = legalName.getOtherNameArray();
    for (LegalName.OtherName otherName : legalOtherNames) {
        if (legalOtherNameTxt == null)
            legalOtherNameTxt = otherName.getPart();
        else
            legalOtherNameTxt += ", " + otherName.getPart();
    }

    OtherNames[] otherNames = demo.getNames().getOtherNamesArray();
    for (OtherNames otherName : otherNames) {
        OtherNames.OtherName[] otherNames2 = otherName.getOtherNameArray();
        for (OtherNames.OtherName otherName2 : otherNames2) {
            if (otherNameTxt == null)
                otherNameTxt = otherName2.getPart();
            else
                otherNameTxt += ", " + otherName2.getPart();
        }
        if (otherName.getNamePurpose() != null) {
            otherNameTxt = Util.addLine(mapNamePurpose(otherName.getNamePurpose()) + ": ", otherNameTxt);
        }
    }
    otherNameTxt = Util.addLine(legalOtherNameTxt, otherNameTxt);

    String title = demo.getNames().getNamePrefix() != null ? demo.getNames().getNamePrefix().toString() : "";
    String suffix = demo.getNames().getLastNameSuffix() != null ? demo.getNames().getLastNameSuffix().toString()
            : "";
    String sex = demo.getGender() != null ? demo.getGender().toString() : "";
    if (StringUtils.empty(sex)) {
        err_data.add("Error! No Gender");
    }
    String birthDate = getCalDate(demo.getDateOfBirth(), timeShiftInDays);
    if (StringUtils.empty(birthDate)) {
        birthDate = null;
        err_data.add("Error! No Date Of Birth");
    }
    String versionCode = "", hin = "", hc_type = "", hc_renew_date = "";
    cdsDt.HealthCard healthCard = demo.getHealthCard();
    if (healthCard != null) {
        hin = StringUtils.noNull(healthCard.getNumber());
        if (hin.equals("")) {
            err_data.add("Error! No health card number");
        }
        hc_type = getProvinceCode(healthCard.getProvinceCode());
        if (hc_type.equals("")) {
            err_data.add("Error! No Province Code for health card");
        }
        versionCode = StringUtils.noNull(healthCard.getVersion());
        hc_renew_date = getCalDate(healthCard.getExpirydate());
    }

    //Check duplicate
    DemographicData dd = new DemographicData();
    ArrayList<Demographic> demodup = null;
    if (StringUtils.filled(hin))
        demodup = dd.getDemographicWithHIN(hin);
    else
        demodup = dd.getDemographicWithLastFirstDOB(lastName, firstName, birthDate);
    if (demodup.size() > 0) {
        err_data.clear();
        err_demo.add("Error! Patient " + patientName + " already exist! Not imported.");
        return packMsgs(err_demo, err_data, err_summ, err_othe, err_note, warnings);
    }

    String patient_status = null;
    Demographics.PersonStatusCode personStatusCode = demo.getPersonStatusCode();
    if (personStatusCode != null) { //somehow personStatusCode is always null ??
        if (personStatusCode.getPersonStatusAsEnum() != null) {
            if (personStatusCode.getPersonStatusAsEnum().equals(cdsDt.PersonStatus.A))
                patient_status = "AC";
            if (personStatusCode.getPersonStatusAsEnum().equals(cdsDt.PersonStatus.I))
                patient_status = "IN";
            if (personStatusCode.getPersonStatusAsEnum().equals(cdsDt.PersonStatus.D))
                patient_status = "DE";
        } else if (personStatusCode.getPersonStatusAsPlainText() != null) {
            patient_status = personStatusCode.getPersonStatusAsPlainText();
        } else {
            err_data.add("Error! No Person Status Code");
        }
    } else {
        err_data.add("Error! No Person Status Code");
    }

    Enrolment[] enrolments = demo.getEnrolmentArray();
    int enrolTotal = enrolments.length;
    String[] roster_status = new String[enrolTotal], roster_date = new String[enrolTotal],
            term_date = new String[enrolTotal], term_reason = new String[enrolTotal];

    String rosterInfo = null;
    Calendar enrollDate = null, currentEnrollDate = null;

    for (int i = 0; i < enrolTotal; i++) {
        roster_status[i] = enrolments[i].getEnrollmentStatus() != null
                ? enrolments[i].getEnrollmentStatus().toString()
                : "";
        if (roster_status[i].equals("1"))
            roster_status[i] = "RO";
        else if (roster_status[i].equals("0"))
            roster_status[i] = "NR";
        roster_date[i] = getCalDate(enrolments[i].getEnrollmentDate(), timeShiftInDays);
        term_date[i] = getCalDate(enrolments[i].getEnrollmentTerminationDate(), timeShiftInDays);
        if (enrolments[i].getTerminationReason() != null)
            term_reason[i] = enrolments[i].getTerminationReason().toString();

        //Sort enrolments by date
        if (enrolments[i].getEnrollmentDate() != null)
            currentEnrollDate = enrolments[i].getEnrollmentDate();
        else if (enrolments[i].getEnrollmentTerminationDate() != null)
            currentEnrollDate = enrolments[i].getEnrollmentTerminationDate();
        else
            currentEnrollDate = null;

        for (int j = i - 1; j >= 0; j--) {
            if (enrolments[j].getEnrollmentDate() != null)
                enrollDate = enrolments[j].getEnrollmentDate();
            else if (enrolments[j].getEnrollmentTerminationDate() != null)
                enrollDate = enrolments[j].getEnrollmentTerminationDate();
            else
                break;

            if (currentEnrollDate == null || currentEnrollDate.before(enrollDate)) {
                rosterInfo = roster_status[j];
                roster_status[j] = roster_status[i];
                roster_status[i] = rosterInfo;
                rosterInfo = roster_date[j];
                roster_date[j] = roster_date[i];
                roster_date[i] = rosterInfo;
                rosterInfo = term_date[j];
                term_date[j] = term_date[i];
                term_date[i] = rosterInfo;
                rosterInfo = term_reason[j];
                term_reason[j] = term_reason[i];
                term_reason[i] = rosterInfo;
            }
        }
    }

    String rosterStatus = null, rosterDate = null, termDate = null, termReason = null;
    if (enrolTotal > 0) {
        rosterStatus = roster_status[enrolTotal - 1];
        rosterDate = roster_date[enrolTotal - 1];
        termDate = term_date[enrolTotal - 1];
        termReason = term_reason[enrolTotal - 1];
    }

    String sin = StringUtils.noNull(demo.getSIN());

    String chart_no = StringUtils.noNull(demo.getChartNumber());
    String official_lang = null;
    if (demo.getPreferredOfficialLanguage() != null) {
        official_lang = demo.getPreferredOfficialLanguage().toString();
        official_lang = official_lang.equals("ENG") ? "English" : official_lang;
        official_lang = official_lang.equals("FRE") ? "French" : official_lang;
    }

    String spoken_lang = null;
    if (demo.getPreferredSpokenLanguage() != null) {
        spoken_lang = Util.convertCodeToLanguage(demo.getPreferredSpokenLanguage());
        if (StringUtils.empty(spoken_lang))
            err_data.add("Error! Cannot map spoken language code " + demo.getPreferredSpokenLanguage());
    }

    String dNote = StringUtils.noNull(demo.getNoteAboutPatient());
    String uvID = demo.getUniqueVendorIdSequence();
    String psDate = getCalDate(demo.getPersonStatusDate(), timeShiftInDays);
    String extra = null;

    if (StringUtils.filled(lastNameQualifier)) {
        extra = Util.addLine(extra, "Lastname Qualifier: ", lastNameQualifier);
    }

    if (StringUtils.filled(firstNameQualifier)) {
        extra = Util.addLine(extra, "Firstname Qualifier: ", firstNameQualifier);
    }

    if (StringUtils.filled(otherNameTxt)) {
        extra = Util.addLine(extra, "Other name: ", otherNameTxt);
    }

    if (StringUtils.filled(suffix)) {
        extra = Util.addLine(extra, "Lastname suffix: ", suffix);
    }

    if (StringUtils.filled(uvID)) {
        extra = Util.addLine(extra, "Unique Vendor ID: ", uvID);
    } else {
        err_data.add("Error! No Unique Vendor ID Sequence");
    }

    String address = "", city = "", province = "", postalCode = "";
    if (demo.getAddressArray().length > 0) {
        cdsDt.Address addr = demo.getAddressArray(0); //only get 1st address, other ignored
        if (addr != null) {
            if (StringUtils.filled(addr.getFormatted())) {
                address = addr.getFormatted();
            } else {
                cdsDt.AddressStructured addrStr = addr.getStructured();
                if (addrStr != null) {
                    address = StringUtils.noNull(addrStr.getLine1()) + StringUtils.noNull(addrStr.getLine2())
                            + StringUtils.noNull(addrStr.getLine3());
                    city = StringUtils.noNull(addrStr.getCity());
                    province = getCountrySubDivCode(addrStr.getCountrySubdivisionCode());
                    cdsDt.PostalZipCode postalZip = addrStr.getPostalZipCode();
                    if (postalZip != null)
                        postalCode = StringUtils.noNull(postalZip.getPostalCode());
                }
            }
        }
    }
    cdsDt.PhoneNumber[] pn = demo.getPhoneNumberArray();
    String workPhone = "", workExt = "", homePhone = "", homeExt = "", cellPhone = "", ext = "",
            patientPhone = "";
    for (int i = 0; i < pn.length; i++) {
        String phone = pn[i].getPhoneNumber();
        if (StringUtils.empty(phone)) {
            if (StringUtils.filled(pn[i].getNumber())) {
                String areaCode = StringUtils.filled(pn[i].getAreaCode()) ? "(" + pn[i].getAreaCode() + ")"
                        : "";
                phone = areaCode + pn[i].getNumber();
            }
        }
        if (StringUtils.filled(phone)) {
            if (StringUtils.filled(pn[i].getExtension()))
                ext = pn[i].getExtension();
            else if (StringUtils.filled(pn[i].getExchange()))
                ext = pn[i].getExchange();

            if (pn[i].getPhoneNumberType() == cdsDt.PhoneNumberType.W) {
                workPhone = phone;
                workExt = ext;
            } else if (pn[i].getPhoneNumberType() == cdsDt.PhoneNumberType.R) {
                homePhone = phone;
                homeExt = ext;
            } else if (pn[i].getPhoneNumberType() == cdsDt.PhoneNumberType.C) {
                cellPhone = phone;
            }
        }
    }
    if (StringUtils.filled(homePhone))
        patientPhone = homePhone + " " + homeExt;
    else if (StringUtils.filled(workPhone))
        patientPhone = workPhone + " " + workExt;
    else if (StringUtils.filled(cellPhone))
        patientPhone = cellPhone;
    String email = StringUtils.noNull(demo.getEmail());

    String primaryPhysician = "";
    if (student == null) {
        Demographics.PrimaryPhysician demoPrimaryPhysician = demo.getPrimaryPhysician();
        if (demoPrimaryPhysician != null) {
            HashMap<String, String> personName = getPersonName(demoPrimaryPhysician.getName());
            String personOHIP = demoPrimaryPhysician.getOHIPPhysicianId();
            if (StringUtils.empty(personName.get("firstname")))
                err_data.add("Error! No Primary Physician first name");
            if (StringUtils.empty(personName.get("lastname")))
                err_data.add("Error! No Primary Physician last name");
            if (StringUtils.empty(personOHIP))
                err_data.add("Error! No Primary Physician OHIP billing number");
            String personCPSO = demoPrimaryPhysician.getPrimaryPhysicianCPSO();
            primaryPhysician = writeProviderData(personName.get("firstname"), personName.get("lastname"),
                    personOHIP, personCPSO);
        }
        if (StringUtils.empty(primaryPhysician)) {
            primaryPhysician = defaultProviderNo();
            err_data.add("Error! No Primary Physician; patient assigned to \"doctor oscardoc\"");
        }
    } else {
        primaryPhysician = student.getProviderNo();
    }

    String year_of_birth = null;
    String month_of_birth = null;
    String date_of_birth = null;
    if (birthDate != null) {
        Date bDate = UtilDateUtilities.StringToDate(birthDate, "yyyy-MM-dd");
        year_of_birth = UtilDateUtilities.DateToString(bDate, "yyyy");
        month_of_birth = UtilDateUtilities.DateToString(bDate, "MM");
        date_of_birth = UtilDateUtilities.DateToString(bDate, "dd");
    }

    DemographicAddResult demoRes = null;

    //Check if Contact-only demographic exists
    org.oscarehr.common.model.Demographic demographic = null;

    if (courseId == 0) {
        demographicNo = dd.getDemoNoByNamePhoneEmail(firstName, lastName, homePhone, workPhone, email);
        demographic = dd.getDemographic(demographicNo);
    }

    if (demographic != null
            && StringUtils.nullSafeEqualsIgnoreCase(demographic.getPatientStatus(), "Contact-only")) {
        //found contact-only demo, replace!
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        demographic.setTitle(title);
        demographic.setAddress(address);
        demographic.setCity(city);
        demographic.setProvince(province);
        demographic.setPostal(postalCode);
        demographic.setYearOfBirth(year_of_birth);
        demographic.setMonthOfBirth(month_of_birth);
        demographic.setDateOfBirth(date_of_birth);
        demographic.setHin(hin);
        demographic.setVer(versionCode);
        demographic.setRosterStatus(rosterStatus);
        demographic.setRosterDate(StringUtils.isNullOrEmpty(rosterDate) ? null : formatter.parse(rosterDate));
        demographic.setRosterTerminationDate(
                StringUtils.isNullOrEmpty(termDate) ? null : formatter.parse(termDate));
        demographic.setRosterTerminationReason(termReason);
        demographic.setPatientStatus(patient_status);
        demographic.setPatientStatusDate(StringUtils.isNullOrEmpty(psDate) ? null : formatter.parse(psDate));
        demographic.setChartNo(chart_no);
        demographic.setOfficialLanguage(official_lang);
        demographic.setSpokenLanguage(spoken_lang);
        demographic.setFamilyDoctor(primaryPhysician);
        demographic.setSex(sex);
        demographic.setHcType(hc_type);
        demographic.setHcRenewDate(
                StringUtils.isNullOrEmpty(hc_renew_date) ? null : formatter.parse(hc_renew_date));
        demographic.setSin(sin);
        dd.setDemographic(demographic);
        err_note.add("Replaced Contact-only patient " + patientName + " (Demo no=" + demographicNo + ")");

    } else { //add patient!
        demoRes = dd.addDemographic(title, lastName, firstName, address, city, province, postalCode, homePhone,
                workPhone, year_of_birth, month_of_birth, date_of_birth, hin, versionCode, rosterStatus,
                rosterDate, termDate, termReason, patient_status, psDate, ""/*date_joined*/, chart_no,
                official_lang, spoken_lang, primaryPhysician, sex, ""/*end_date*/, ""/*eff_date*/,
                ""/*pcn_indicator*/, hc_type, hc_renew_date, ""/*family_doctor*/, email, ""/*pin*/, ""/*alias*/,
                ""/*previousAddress*/, ""/*children*/, ""/*sourceOfIncome*/, ""/*citizenship*/, sin);
        demographicNo = demoRes.getId();
    }

    if (StringUtils.filled(demographicNo)) {
        //TODO: Course - Admit to student program

        entries.put(PATIENTID + importNo, Integer.valueOf(demographicNo));

        if (admitTo == null) {
            insertIntoAdmission(demographicNo);
        } else {
            admissionManager.processAdmission(Integer.valueOf(demographicNo), student.getProviderNo(), admitTo,
                    "", "batch import");
        }

        //Put enrolment history into demographicArchive
        demographic = dd.getDemographic(demographicNo);
        for (int i = 0; i < roster_status.length - 1; i++) {
            DemographicArchive demographicArchive = archiveDemographic(demographic);
            demographicArchive.setRosterStatus(roster_status[i]);
            demographicArchive.setRosterDate(UtilDateUtilities.StringToDate(roster_date[i]));
            demographicArchive.setRosterTerminationDate(UtilDateUtilities.StringToDate(term_date[i]));
            demographicArchive.setRosterTerminationReason(term_reason[i]);
            demoArchiveDao.persist(demographicArchive);
        }

        //Patient notes
        if (StringUtils.filled(dNote))
            dd.addDemographiccust(demographicNo, dNote);

        //to dumpsite: Extra demographic data
        /*
        if (StringUtils.filled(extra)) {
           extra = Util.addLine("imported.cms4.2011.06", extra);
           CaseManagementNote dmNote = prepareCMNote("2",null);
           dmNote.setNote(extra);
           saveLinkNote(dmNote, CaseManagementNoteLink.DEMOGRAPHIC, Long.valueOf(demographicNo));
        }*/

        if (!workExt.equals(""))
            demographicExtDao.addKey(primaryPhysician, demographicNo, "wPhoneExt", workExt);
        if (!homeExt.equals(""))
            demographicExtDao.addKey(primaryPhysician, demographicNo, "hPhoneExt", homeExt);
        if (!cellPhone.equals(""))
            demographicExtDao.addKey(primaryPhysician, demographicNo, "demo_cell", cellPhone);
        if (courseId > 0)
            demographicExtDao.addKey(primaryPhysician, demographicNo, "course", String.valueOf(courseId));

        //Demographic Contacts
        DemographicContactDao contactDao = (DemographicContactDao) SpringUtils.getBean("demographicContactDao");

        Demographics.Contact[] contt = demo.getContactArray();
        for (int i = 0; i < contt.length; i++) {
            HashMap<String, String> contactName = getPersonName(contt[i].getName());
            String cFirstName = StringUtils.noNull(contactName.get("firstname"));
            String cLastName = StringUtils.noNull(contactName.get("lastname"));
            String cEmail = StringUtils.noNull(contt[i].getEmailAddress());

            pn = contt[i].getPhoneNumberArray();
            workPhone = "";
            workExt = "";
            homePhone = "";
            homeExt = "";
            cellPhone = "";
            ext = "";
            for (int j = 0; j < pn.length; j++) {
                String phone = pn[j].getPhoneNumber();
                if (phone == null) {
                    if (pn[j].getNumber() != null) {
                        if (pn[j].getAreaCode() != null)
                            phone = pn[j].getAreaCode() + "-" + pn[j].getNumber();
                        else
                            phone = pn[j].getNumber();
                    }
                }
                if (phone != null) {
                    if (pn[j].getExtension() != null)
                        ext = pn[j].getExtension();
                    else if (pn[j].getExchange() != null)
                        ext = pn[j].getExchange();

                    if (pn[j].getPhoneNumberType() == cdsDt.PhoneNumberType.W) {
                        workPhone = phone;
                        workExt = ext;
                    } else if (pn[j].getPhoneNumberType() == cdsDt.PhoneNumberType.R) {
                        homePhone = phone;
                        homeExt = ext;
                    } else if (pn[j].getPhoneNumberType() == cdsDt.PhoneNumberType.C) {
                        cellPhone = phone;
                    }
                }
            }

            String contactNote = StringUtils.noNull(contt[i].getNote());
            String cDemoNo = dd.getDemoNoByNamePhoneEmail(cFirstName, cLastName, homePhone, workPhone, cEmail);
            String cPatient = cLastName + "," + cFirstName;
            if (StringUtils.empty(cDemoNo)) { //add new demographic as contact
                psDate = UtilDateUtilities.DateToString(new Date(), "yyyy-MM-dd");
                demoRes = dd.addDemographic(""/*title*/, cLastName, cFirstName, ""/*address*/, ""/*city*/,
                        ""/*province*/, ""/*postal*/, homePhone, workPhone, ""/*year_of_birth*/, ""/*month_*/,
                        ""/*date_*/, ""/*hin*/, ""/*ver*/, ""/*roster_status*/, "", "", "", "Contact-only",
                        psDate, ""/*date_joined*/, ""/*chart_no*/, ""/*official_lang*/, ""/*spoken_lang*/,
                        ""/*provider_no*/, "F", ""/*end_date*/, ""/*eff_date*/, ""/*pcn_indicator*/,
                        ""/*hc_type*/, ""/*hc_renew_date*/, ""/*family_doctor*/, cEmail, "", "", "", "", "", "",
                        "");
                cDemoNo = demoRes.getId();
                err_note.add("Contact-only patient " + cPatient + " (Demo no=" + cDemoNo + ") created");

                if (!workExt.equals(""))
                    demographicExtDao.addKey("", cDemoNo, "wPhoneExt", workExt);
                if (!homeExt.equals(""))
                    demographicExtDao.addKey("", cDemoNo, "hPhoneExt", homeExt);
                if (!cellPhone.equals(""))
                    demographicExtDao.addKey("", cDemoNo, "demo_cell", cellPhone);
            }
            insertIntoAdmission(cDemoNo);

            cdsDt.PurposeEnumOrPlainText[] contactPurposes = contt[i].getContactPurposeArray();
            String sdm = "", emc = "", cPurpose = null;
            String[] rel = new String[contactPurposes.length];

            for (int j = 0; j < contactPurposes.length; j++) {
                cPurpose = contactPurposes[j].getPurposeAsPlainText();
                if (cPurpose == null)
                    cPurpose = contactPurposes[j].getPurposeAsEnum().toString();
                if (cPurpose != null)
                    cPurpose = cPurpose.trim();
                else
                    continue;

                if (cPurpose.equals("EC") || cPurpose.equalsIgnoreCase("emergency contact"))
                    emc = "true";
                else if (cPurpose.equals("SDM") || cPurpose.equalsIgnoreCase("substitute decision maker"))
                    sdm = "true";
                else if (cPurpose.equals("NK"))
                    rel[j] = "Next of Kin";
                else if (cPurpose.equals("AS"))
                    rel[j] = "Administrative Staff";
                else if (cPurpose.equals("CG"))
                    rel[j] = "Care Giver";
                else if (cPurpose.equals("PA"))
                    rel[j] = "Power of Attorney";
                else if (cPurpose.equals("IN"))
                    rel[j] = "Insurance";
                else if (cPurpose.equals("GT"))
                    rel[j] = "Guarantor";
                else {
                    rel[j] = cPurpose;
                }
            }

            if (StringUtils.filled(cDemoNo)) {
                if (oscarProperties.isPropertyActive("NEW_CONTACTS_UI")) {
                    for (int j = 0; j < rel.length; j++) {
                        if (rel[j] == null)
                            continue;

                        DemographicContact demoContact = new DemographicContact();
                        demoContact.setCreated(new Date());
                        demoContact.setUpdateDate(new Date());
                        demoContact.setDemographicNo(Integer.valueOf(demographicNo));
                        demoContact.setContactId(cDemoNo);
                        demoContact.setType(1); //should be "type" - display problem
                        demoContact.setCategory("personal");
                        demoContact.setRole(rel[j]);
                        demoContact.setEc(emc);
                        demoContact.setSdm(sdm);
                        demoContact.setNote(contactNote);
                        contactDao.persist(demoContact);

                        //clear emc, sdm, contactNote after 1st save
                        emc = "";
                        sdm = "";
                        contactNote = "";
                    }
                } else {
                    Facility facility = (Facility) request.getSession()
                            .getAttribute(SessionConstants.CURRENT_FACILITY);
                    Integer facilityId = null;
                    if (facility != null)
                        facilityId = facility.getId();

                    for (int j = 0; j < rel.length; j++) {
                        if (rel[j] == null)
                            continue;

                        DemographicRelationship demoRel = new DemographicRelationship();
                        demoRel.addDemographicRelationship(demographicNo, cDemoNo, rel[j], sdm.equals("true"),
                                emc.equals("true"), contactNote, admProviderNo, facilityId);

                        //clear emc, sdm, contactNote after 1st save
                        emc = "";
                        sdm = "";
                        contactNote = "";
                    }
                }
            }
        }

        Set<CaseManagementIssue> scmi = null; //Declare a set for CaseManagementIssues
        //PERSONAL HISTORY
        PersonalHistory[] pHist = patientRec.getPersonalHistoryArray();
        for (int i = 0; i < pHist.length; i++) {
            if (i == 0)
                scmi = getCMIssue("SocHistory");
            CaseManagementNote cmNote = prepareCMNote("1", null);
            cmNote.setIssues(scmi);

            //main field
            String socialHist = "Imported Personal History";
            //                String summary = pHist[i].getCategorySummaryLine();
            String residual = getResidual(pHist[i].getResidualInfo());
            if (StringUtils.empty(residual))
                continue;

            cmNote.setNote(socialHist);
            if (cmNote.getProviderNo() == null)
                cmNote.setProviderNo("999998");
            if (cmNote.getSigning_provider_no() == null)
                cmNote.setSigning_provider_no("999998");
            caseManagementManager.saveNoteSimple(cmNote);
            addOneEntry(PERSONALHISTORY);

            //to dumpsite
            /*
            residual = Util.addLine("imported.cms4.2011.06", residual);
            Long hostNoteId = cmNote.getId();
            cmNote = prepareCMNote("2",null);
            cmNote.setNote(residual);
            saveLinkNote(hostNoteId, cmNote);
            */
        }

        //FAMILY HISTORY
        FamilyHistory[] fHist = patientRec.getFamilyHistoryArray();
        for (int i = 0; i < fHist.length; i++) {
            if (i == 0)
                scmi = getCMIssue("FamHistory");
            CaseManagementNote cmNote = prepareCMNote("1", null);

            //diagnosis code
            if (fHist[i].getDiagnosisProcedureCode() == null) {
                cmNote.setIssues(scmi);
            } else {
                cmNote.setIssues(getCMIssue("FamHistory", fHist[i].getDiagnosisProcedureCode()));
                if (fHist[i].getDiagnosisProcedureCode().getStandardCode() == null) {
                    err_note.add(
                            "Family History diagnosis procedure code could not be retrieved. The code description is:"
                                    + fHist[i].getDiagnosisProcedureCode().getStandardCodeDescription());
                }
            }

            //main field
            String familyHist = fHist[i].getProblemDiagnosisProcedureDescription();
            if (StringUtils.empty(familyHist)) {
                familyHist = fHist[i].getCategorySummaryLine();
                if (StringUtils.empty(familyHist))
                    familyHist = "Imported Family History";

            }

            cmNote.setNote(familyHist);
            if (cmNote.getProviderNo() == null)
                cmNote.setProviderNo("999998");
            if (cmNote.getSigning_provider_no() == null)
                cmNote.setSigning_provider_no("999998");
            caseManagementManager.saveNoteSimple(cmNote);
            addOneEntry(FAMILYHISTORY);

            //annotation
            Long hostNoteId = cmNote.getId();
            cmNote = prepareCMNote("2", null);
            String note = StringUtils.noNull(fHist[i].getNotes());
            cmNote.setNote(note);
            saveLinkNote(hostNoteId, cmNote);

            //to dumpsite
            String dump = "imported.cms4.2011.06";
            /*
            String summary = fHist[i].getCategorySummaryLine();
            if (StringUtils.empty(summary)) {
                err_summ.add("No Summary for Family History ("+(i+1)+")");
            }
            dump = Util.addLine(dump, summary);
            */
            /*
            String diagCode = getCode(fHist[i].getDiagnosisProcedureCode(),"Diagnosis/Procedure");
            dump = Util.addLine(dump, diagCode);
            dump = Util.addLine(dump, getResidual(fHist[i].getResidualInfo()));
            cmNote = prepareCMNote("2",null);
            cmNote.setNote(dump);
            saveLinkNote(hostNoteId, cmNote);
            */
            //extra fields
            CaseManagementNoteExt cme = new CaseManagementNoteExt();
            cme.setNoteId(hostNoteId);
            if (fHist[i].getStartDate() != null) {
                cme.setKeyVal(CaseManagementNoteExt.STARTDATE);
                cme.setDateValue(dateFPtoDate(fHist[i].getStartDate(), timeShiftInDays));
                cme.setValue(dateFPGetPartial(fHist[i].getStartDate()));
                caseManagementManager.saveNoteExt(cme);
            }
            if (fHist[i].getAgeAtOnset() != null) {
                cme.setKeyVal(CaseManagementNoteExt.AGEATONSET);
                cme.setDateValue((Date) null);
                cme.setValue(fHist[i].getAgeAtOnset().toString());
                caseManagementManager.saveNoteExt(cme);
            }
            if (StringUtils.filled(fHist[i].getRelationship())) {
                cme.setKeyVal(CaseManagementNoteExt.RELATIONSHIP);
                cme.setDateValue((Date) null);
                cme.setValue(fHist[i].getRelationship());
                caseManagementManager.saveNoteExt(cme);
            }
            if (StringUtils.filled(fHist[i].getTreatment())) {
                cme.setKeyVal(CaseManagementNoteExt.TREATMENT);
                cme.setDateValue((Date) null);
                cme.setValue(fHist[i].getTreatment());
                caseManagementManager.saveNoteExt(cme);
            }
            if (fHist[i].getLifeStage() != null) {
                cme.setKeyVal(CaseManagementNoteExt.LIFESTAGE);
                cme.setDateValue((Date) null);
                cme.setValue(fHist[i].getLifeStage().toString());
                caseManagementManager.saveNoteExt(cme);
            }
        }

        //PAST HEALTH
        PastHealth[] pHealth = patientRec.getPastHealthArray();
        for (int i = 0; i < pHealth.length; i++) {
            if (i == 0)
                scmi = getCMIssue("MedHistory");
            CaseManagementNote cmNote = prepareCMNote("1", null);

            //diagnosis code
            if (pHealth[i].getDiagnosisProcedureCode() == null) {
                cmNote.setIssues(scmi);
            } else {
                cmNote.setIssues(getCMIssue("MedHistory", pHealth[i].getDiagnosisProcedureCode()));
            }

            //main field
            String medicalHist = pHealth[i].getPastHealthProblemDescriptionOrProcedures();
            if (StringUtils.empty(medicalHist))
                medicalHist = "Imported Medical History";
            cmNote.setNote(medicalHist);
            if (cmNote.getProviderNo() == null)
                cmNote.setProviderNo("999998");
            if (cmNote.getSigning_provider_no() == null)
                cmNote.setSigning_provider_no("999998");
            caseManagementManager.saveNoteSimple(cmNote);
            addOneEntry(FAMILYHISTORY);

            //annotation
            Long hostNoteId = cmNote.getId();
            cmNote = prepareCMNote("2", null);
            String note = pHealth[i].getNotes();
            cmNote.setNote(note);
            saveLinkNote(hostNoteId, cmNote);

            //to dumpsite
            String dump = "imported.cms4.2011.06";
            /*
            String summary = pHealth[i].getCategorySummaryLine();
            if (StringUtils.empty(summary)) {
            err_summ.add("No Summary for Past Health ("+(i+1)+")");
            }
            dump = Util.addLine(dump, summary);
            */
            /*
            String diagCode = isICD9(pHealth[i].getDiagnosisProcedureCode()) ? null : getCode(pHealth[i].getDiagnosisProcedureCode(),"Diagnosis/Procedure");
            dump = Util.addLine(dump, diagCode);
            dump = Util.addLine(dump, getResidual(pHealth[i].getResidualInfo()));
            cmNote = prepareCMNote("2",null);
            cmNote.setNote(dump);
            saveLinkNote(hostNoteId, cmNote);
            */
            //extra fields
            CaseManagementNoteExt cme = new CaseManagementNoteExt();
            cme.setNoteId(hostNoteId);
            if (pHealth[i].getOnsetOrEventDate() != null) {
                cme.setKeyVal(CaseManagementNoteExt.STARTDATE);
                cme.setDateValue(dateFPtoDate(pHealth[i].getOnsetOrEventDate(), timeShiftInDays));
                cme.setValue(dateFPGetPartial(pHealth[i].getOnsetOrEventDate()));
                caseManagementManager.saveNoteExt(cme);
            }
            if (pHealth[i].getProcedureDate() != null) {
                cme.setKeyVal(CaseManagementNoteExt.PROCEDUREDATE);
                cme.setDateValue(dateFPtoDate(pHealth[i].getProcedureDate(), timeShiftInDays));
                cme.setValue(dateFPGetPartial(pHealth[i].getProcedureDate()));
                caseManagementManager.saveNoteExt(cme);
            }
            if (pHealth[i].getResolvedDate() != null) {
                cme.setKeyVal(CaseManagementNoteExt.RESOLUTIONDATE);
                cme.setDateValue(dateFPtoDate(pHealth[i].getResolvedDate(), timeShiftInDays));
                cme.setValue(dateFPGetPartial(pHealth[i].getResolvedDate()));
                caseManagementManager.saveNoteExt(cme);
            }
            if (pHealth[i].getLifeStage() != null) {
                cme.setKeyVal(CaseManagementNoteExt.LIFESTAGE);
                cme.setDateValue((Date) null);
                cme.setValue(pHealth[i].getLifeStage().toString());
                caseManagementManager.saveNoteExt(cme);
            }
        }

        //PROBLEM LIST
        ProblemList[] probList = patientRec.getProblemListArray();
        for (int i = 0; i < probList.length; i++) {
            if (i == 0)
                scmi = getCMIssue("Concerns");
            CaseManagementNote cmNote = prepareCMNote("1", null);

            //diagnosis code
            if (probList[i].getDiagnosisCode() == null) {
                cmNote.setIssues(scmi);
            } else {
                cmNote.setIssues(getCMIssue("Concerns", probList[i].getDiagnosisCode()));
                if (probList[i].getDiagnosisCode().getStandardCode() == null) {
                    err_note.add(
                            "Problem List diagnosis procedure code could not be retrieved. The code description is:"
                                    + probList[i].getDiagnosisCode().getStandardCodeDescription());
                }
            }

            if (cmNote.getIssues().isEmpty())
                cmNote.setIssues(scmi);

            //main field
            String ongConcerns = probList[i].getProblemDiagnosisDescription();
            if (StringUtils.empty(ongConcerns)) {
                ongConcerns = probList[i].getCategorySummaryLine();
                if (StringUtils.empty(ongConcerns))
                    ongConcerns = "Imported Concern";
            }
            cmNote.setNote(ongConcerns);
            if (cmNote.getProviderNo() == null)
                cmNote.setProviderNo("999998");
            if (cmNote.getSigning_provider_no() == null)
                cmNote.setSigning_provider_no("999998");
            caseManagementManager.saveNoteSimple(cmNote);
            addOneEntry(PROBLEMLIST);

            //annotation
            Long hostNoteId = cmNote.getId();
            cmNote = prepareCMNote("2", null);
            String note = probList[i].getNotes();
            cmNote.setNote(note);
            saveLinkNote(hostNoteId, cmNote);

            //to dumpsite
            String dump = "imported.cms4.2011.06";
            /*
            String summary = probList[i].getCategorySummaryLine();
            if (StringUtils.empty(summary)) {
                    err_summ.add("No Summary for Problem List ("+(i+1)+")");
            }
            dump = Util.addLine(dump, summary);
            */
            /*
            String diagCode = isICD9(probList[i].getDiagnosisCode()) ? null : getCode(probList[i].getDiagnosisCode(),"Diagnosis");
            dump = Util.addLine(dump, diagCode);
            dump = Util.addLine(dump, getResidual(probList[i].getResidualInfo()));
            cmNote = prepareCMNote("2",null);
            cmNote.setNote(dump);
            saveLinkNote(hostNoteId, cmNote);
            */
            //extra fields
            CaseManagementNoteExt cme = new CaseManagementNoteExt();
            cme.setNoteId(hostNoteId);
            if (StringUtils.filled(probList[i].getProblemDescription())) {
                cme.setKeyVal(CaseManagementNoteExt.PROBLEMDESC);
                cme.setDateValue((Date) null);
                cme.setValue(probList[i].getProblemDescription());
                caseManagementManager.saveNoteExt(cme);
            }
            if (probList[i].getOnsetDate() != null) {
                cme.setKeyVal(CaseManagementNoteExt.STARTDATE);
                cme.setDateValue(dateFPtoDate(probList[i].getOnsetDate(), timeShiftInDays));
                cme.setValue(dateFPGetPartial(probList[i].getOnsetDate()));
                caseManagementManager.saveNoteExt(cme);
            } else {
                err_data.add("Error! No Onset Date for Problem List (" + (i + 1) + ")");
            }
            if (probList[i].getResolutionDate() != null) {
                cme.setKeyVal(CaseManagementNoteExt.RESOLUTIONDATE);
                cme.setDateValue(dateFPtoDate(probList[i].getResolutionDate(), timeShiftInDays));
                cme.setValue(dateFPGetPartial(probList[i].getResolutionDate()));
                caseManagementManager.saveNoteExt(cme);
            }
            if (StringUtils.filled(probList[i].getProblemStatus())) {
                cme.setKeyVal(CaseManagementNoteExt.PROBLEMSTATUS);
                cme.setDateValue((Date) null);
                cme.setValue(probList[i].getProblemStatus());
                caseManagementManager.saveNoteExt(cme);
            }
            if (probList[i].getLifeStage() != null) {
                cme.setKeyVal(CaseManagementNoteExt.LIFESTAGE);
                cme.setDateValue((Date) null);
                cme.setValue(probList[i].getLifeStage().toString());
                caseManagementManager.saveNoteExt(cme);
            }
        }

        //RISK FACTORS
        RiskFactors[] rFactors = patientRec.getRiskFactorsArray();
        for (int i = 0; i < rFactors.length; i++) {
            if (i == 0)
                scmi = getCMIssue("RiskFactors");
            CaseManagementNote cmNote = prepareCMNote("1", null);
            cmNote.setIssues(scmi);

            //main field
            String riskFactors = rFactors[i].getRiskFactor();
            if (StringUtils.empty(riskFactors))
                riskFactors = "Imported Risk Factor";
            cmNote.setNote(riskFactors);
            if (cmNote.getProviderNo() == null)
                cmNote.setProviderNo("999998");
            if (cmNote.getSigning_provider_no() == null)
                cmNote.setSigning_provider_no("999998");
            caseManagementManager.saveNoteSimple(cmNote);
            addOneEntry(RISKFACTOR);

            //annotation
            Long hostNoteId = cmNote.getId();
            cmNote = prepareCMNote("2", null);
            String note = rFactors[i].getNotes();
            cmNote.setNote(note);
            saveLinkNote(hostNoteId, cmNote);

            //to dumpsite
            String dump = "imported.cms4.2011.06";
            /*
            String summary = rFactors[i].getCategorySummaryLine();
            if (StringUtils.empty(summary)) {
                err_summ.add("No Summary for Risk Factors ("+(i+1)+")");
            }
            dump = Util.addLine(dump, summary);
            */
            /*
            dump = Util.addLine(dump, getResidual(rFactors[i].getResidualInfo()));
            cmNote = prepareCMNote("2",null);
            cmNote.setNote(dump);
            saveLinkNote(hostNoteId, cmNote);
            */
            //extra fields
            CaseManagementNoteExt cme = new CaseManagementNoteExt();
            cme.setNoteId(hostNoteId);
            if (rFactors[i].getStartDate() != null) {
                cme.setKeyVal(CaseManagementNoteExt.STARTDATE);
                cme.setDateValue(dateFPtoDate(rFactors[i].getStartDate(), timeShiftInDays));
                cme.setValue(dateFPGetPartial(rFactors[i].getStartDate()));
                caseManagementManager.saveNoteExt(cme);
            }
            if (rFactors[i].getEndDate() != null) {
                cme.setKeyVal(CaseManagementNoteExt.RESOLUTIONDATE);
                cme.setDateValue(dateFPtoDate(rFactors[i].getEndDate(), timeShiftInDays));
                cme.setValue(dateFPGetPartial(rFactors[i].getEndDate()));
                caseManagementManager.saveNoteExt(cme);
            }
            if (rFactors[i].getAgeOfOnset() != null) {
                cme.setKeyVal(CaseManagementNoteExt.AGEATONSET);
                cme.setDateValue((Date) null);
                cme.setValue(rFactors[i].getAgeOfOnset().toString());
                caseManagementManager.saveNoteExt(cme);
            }
            if (StringUtils.filled(rFactors[i].getExposureDetails())) {
                cme.setKeyVal(CaseManagementNoteExt.EXPOSUREDETAIL);
                cme.setDateValue((Date) null);
                cme.setValue(rFactors[i].getExposureDetails());
                caseManagementManager.saveNoteExt(cme);
            }
            if (rFactors[i].getLifeStage() != null) {
                cme.setKeyVal(CaseManagementNoteExt.LIFESTAGE);
                cme.setDateValue((Date) null);
                cme.setValue(rFactors[i].getLifeStage().toString());
                caseManagementManager.saveNoteExt(cme);
            }
        }

        //ALERTS & SPECIAL NEEDS
        AlertsAndSpecialNeeds[] alerts = patientRec.getAlertsAndSpecialNeedsArray();
        for (int i = 0; i < alerts.length; i++) {
            if (i == 0)
                scmi = getCMIssue("Reminders");
            CaseManagementNote cmNote = prepareCMNote("1", null);
            cmNote.setIssues(scmi);

            //main field
            String reminders = alerts[i].getAlertDescription();
            if (StringUtils.empty(reminders)) {
                err_data.add("Error! No Alert Description (" + (i + 1) + ")");
                reminders = "Imported Alert";
            }
            cmNote.setNote(reminders);
            if (cmNote.getProviderNo() == null)
                cmNote.setProviderNo("999998");
            if (cmNote.getSigning_provider_no() == null)
                cmNote.setSigning_provider_no("999998");
            caseManagementManager.saveNoteSimple(cmNote);
            addOneEntry(ALERT);

            //annotation
            Long hostNoteId = cmNote.getId();
            cmNote = prepareCMNote("2", null);
            String note = alerts[i].getNotes();
            cmNote.setNote(note);
            saveLinkNote(hostNoteId, cmNote);

            //to dumpsite
            String dump = "imported.cms4.2011.06";
            /*
            String summary = alerts[i].getCategorySummaryLine();
            if (StringUtils.empty(summary)) {
                    err_summ.add("No Summary for Alerts & Special Needs ("+(i+1)+")");
            }
            dump = Util.addLine(dump, summary);
            */
            /*
            dump = Util.addLine(dump, getResidual(alerts[i].getResidualInfo()));
            cmNote = prepareCMNote("2",null);
            cmNote.setNote(dump);
            saveLinkNote(hostNoteId, cmNote);
            */
            //extra fields
            CaseManagementNoteExt cme = new CaseManagementNoteExt();
            cme.setNoteId(hostNoteId);
            if (alerts[i].getDateActive() != null) {
                cme.setKeyVal(CaseManagementNoteExt.STARTDATE);
                cme.setDateValue(dateFPtoDate(alerts[i].getDateActive(), timeShiftInDays));
                cme.setValue(dateFPGetPartial(alerts[i].getDateActive()));
                caseManagementManager.saveNoteExt(cme);
            }
            if (alerts[i].getEndDate() != null) {
                cme.setKeyVal(CaseManagementNoteExt.RESOLUTIONDATE);
                cme.setDateValue(dateFPtoDate(alerts[i].getEndDate(), timeShiftInDays));
                cme.setValue(dateFPGetPartial(alerts[i].getEndDate()));
                caseManagementManager.saveNoteExt(cme);
            }
        }

        //CLINICAL NOTES
        ClinicalNotes[] cNotes = patientRec.getClinicalNotesArray();
        Date observeDate = new Date(), createDate = new Date();
        for (int i = 0; i < cNotes.length; i++) {
            //encounter note
            String encounter = cNotes[i].getMyClinicalNotesContent();
            if (StringUtils.empty(encounter)) {
                err_data.add("Empty clinical note - not added (" + (i + 1) + ")");
                continue;
            }

            //create date
            if (cNotes[i].getEnteredDateTime() != null) {
                createDate = dateTimeFPtoDate(cNotes[i].getEnteredDateTime(), timeShiftInDays);
                observeDate = createDate;
            }

            //observation date
            if (cNotes[i].getEventDateTime() != null) {
                observeDate = dateTimeFPtoDate(cNotes[i].getEventDateTime(), timeShiftInDays);
                if (cNotes[i].getEnteredDateTime() == null)
                    createDate = observeDate;

            }

            CaseManagementNote cmNote = prepareCMNote("1", null);
            cmNote.setUpdate_date(createDate);
            cmNote.setCreate_date(createDate);
            cmNote.setObservation_date(observeDate);
            cmNote.setNote(encounter);

            String uuid = null;
            ClinicalNotes.ParticipatingProviders[] participatingProviders = cNotes[i]
                    .getParticipatingProvidersArray();
            ClinicalNotes.NoteReviewer[] noteReviewers = cNotes[i].getNoteReviewerArray();

            int p_total = participatingProviders.length + noteReviewers.length;
            for (int p = 0; p < p_total; p++) {
                if (p > 0) {
                    cmNote = prepareCMNote("1", uuid);
                    cmNote.setObservation_date(observeDate);
                    cmNote.setCreate_date(createDate);
                    cmNote.setNote(encounter);
                }

                //participating providers
                if (p < participatingProviders.length) {
                    if (participatingProviders[p].getDateTimeNoteCreated() == null)
                        cmNote.setUpdate_date(new Date());
                    else
                        cmNote.setUpdate_date(dateTimeFPtoDate(
                                participatingProviders[p].getDateTimeNoteCreated(), timeShiftInDays));

                    if (participatingProviders[p].getName() != null) {
                        HashMap<String, String> authorName = getPersonName(participatingProviders[p].getName());
                        String authorOHIP = participatingProviders[p].getOHIPPhysicianId();
                        String authorProvider = writeProviderData(authorName.get("firstname"),
                                authorName.get("lastname"), authorOHIP);
                        if (StringUtils.empty(authorProvider)) {
                            authorProvider = defaultProviderNo();
                            err_note.add("Clinical notes have no author; assigned to \"doctor oscardoc\" ("
                                    + (i + 1) + ")");
                        }
                        cmNote.setProviderNo(authorProvider);
                        cmNote.setSigning_provider_no(authorProvider);
                    }
                } else {

                    //note reviewers
                    int r = p - participatingProviders.length;
                    if (noteReviewers[r].getName() != null) {
                        if (noteReviewers[r].getDateTimeNoteReviewed() == null)
                            cmNote.setUpdate_date(new Date());
                        else
                            cmNote.setUpdate_date(dateTimeFPtoDate(noteReviewers[r].getDateTimeNoteReviewed(),
                                    timeShiftInDays));

                        HashMap<String, String> authorName = getPersonName(noteReviewers[r].getName());
                        String reviewerOHIP = noteReviewers[r].getOHIPPhysicianId();
                        String reviewer = writeProviderData(authorName.get("firstname"),
                                authorName.get("lastname"), reviewerOHIP);

                        cmNote.setProviderNo(reviewer);
                        cmNote.setSigning_provider_no(reviewer);
                        Util.writeVerified(cmNote);
                    }
                }
                if (cmNote.getProviderNo() == null)
                    cmNote.setProviderNo("999998");
                if (cmNote.getSigning_provider_no() == null)
                    cmNote.setSigning_provider_no("999998");

                //Sset 
                caseManagementManager.saveNoteSimple(cmNote);

                //prepare for extra notes
                if (p == 0) {
                    addOneEntry(CLINICALNOTE);
                    uuid = cmNote.getUuid();

                    //create "header", cms4 only
                    if (cNotes[i].getEnteredDateTime() != null && !createDate.equals(cmNote.getUpdate_date())) {
                        CaseManagementNote headNote = prepareCMNote("2", null);
                        headNote.setCreate_date(createDate);
                        headNote.setUpdate_date(createDate);
                        headNote.setObservation_date(observeDate);
                        headNote.setNote("imported.cms4.2011.06" + uuid);
                        if (headNote.getProviderNo() == null)
                            headNote.setProviderNo("999998");
                        if (cmNote.getSigning_provider_no() == null)
                            cmNote.setSigning_provider_no("999998");
                        caseManagementManager.saveNoteSimple(headNote);
                    }

                }
            }
            if (p_total == 0) {
                err_note.add(
                        "Clinical notes have no author; assigned to \"doctor oscardoc\" (" + (i + 1) + ")");
                if (cmNote.getProviderNo() == null)
                    cmNote.setProviderNo("999998");
                if (cmNote.getSigning_provider_no() == null)
                    cmNote.setSigning_provider_no("999998");
                caseManagementManager.saveNoteSimple(cmNote);
            }

            //to dumpsite
            /*
            String noteType = cNotes[i].getNoteType();
            if (StringUtils.filled(noteType)) {
               noteType = Util.addLine("imported.cms4.2011.06", "Note Type: ", noteType);
            }
                    
            CaseManagementNote dumpNote = prepareCMNote("2",null);
            dumpNote.setNote(noteType);
            saveLinkNote(cmNote.getId(), dumpNote);
            */
        }

        //ALLERGIES & ADVERSE REACTIONS
        AllergiesAndAdverseReactions[] aaReactArray = patientRec.getAllergiesAndAdverseReactionsArray();
        for (int i = 0; i < aaReactArray.length; i++) {
            String description = "", regionalId = "", reaction = "", severity = "", entryDate = "",
                    startDate = "", typeCode = "", lifeStage = "", alg_extra = "";
            String entryDateFormat = null, startDateFormat = null;

            reaction = StringUtils.noNull(aaReactArray[i].getReaction());
            description = StringUtils.noNull(aaReactArray[i].getOffendingAgentDescription());
            entryDate = dateFPtoString(aaReactArray[i].getRecordedDate(), timeShiftInDays);
            startDate = dateFPtoString(aaReactArray[i].getStartDate(), timeShiftInDays);
            if (aaReactArray[i].getLifeStage() != null)
                lifeStage = aaReactArray[i].getLifeStage().toString();

            if (StringUtils.empty(entryDate))
                entryDate = null;
            else
                entryDateFormat = dateFPGetPartial(aaReactArray[i].getRecordedDate());
            if (StringUtils.empty(startDate))
                startDate = null;
            else
                startDateFormat = dateFPGetPartial(aaReactArray[i].getStartDate());

            if (aaReactArray[i].getCode() != null)
                regionalId = StringUtils.noNull(aaReactArray[i].getCode().getCodeValue());
            alg_extra = Util.addLine(alg_extra, "Offending Agent Description: ",
                    aaReactArray[i].getOffendingAgentDescription());
            if (aaReactArray[i].getReactionType() != null)
                alg_extra = Util.addLine(alg_extra, "Reaction Type: ",
                        aaReactArray[i].getReactionType().toString());

            if (typeCode.equals("") && aaReactArray[i].getPropertyOfOffendingAgent() != null) {
                if (aaReactArray[i].getPropertyOfOffendingAgent() == cdsDt.PropertyOfOffendingAgent.DR)
                    typeCode = "13"; //drug
                else if (aaReactArray[i].getPropertyOfOffendingAgent() == cdsDt.PropertyOfOffendingAgent.ND)
                    typeCode = "0"; //non-drug
                else if (aaReactArray[i].getPropertyOfOffendingAgent() == cdsDt.PropertyOfOffendingAgent.UK)
                    typeCode = "0"; //unknown
            }
            if (aaReactArray[i].getSeverity() != null) {
                if (aaReactArray[i].getSeverity() == cdsDt.AdverseReactionSeverity.MI)
                    severity = "1"; //mild
                else if (aaReactArray[i].getSeverity() == cdsDt.AdverseReactionSeverity.MO)
                    severity = "2"; //moderate
                else if (aaReactArray[i].getSeverity() == cdsDt.AdverseReactionSeverity.LT)
                    severity = "3"; //severe
                else if (aaReactArray[i].getSeverity() == cdsDt.AdverseReactionSeverity.NO) {
                    severity = "4"; //No reaction, map to unknown
                    alg_extra = Util.addLine(alg_extra, "Severity: No reaction");
                }
            } else {
                severity = "4"; //severity unknown
            }

            Date entryDateDate = toDateFromString(entryDate);
            Date startDateDate = toDateFromString(startDate);
            Integer allergyId = saveRxAllergy(Integer.valueOf(demographicNo), entryDateDate, description,
                    Integer.parseInt(typeCode), reaction, startDateDate, severity, regionalId, lifeStage);
            addOneEntry(ALLERGY);

            //write partial dates
            if (entryDateFormat != null)
                partialDateDao.setPartialDate(PartialDate.ALLERGIES, allergyId.intValue(),
                        PartialDate.ALLERGIES_ENTRYDATE, entryDateFormat);
            if (startDateFormat != null)
                partialDateDao.setPartialDate(PartialDate.ALLERGIES, allergyId.intValue(),
                        PartialDate.ALLERGIES_STARTDATE, startDateFormat);

            //annotation
            String note = StringUtils.noNull(aaReactArray[i].getNotes());
            CaseManagementNote cmNote = prepareCMNote("2", null);
            cmNote.setNote(note);
            saveLinkNote(cmNote, CaseManagementNoteLink.ALLERGIES, Long.valueOf(allergyId));

            //to dumpsite
            String dump = "imported.cms4.2011.06";
            /*
            String summary = aaReactArray[i].getCategorySummaryLine();
            if (StringUtils.empty(summary)) {
                err_summ.add("No Summary for Allergies & Adverse Reactions ("+(i+1)+")");
            }
            dump = Util.addLine(dump, summary);
            */
            /*
            dump = Util.addLine(dump, alg_extra);
            dump = Util.addLine(dump, getResidual(aaReactArray[i].getResidualInfo()));
                    
            cmNote = prepareCMNote("2",null);
            cmNote.setNote(dump);
            saveLinkNote(cmNote, CaseManagementNoteLink.ALLERGIES, Long.valueOf(allergyId));
            */
        }

        //MEDICATIONS & TREATMENTS
        MedicationsAndTreatments[] medArray = patientRec.getMedicationsAndTreatmentsArray();
        String duration, quantity, dosage, special;
        for (int i = 0; i < medArray.length; i++) {
            Drug drug = new Drug();
            drug.setCreateDate(dateTimeFPtoDate(medArray[i].getPrescriptionWrittenDate(), timeShiftInDays));
            drug.setWrittenDate(dateTimeFPtoDate(medArray[i].getPrescriptionWrittenDate(), timeShiftInDays));
            String writtenDateFormat = dateFPGetPartial(medArray[i].getPrescriptionWrittenDate());

            drug.setRxDate(dateFPtoDate(medArray[i].getStartDate(), timeShiftInDays));
            if (medArray[i].getStartDate() == null)
                drug.setRxDate(drug.getWrittenDate());

            duration = medArray[i].getDuration();
            if (StringUtils.filled(duration)) {
                duration = duration.trim();
                if (duration.endsWith("days"))
                    duration = Util.leadingNum(duration);
                if (NumberUtils.isDigits(duration)) {
                    drug.setDuration(duration);
                    drug.setDurUnit("D");
                } else
                    err_data.add("Error! Invalid Duration [" + medArray[i].getDuration() + "] for Medications");
            }

            quantity = medArray[i].getQuantity();
            if (StringUtils.filled(quantity)) {
                quantity = Util.leadingNum(quantity.trim());
                if (NumberUtils.isNumber(quantity)) {
                    drug.setQuantity(quantity);
                } else
                    err_data.add("Error! Invalid Quantity [" + medArray[i].getQuantity() + "] for Medications");
            }

            Calendar endDate = Calendar.getInstance();
            endDate.setTime(drug.getRxDate() == null ? new Date() : drug.getRxDate());
            if (StringUtils.filled(duration))
                endDate.add(Calendar.DAY_OF_YEAR, Integer.valueOf(duration) + timeShiftInDays);
            drug.setEndDate(endDate.getTime());

            String freq = StringUtils.noNull(medArray[i].getFrequency());
            int prnPos = freq.toUpperCase().indexOf("PRN");
            if (prnPos >= 0) {
                drug.setPrn(true);
                freq = freq.substring(0, prnPos).trim() + " " + freq.substring(prnPos + 3).trim(); //remove "prn" from freq
            }
            drug.setFreqCode(freq);

            drug.setFreqCode(medArray[i].getFrequency());
            if (medArray[i].getFrequency() != null && medArray[i].getFrequency().contains("PRN"))
                drug.setPrn(true);
            else
                drug.setPrn(false);

            drug.setRegionalIdentifier(medArray[i].getDrugIdentificationNumber());
            drug.setRoute(medArray[i].getRoute());
            drug.setDrugForm(medArray[i].getForm());
            drug.setLongTerm(getYN(medArray[i].getLongTermMedication()).equals("Yes"));
            drug.setPastMed(getYN(medArray[i].getPastMedications()).equals("Yes"));
            drug.setPatientCompliance(getYN(medArray[i].getPatientCompliance()));

            if (NumberUtils.isDigits(medArray[i].getNumberOfRefills()))
                drug.setRepeat(Integer.valueOf(medArray[i].getNumberOfRefills()));
            duration = medArray[i].getRefillDuration();
            if (StringUtils.filled(duration)) {
                duration = duration.trim();
                if (duration.endsWith("days"))
                    duration = Util.leadingNum(duration);
                if (NumberUtils.isDigits(duration))
                    drug.setRefillDuration(Integer.valueOf(duration));
                else
                    err_data.add("Error! Invalid Refill Duration [" + medArray[i].getRefillDuration()
                            + "] for Medications");
            }
            if (drug.getRefillDuration() == null)
                drug.setRefillDuration(0);

            quantity = medArray[i].getRefillQuantity();
            if (StringUtils.filled(quantity)) {
                quantity = Util.leadingNum(quantity.trim());
                if (NumberUtils.isNumber(quantity))
                    drug.setRefillQuantity(Integer.valueOf(quantity));
                else
                    err_data.add("Error! Invalid Refill Quantity [" + medArray[i].getRefillQuantity()
                            + "] for Medications");
            }
            if (drug.getRefillQuantity() == null)
                drug.setRefillQuantity(0);

            drug.setETreatmentType(medArray[i].getTreatmentType());
            //no need: DrugReason drugReason = new DrugReason();
            //no need: drug.setRxStatus(medArray[i].getPrescriptionStatus());

            //no need: String nosub = medArray[i].getSubstitutionNotAllowed();
            //no need: if (nosub!=null) drug.setNoSubs(nosub.equalsIgnoreCase("Y"));

            //no need: String non_auth = medArray[i].getNonAuthoritativeIndicator();
            //no need: if (non_auth!=null) drug.setNonAuthoritative(non_auth.equalsIgnoreCase("Y"));
            //no need: else  err_data.add("Error! No non-authoritative indicator for Medications & Treatments ("+(i+1)+")");

            if (NumberUtils.isDigits(medArray[i].getDispenseInterval()))
                drug.setDispenseInterval(Integer.parseInt(medArray[i].getDispenseInterval()));
            else {
                err_data.add("Error! Invalid Dispense Interval for Medications & Treatments (" + (i + 1) + ")");
                drug.setDispenseInterval(0);
            }

            String take = StringUtils.noNull(medArray[i].getDosage()).trim();
            drug.setTakeMin(Util.leadingNumF(take));
            int sep = take.indexOf("-");
            if (sep > 0)
                drug.setTakeMax(Util.leadingNumF(take.substring(sep + 1)));
            else
                drug.setTakeMax(drug.getTakeMin());
            drug.setUnit(medArray[i].getDosageUnitOfMeasure());
            if ("table".equalsIgnoreCase(drug.getUnit()))
                drug.setUnit("tab");

            drug.setDemographicId(Integer.valueOf(demographicNo));
            drug.setArchived(false);

            drug.setBrandName(medArray[i].getDrugName());
            drug.setCustomName(medArray[i].getDrugDescription());

            special = StringUtils.noNull(drug.getBrandName());
            if (special.equals("")) {
                special = StringUtils.noNull(drug.getCustomName());
                drug.setCustomInstructions(true);
            }

            cdsDt.DrugMeasure strength = medArray[i].getStrength();
            if (strength != null) {
                String dosageValue = StringUtils.noNull(strength.getAmount());
                String dosageUnit = StringUtils.noNull(strength.getUnitOfMeasure());

                if (dosageValue.contains("/")) {
                    String[] dValue = dosageValue.split("/");
                    String[] dUnit = dosageUnit.split("/");
                    dosage = dValue[0] + dUnit[0] + " / " + dValue[1] + (dUnit.length > 1 ? dUnit[1] : "unit");
                } else {
                    dosage = dosageValue + " " + dosageUnit;
                }
                drug.setDosage(dosage);
            }

            special = addSpaced(special, medArray[i].getDosage());
            special = addSpaced(special, drug.getRoute());
            special = addSpaced(special, drug.getFreqCode());

            if (drug.getDuration() != null) {
                special = addSpaced(special, "for " + drug.getDuration() + " days");
            }
            drug.setSpecial(special);

            //no need: special = Util.addLine(special, "Prescription Status: ", medArray[i].getPrescriptionStatus());
            //no need: special = Util.addLine(special, "Dispense Interval: ", medArray[i].getDispenseInterval());
            //no need: special = Util.addLine(special, "Protocol Id: ", medArray[i].getProtocolIdentifier());
            //no need: special = Util.addLine(special, "Prescription Id: ", medArray[i].getPrescriptionIdentifier());
            //no need: special = Util.addLine(special, "Prior Prescription Id: ", medArray[i].getPriorPrescriptionReferenceIdentifier());

            if (StringUtils.filled(medArray[i].getPrescriptionInstructions())) {
                drug.setSpecialInstruction(medArray[i].getPrescriptionInstructions());
            }

            if (medArray[i].getPrescribedBy() != null) {
                HashMap<String, String> personName = getPersonName(medArray[i].getPrescribedBy().getName());
                String personOHIP = medArray[i].getPrescribedBy().getOHIPPhysicianId();
                ProviderData pd = getProviderByOhip(personOHIP);
                if (pd != null && Integer.valueOf(pd.getProviderNo()) > -1000)
                    drug.setProviderNo(pd.getProviderNo());
                else { //outside provider
                    drug.setOutsideProviderName(StringUtils.noNull(personName.get("lastname")) + ", "
                            + StringUtils.noNull(personName.get("firstname")));
                    drug.setOutsideProviderOhip(personOHIP);
                    drug.setProviderNo(writeProviderData(personName.get("firstname"),
                            personName.get("lastname"), personOHIP));
                }
            } else {
                drug.setProviderNo(admProviderNo);
            }
            drug.setPosition(0);
            if (drug.getRxDate() == null) {
                drug.setRxDate(UtilDateUtilities.StringToDate("1900-01-01", "yyyy-MM-dd"));
            }

            drugDao.persist(drug);
            addOneEntry(MEDICATION);

            /* no need:
            if (medArray[i].getProblemCode()!=null) {
                drugReason.setCode(medArray[i].getProblemCode());
                drugReason.setDemographicNo(Integer.valueOf(demographicNo));
                drugReason.setDrugId(drug.getId());
                drugReason.setProviderNo(drug.getProviderNo());
                drugReason.setPrimaryReasonFlag(true);
                drugReason.setArchivedFlag(false);
                drugReasonDao.persist(drugReason);
            }
             *
             */

            //partial date
            partialDateDao.setPartialDate(PartialDate.DRUGS, drug.getId(), PartialDate.DRUGS_WRITTENDATE,
                    writtenDateFormat);

            //annotation
            CaseManagementNote cmNote = prepareCMNote("2", null);
            String note = StringUtils.noNull(medArray[i].getNotes());
            cmNote.setNote(note);
            saveLinkNote(cmNote, CaseManagementNoteLink.DRUGS, (long) drug.getId());

            //to dumpsite
            String dump = "imported.cms4.2011.06";
            /*
            String summary = medArray[i].getCategorySummaryLine();
            if (StringUtils.empty(summary)) {
                err_summ.add("No Summary for Medications & Treatments ("+(i+1)+")");
            }
            dump = Util.addLine(dump, summary);
            */
            /*
            dump = Util.addLine(dump, getResidual(medArray[i].getResidualInfo()));
                    
            cmNote = prepareCMNote("2",null);
            cmNote.setNote(dump);
            saveLinkNote(cmNote, CaseManagementNoteLink.DRUGS, (long)drug.getId());
            */
        }

        //IMMUNIZATIONS
        Immunizations[] immuArray = patientRec.getImmunizationsArray();
        for (int i = 0; i < immuArray.length; i++) {
            String preventionDate = "", refused = "0";
            String preventionType = null, immExtra = null;
            ArrayList<Map<String, String>> preventionExt = new ArrayList<Map<String, String>>();

            if (StringUtils.filled(immuArray[i].getImmunizationName())) {
                Map<String, String> ht = new HashMap<String, String>();
                ht.put("name", immuArray[i].getImmunizationName());
                preventionExt.add(ht);
            } else {
                err_data.add("Error! No Immunization Name (" + (i + 1) + ")");
            }

            if (immuArray[i].getImmunizationType() != null)
                preventionType = Util.getPreventionType(immuArray[i].getImmunizationType().toString());

            if (preventionType == null) {
                if (immuArray[i].getImmunizationName() != null)
                    preventionType = Util.getPreventionType(immuArray[i].getImmunizationName());
            }

            //               if (preventionType==null)
            //                       preventionType = mapPreventionTypeByCode(immuArray[i].getImmunizationCode());
            if (preventionType == null) {
                preventionType = "OtherA";
                err_note.add("Cannot map Immunization Type, " + immuArray[i].getImmunizationName()
                        + " mapped to Other Layout A");
            }

            if (StringUtils.filled(immuArray[i].getManufacturer())) {
                Map<String, String> ht = new HashMap<String, String>();
                ht.put("manufacture", immuArray[i].getManufacturer());
                preventionExt.add(ht);
            }
            if (StringUtils.filled(immuArray[i].getLotNumber())) {
                Map<String, String> ht = new HashMap<String, String>();
                ht.put("lot", immuArray[i].getLotNumber());
                preventionExt.add(ht);
            }
            if (StringUtils.filled(immuArray[i].getRoute())) {
                Map<String, String> ht = new HashMap<String, String>();
                ht.put("route", immuArray[i].getRoute());
                preventionExt.add(ht);
            }
            if (StringUtils.filled(immuArray[i].getSite())) {
                Map<String, String> ht = new HashMap<String, String>();
                ht.put("location", immuArray[i].getSite());
                preventionExt.add(ht);
            }
            if (StringUtils.filled(immuArray[i].getDose())) {
                Map<String, String> ht = new HashMap<String, String>();
                ht.put("dose", immuArray[i].getDose());
                preventionExt.add(ht);
            }

            if (StringUtils.filled(immuArray[i].getNotes())) {
                Map<String, String> ht = new HashMap<String, String>();
                ht.put("comments", immuArray[i].getNotes());
                preventionExt.add(ht);
            }

            preventionDate = dateFPtoString(immuArray[i].getDate(), timeShiftInDays);
            refused = getYN(immuArray[i].getRefusedFlag()).equals("Yes") ? "1" : "0";
            if (immuArray[i].getRefusedFlag() == null)
                err_data.add("Error! No Refused Flag for Immunizations (" + (i + 1) + ")");
            /*
            String iSummary="";
            if (immuArray[i].getCategorySummaryLine()!=null) {
                iSummary = immuArray[i].getCategorySummaryLine().trim();
            } else {
                err_summ.add("No Summary for Immunizations ("+(i+1)+")");
            }
                    
                    
            if (StringUtils.filled(iSummary)) {
                comments = Util.addLine(comments, "Summary: ", iSummary);
                err_note.add("Immunization Summary imported in [comments] ("+(i+1)+")");
            }
             *
             */

            immExtra = Util.addLine(immExtra, getCode(immuArray[i].getImmunizationCode(), "Immunization Code"));
            immExtra = Util.addLine(immExtra, "Instructions: ", immuArray[i].getInstructions());
            immExtra = Util.addLine(immExtra, getResidual(immuArray[i].getResidualInfo()));

            Integer preventionId = PreventionData.insertPreventionData(admProviderNo, demographicNo,
                    preventionDate, defaultProviderNo(), "", preventionType, refused, "", "", preventionExt);
            addOneEntry(IMMUNIZATION);

            //to dumpsite: Extra immunization data
            /*
            if (StringUtils.filled(immExtra) && preventionId>=0) {
               immExtra = Util.addLine("imported.cms4.2011.06", immExtra);
               CaseManagementNote imNote = prepareCMNote("2",null);
               imNote.setNote(immExtra);
               saveLinkNote(imNote, CaseManagementNoteLink.PREVENTIONS, Long.valueOf(preventionId));
            }*/
        }

        //LABORATORY RESULTS
        LaboratoryResults[] labResultArr = patientRec.getLaboratoryResultsArray();
        String[] _accession = new String[labResultArr.length];
        String[] _coll_date = new String[labResultArr.length];
        String[] _title = new String[labResultArr.length];
        String[] _testName = new String[labResultArr.length];
        String[] _abn = new String[labResultArr.length];
        String[] _minimum = new String[labResultArr.length];
        String[] _maximum = new String[labResultArr.length];
        String[] _result = new String[labResultArr.length];
        String[] _unit = new String[labResultArr.length];
        String[] _labnotes = new String[labResultArr.length];
        String[] _location = new String[labResultArr.length];
        String[] _reviewer = new String[labResultArr.length];
        String[] _lab_ppid = new String[labResultArr.length];
        String[] _rev_date = new String[labResultArr.length];
        String[] _req_date = new String[labResultArr.length];

        // Save to labPatientPhysicianInfo, labTestResults, patientLabRouting
        String accessionString = "Temp";
        int accNum = 0;
        String tempCollectionDate = "";
        for (int i = 0; i < labResultArr.length; i++) {
            _location[i] = StringUtils.noNull(labResultArr[i].getLaboratoryName());
            _accession[i] = StringUtils.noNull(labResultArr[i].getAccessionNumber());
            _coll_date[i] = dateFPtoString(labResultArr[i].getCollectionDateTime(), timeShiftInDays);
            //labResultArr[i].getCollectionDateTime() return : <cdsd:DateTime xmlns:cdsd="cds_dt" xmlns="cds" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2007-07-24T00:00:00-05:00</cdsd:DateTime>
            //labResultArr[i].getLabRequisitionDateTime().getFullDate() or any other mothods always return NULL.    
            String collectionDateTimeXmlText = "";
            String dateText = "";
            Calendar c = labResultArr[i].getCollectionDateTime().getFullDateTime(); //get null
            if (c == null) {
                collectionDateTimeXmlText = labResultArr[i].getCollectionDateTime().toString();
                if (collectionDateTimeXmlText != null) {
                    dateText = collectionDateTimeXmlText.substring(collectionDateTimeXmlText.indexOf(">") + 1,
                            collectionDateTimeXmlText.indexOf("</"));
                }
            }

            if (_coll_date[i] == null || _coll_date[i].equals("")) {
                if (!StringUtils.isNullOrEmpty(dateText) && dateText.length() > 10) {
                    _coll_date[i] = dateText.substring(0, 10);
                }
            }

            if (StringUtils.isNullOrEmpty(_accession[i])) { //the lab accession number from another EMR could be null, which does not mean all labs without accession number should belong to one accession.
                _accession[i] = dateText; //use collection date as accession number if the lab has no accesssion number.                       
            }

            _req_date[i] = dateFPtoString(labResultArr[i].getLabRequisitionDateTime(), timeShiftInDays);
            if (StringUtils.empty(_req_date[i]))
                _req_date[i] = _coll_date[i];

            _testName[i] = StringUtils.noNull(labResultArr[i].getTestName());
            if (StringUtils.filled(labResultArr[i].getTestNameReportedByLab())) {
                _testName[i] += StringUtils.filled(_testName[i]) ? " / " : "";
                _testName[i] += labResultArr[i].getTestNameReportedByLab();
            }
            _title[i] = _testName[i];

            if (StringUtils.filled(labResultArr[i].getNotesFromLab()))
                _labnotes[i] = "Notes: " + labResultArr[i].getNotesFromLab();

            if (labResultArr[i].getResultNormalAbnormalFlag() != null) {
                cdsDt.ResultNormalAbnormalFlag.Enum flag = labResultArr[i].getResultNormalAbnormalFlag();
                if (flag.equals(cdsDt.ResultNormalAbnormalFlag.Y))
                    _abn[i] = "A";
                if (flag.equals(cdsDt.ResultNormalAbnormalFlag.N))
                    _abn[i] = "N";
            }

            if (labResultArr[i].getResult() != null) {
                _result[i] = StringUtils.noNull(labResultArr[i].getResult().getValue());
                _unit[i] = StringUtils.noNull(labResultArr[i].getResult().getUnitOfMeasure());
            }

            if (labResultArr[i].getReferenceRange() != null) {
                LaboratoryResults.ReferenceRange ref = labResultArr[i].getReferenceRange();
                if (StringUtils.filled(ref.getReferenceRangeText())) {
                    _minimum[i] = ref.getReferenceRangeText();
                } else {
                    _maximum[i] = StringUtils.noNull(ref.getHighLimit());
                    _minimum[i] = StringUtils.noNull(ref.getLowLimit());
                }
            }

            LaboratoryResults.ResultReviewer[] resultReviewers = labResultArr[i].getResultReviewerArray();
            if (resultReviewers.length > 0) {
                HashMap<String, String> revName = getPersonName(resultReviewers[0].getName());
                String revOhip = StringUtils.noNull(resultReviewers[0].getOHIPPhysicianId());
                _reviewer[i] = writeProviderData(revName.get("firstname"), revName.get("lastname"), revOhip);
                _rev_date[i] = dateFPtoString(resultReviewers[0].getDateTimeResultReviewed(), timeShiftInDays);
            }
        }

        ArrayList<String> uniq_labs = getUniques(_location);
        ArrayList<String> uniq_accs = getUniques(_accession);
        for (String lab : uniq_labs) {
            boolean labNew = true;
            String rptInfoId = "";
            for (String acc : uniq_accs) {
                boolean accNew = true;
                String paPhysId = "";
                for (int i = 0; i < _location.length; i++) {
                    if (!(_location[i].equals(lab) && _accession[i].equals(acc)))
                        continue;

                    if (labNew) {
                        rptInfoId = LabResultImport.saveLabReportInfo(_location[i]);
                        labNew = false;
                    }
                    if (accNew) {
                        paPhysId = LabResultImport.saveLabPatientPhysicianInfo(rptInfoId, _accession[i],
                                _coll_date[i], firstName, lastName, sex, hin, birthDate, patientPhone);

                        LabResultImport.savePatientLabRouting(demographicNo, paPhysId);
                        LabRequestReportLink.save(null, null, _req_date[i], "labPatientPhysicianInfo",
                                Long.valueOf(paPhysId));

                        String status = StringUtils.filled(_reviewer[i]) ? "A" : "N";
                        _reviewer[i] = status.equals("A") ? _reviewer[i] : "0";
                        LabResultImport.saveProviderLabRouting(_reviewer[i], paPhysId, status, "",
                                _rev_date[i]);

                        accNew = false;
                    }
                    LabResultImport.saveLabTestResults(_title[i], _testName[i], _abn[i], _minimum[i],
                            _maximum[i], _result[i], _unit[i], "", _location[i], paPhysId, "C", "Y");
                    LabResultImport.saveLabTestResults(_title[i], _testName[i], "", "", "", "", "",
                            _labnotes[i], _location[i], paPhysId, "D", "Y");
                    addOneEntry(LABS);

                    _lab_ppid[i] = paPhysId;
                }
            }
        }
        /*
        String labEverything = getLabDline(labResultArr[i]);
        if (StringUtils.filled(labEverything)){
            LabResultImport.SaveLabDesc(labEverything,patiPhysId);
        }
        */

        // Save to measurements, measurementsExt
        for (LaboratoryResults labResults : labResultArr) {
            Measurements meas = new Measurements(Long.valueOf(demographicNo), admProviderNo);
            LaboratoryResults.Result result = labResults.getResult();

            //save lab result & get unit
            String unit = null;
            if (result != null) {
                meas.setDataField(StringUtils.noNull(result.getValue()));
                unit = StringUtils.noNull(result.getUnitOfMeasure());
            } else {
                meas.setDataField("");
            }

            //save lab result unit
            meas.setDateEntered(new Date());
            ImportExportMeasurements.saveMeasurements(meas);
            Long measId = meas.getId();
            saveMeasurementsExt(measId, "unit", unit);

            //save lab test code, test name
            String testCode = labResults.getLabTestCode();
            String testName = labResults.getTestName();
            String testNameLab = labResults.getTestNameReportedByLab();

            saveMeasurementsExt(measId, "identifier", testCode);
            saveMeasurementsExt(measId, "name_internal", testName);
            saveMeasurementsExt(measId, "name", testNameLab);

            //save lab collection datetime
            cdsDt.DateTimeFullOrPartial collDate = labResults.getCollectionDateTime();
            String coll_date = null;
            if (collDate != null) {
                coll_date = dateFPtoString(collDate, timeShiftInDays);
                saveMeasurementsExt(measId, "datetime", coll_date);
            } else {
                err_data.add("Error! No Collection DateTime for Lab Test " + testCode + " for Patient "
                        + demographicNo);
            }

            //save lab requisition datetime
            cdsDt.DateTimeFullOrPartial reqDate = labResults.getLabRequisitionDateTime();
            if (reqDate != null) {
                saveMeasurementsExt(measId, "request_datetime", dateFPtoString(reqDate, timeShiftInDays));
            }

            //save laboratory name
            String labname = StringUtils.noNull(labResults.getLaboratoryName());
            if (StringUtils.filled(labname)) {
                saveMeasurementsExt(measId, "labname", labname);
            } else {
                err_data.add(
                        "Error! No Laboratory Name for Lab Test " + testCode + " for Patient " + demographicNo);
            }

            //save lab normal/abnormal flag
            cdsDt.ResultNormalAbnormalFlag.Enum abnFlag = labResults.getResultNormalAbnormalFlag();
            if (abnFlag != null) {
                String abn = abnFlag.toString();
                if (!abn.equals("U")) {
                    saveMeasurementsExt(measId, "abnormal", (abn.equals("Y") ? "A" : abn));
                }
            }

            //save lab accession number
            String accnum = labResults.getAccessionNumber();
            saveMeasurementsExt(measId, "accession", accnum);

            //save lab reference range
            LaboratoryResults.ReferenceRange refRange = labResults.getReferenceRange();
            if (refRange != null) {
                if (StringUtils.filled(refRange.getReferenceRangeText())) {
                    saveMeasurementsExt(measId, "range", refRange.getReferenceRangeText());
                } else {
                    saveMeasurementsExt(measId, "maximum", refRange.getHighLimit());
                    saveMeasurementsExt(measId, "minimum", refRange.getLowLimit());
                }
            }

            //save OLIS test result status
            String olis_status = labResults.getOLISTestResultStatus();
            if (StringUtils.filled(olis_status))
                saveMeasurementsExt(measId, "olis_status", olis_status);

            //save notes from lab
            String labNotes = labResults.getNotesFromLab();
            if (StringUtils.filled(labNotes))
                saveMeasurementsExt(measId, "comments", labNotes);

            //retrieve lab_ppid
            String lab_ppid = null;
            for (int i = 0; i < labResultArr.length; i++) {
                if (!(_location[i].equals(labname) && _coll_date[i].equals(coll_date)))
                    continue;
                saveMeasurementsExt(measId, "lab_ppid", _lab_ppid[i]);
                lab_ppid = _lab_ppid[i];
                break;
            }

            if (lab_ppid != null) {

                //save lab physician notes (annotation)
                String annotation = labResults.getPhysiciansNotes();
                if (StringUtils.filled(annotation)) {
                    saveMeasurementsExt(measId, "other_id", "0-0");
                    CaseManagementNote cmNote = prepareCMNote("2", null);
                    cmNote.setNote(annotation);
                    saveLinkNote(cmNote, CaseManagementNoteLink.LABTEST2, Long.valueOf(lab_ppid), "0-0");
                }

                //to dumpsite
                /*
                String testResultsInfo = labResults.getTestResultsInformationReportedByTheLab();
                if (StringUtils.filled(testResultsInfo)) {
                    String dump = Util.addLine("imported.cms4.2011.06", "Test Results Info: ", testResultsInfo);
                    CaseManagementNote cmNote = prepareCMNote("2",null);
                    cmNote.setNote(dump);
                    saveLinkNote(cmNote, CaseManagementNoteLink.LABTEST2, Long.valueOf(lab_ppid), "0-0");
                }
                */
            } else {
                logger.error("No lab no! (demo=" + demographicNo + ")");
            }
        }

        //APPOINTMENTS
        Appointments[] appArray = patientRec.getAppointmentsArray();
        Date appointmentDate = null;
        String notes = "", reason = "", status = "", startTime = "", endTime = "", apptProvider = "";
        ApptStatusData asd = new ApptStatusData();
        String[] allStatus = asd.getAllStatus();
        String[] allTitle = asd.getAllTitle();

        for (int i = 0; i < appArray.length; i++) {
            String apptDateStr = dateFPtoString(appArray[i].getAppointmentDate(), timeShiftInDays);
            if (StringUtils.filled(apptDateStr)) {
                appointmentDate = UtilDateUtilities.StringToDate(apptDateStr);
            } else {
                err_data.add("Error! No Appointment Date (" + (i + 1) + ")");
            }
            if (appArray[i].getAppointmentTime() != null) {
                startTime = getCalTime(appArray[i].getAppointmentTime());
                if (appArray[i].getDuration() != null) {
                    Date d_startTime = appArray[i].getAppointmentTime().getTime();
                    Date d_endTime = new Date();
                    d_endTime.setTime(
                            d_startTime.getTime() + (appArray[i].getDuration().longValue() - 1) * 60000);
                    endTime = UtilDateUtilities.DateToString(d_endTime, "HH:mm:ss");
                } else {
                    Date d_startTime = appArray[i].getAppointmentTime().getTime();
                    Date d_endTime = new Date();
                    d_endTime.setTime(d_startTime.getTime() + 14 * 60000);
                    endTime = UtilDateUtilities.DateToString(d_endTime, "HH:mm:ss");
                }
            } else {
                err_data.add("Error! No Appointment Time (" + (i + 1) + ")");
            }
            if (StringUtils.filled(appArray[i].getAppointmentNotes())) {
                notes = appArray[i].getAppointmentNotes();
            }
            String apptStatus_tmp = appArray[i].getAppointmentStatus(); //return "Ready", "EntireEventCancelled", or "NoShow"
            String apptStatus = "";
            status = "";
            if (apptStatus_tmp != null) {
                if (apptStatus_tmp.equalsIgnoreCase("Ready"))
                    apptStatus = "To Do";
                else if (apptStatus_tmp.equalsIgnoreCase("EntireEventCancelled"))
                    apptStatus = "Cancelled";
                else if (apptStatus_tmp.equalsIgnoreCase("NoShow"))
                    apptStatus = "No Show";
                else
                    apptStatus = "";
                for (int j = 1; j < allStatus.length; j++) {
                    String msg = getResources(request).getMessage(allTitle[j]); //return "To Do", "Cancelled" or "No Show"
                    if (apptStatus.equalsIgnoreCase(msg)) {
                        status = allStatus[j];
                        apptStatus = null;
                        break;
                    }
                }
                if (StringUtils.empty(status)) {
                    status = allStatus[0];
                    err_note.add("Cannot map appointment status [" + apptStatus_tmp
                            + "]. Appointment Status set to [To Do]");
                }

            }
            err_note.add("Get appt status: [" + apptStatus_tmp + "] from xml file.");

            reason = StringUtils.noNull(appArray[i].getAppointmentPurpose());
            if (appArray[i].getProvider() != null) {
                HashMap<String, String> providerName = getPersonName(appArray[i].getProvider().getName());
                String personOHIP = appArray[i].getProvider().getOHIPPhysicianId();
                apptProvider = writeProviderData(providerName.get("firstname"), providerName.get("lastname"),
                        personOHIP);
                if (StringUtils.empty(apptProvider)) {
                    apptProvider = defaultProviderNo();
                    err_note.add(
                            "Appointment has no provider; assigned to \"doctor oscardoc\" (" + (i + 1) + ")");
                }
            }
            oscarSuperManager.update("appointmentDao", "import_appt",
                    new Object[] { apptProvider, appointmentDate, startTime, endTime, patientName,
                            demographicNo, notes, reason, status, apptStatus });
            addOneEntry(APPOINTMENT);
        }

        //REPORTS RECEIVED

        HRMDocumentDao hrmDocDao = (HRMDocumentDao) SpringUtils.getBean("HRMDocumentDao");
        HRMDocumentCommentDao hrmDocCommentDao = (HRMDocumentCommentDao) SpringUtils
                .getBean("HRMDocumentCommentDao");
        HRMDocumentSubClassDao hrmDocSubClassDao = (HRMDocumentSubClassDao) SpringUtils
                .getBean("HRMDocumentSubClassDao");
        HRMDocumentToDemographicDao hrmDocToDemoDao = (HRMDocumentToDemographicDao) SpringUtils
                .getBean("HRMDocumentToDemographicDao");

        ReportsReceived[] repR = patientRec.getReportsReceivedArray();
        List<ReportsReceived> HRMreports = new ArrayList<ReportsReceived>();
        String HRMfile = docDir + "HRM_" + UtilDateUtilities.getToday("yyyy-MM-dd.HH.mm.ss");
        for (int i = 0; i < repR.length; i++) {

            if (repR[i].getHRMResultStatus() != null || repR[i].getOBRContentArray().length > 0) { //HRM reports
                HRMDocument hrmDoc = new HRMDocument();
                HRMDocumentComment hrmDocComment = new HRMDocumentComment();
                HRMDocumentToDemographic hrmDocToDemo = new HRMDocumentToDemographic();

                hrmDoc.setReportFile(HRMfile);
                if (repR[i].getSourceFacility() != null)
                    hrmDoc.setSourceFacility(repR[i].getSourceFacility());
                if (repR[i].getReceivedDateTime() != null) {
                    hrmDoc.setTimeReceived(dateTimeFPtoDate(repR[i].getReceivedDateTime(), timeShiftInDays));
                } else {
                    hrmDoc.setTimeReceived(new Date());
                }
                if (repR[i].getHRMResultStatus() != null)
                    hrmDoc.setReportStatus(repR[i].getHRMResultStatus());
                if (repR[i].getClass1() != null)
                    hrmDoc.setReportType(repR[i].getClass1().toString());
                if (repR[i].getEventDateTime() != null)
                    hrmDoc.setReportDate(dateTimeFPtoDate(repR[i].getEventDateTime(), timeShiftInDays));
                hrmDocDao.persist(hrmDoc);

                if (repR[i].getNotes() != null) {
                    hrmDocComment.setHrmDocumentId(hrmDoc.getId());
                    hrmDocComment.setComment(repR[i].getNotes());
                    hrmDocCommentDao.persist(hrmDocComment);
                }

                hrmDocToDemo.setDemographicNo(demographicNo);
                hrmDocToDemo.setHrmDocumentId(hrmDoc.getId().toString());
                hrmDocToDemoDao.persist(hrmDocToDemo);

                ReportsReceived.OBRContent[] obr = repR[i].getOBRContentArray();
                for (int j = 0; j < obr.length; j++) {
                    HRMDocumentSubClass hrmDocSc = new HRMDocumentSubClass();
                    if (obr[j].getAccompanyingSubClass() != null)
                        hrmDocSc.setSubClass(obr[j].getAccompanyingSubClass());
                    if (obr[j].getAccompanyingDescription() != null)
                        hrmDocSc.setSubClassDescription(obr[j].getAccompanyingDescription());
                    if (obr[j].getAccompanyingMnemonic() != null)
                        hrmDocSc.setSubClassMnemonic(obr[j].getAccompanyingMnemonic());
                    if (obr[j].getObservationDateTime() != null)
                        hrmDocSc.setSubClassDateTime(
                                dateTimeFPtoDate(obr[j].getObservationDateTime(), timeShiftInDays));
                    hrmDocSc.setHrmDocumentId(hrmDoc.getId());
                    hrmDocSc.setActive(true);
                    hrmDocSubClassDao.persist(hrmDocSc);
                }
                HRMreports.add(repR[i]);

            } else { //non-HRM reports
                boolean binaryFormat = false;
                if (repR[i].getFormat() != null) {
                    if (repR[i].getFormat().equals(cdsDt.ReportFormat.BINARY))
                        binaryFormat = true;
                } else {
                    err_data.add("Error! No Report Format for Report (" + (i + 1) + ")");
                }
                cdsDt.ReportContent repCt = repR[i].getContent();
                if (repCt != null) {
                    byte[] b = null;
                    if (repCt.getMedia() != null)
                        b = repCt.getMedia();
                    else if (repCt.getTextContent() != null)
                        b = repCt.getTextContent().getBytes();
                    if (b == null) {
                        err_othe.add("Error! No report file in xml (" + (i + 1) + ")");
                    } else {
                        String docFileName = "ImportReport" + (i + 1) + "-"
                                + UtilDateUtilities.getToday("yyyy-MM-dd.HH.mm.ss");
                        String docClass = null, docSubClass = null, contentType = "", observationDate = null,
                                updateDateTime = null, docCreator = admProviderNo;
                        String reviewer = null, reviewDateTime = null, source = null, sourceFacility = null,
                                reportExtra = null;
                        Integer docNum = null;

                        if (StringUtils.filled(repR[i].getFileExtensionAndVersion())) {
                            contentType = repR[i].getFileExtensionAndVersion();
                            docFileName += Util.mimeToExt(contentType);
                        } else {
                            if (binaryFormat)
                                err_data.add("Error! No File Extension for Report (" + (i + 1) + ")");
                        }
                        String docDesc = repR[i].getSubClass();
                        if (StringUtils.empty(docDesc))
                            docDesc = "ImportReport" + (i + 1);
                        FileOutputStream f = new FileOutputStream(docDir + docFileName);
                        f.write(b);
                        f.close();

                        if (repR[i].getClass1() != null) {
                            docClass = repR[i].getClass1().toString();
                        } else {
                            err_data.add("Error! No Class Type for Report (" + (i + 1) + ")");
                        }
                        if (repR[i].getSubClass() != null) {
                            docSubClass = repR[i].getSubClass();
                        }

                        if (repR[i].getSourceFacility() != null) {
                            sourceFacility = repR[i].getSourceFacility();
                        }

                        if (repR[i].getMedia() != null) {
                            reportExtra = Util.addLine(reportExtra, "Media: ", repR[i].getMedia().toString());
                        }

                        ReportsReceived.SourceAuthorPhysician authorPhysician = repR[i]
                                .getSourceAuthorPhysician();
                        if (authorPhysician != null) {
                            if (authorPhysician.getAuthorName() != null) {
                                HashMap<String, String> author = getPersonName(authorPhysician.getAuthorName());
                                source = StringUtils.noNull(author.get("firstname")) + " "
                                        + StringUtils.noNull(author.get("lastname"));
                            } else if (authorPhysician.getAuthorFreeText() != null) {
                                source = authorPhysician.getAuthorFreeText();
                            }
                        }

                        ReportsReceived.ReportReviewed[] reportReviewed = repR[i].getReportReviewedArray();
                        if (reportReviewed.length > 0) {
                            HashMap<String, String> reviewerName = getPersonName(reportReviewed[0].getName());
                            reviewer = writeProviderData(reviewerName.get("firstname"),
                                    reviewerName.get("lastname"),
                                    reportReviewed[0].getReviewingOHIPPhysicianId());
                            reviewDateTime = dateFPtoString(reportReviewed[0].getDateTimeReportReviewed(),
                                    timeShiftInDays);
                        }

                        observationDate = dateFPtoString(repR[i].getEventDateTime(), timeShiftInDays);
                        updateDateTime = dateFPtoString(repR[i].getReceivedDateTime(), timeShiftInDays);

                        docNum = EDocUtil.addDocument(demographicNo, docFileName, docDesc, docClass, docClass,
                                docSubClass, contentType, observationDate, updateDateTime, docCreator,
                                admProviderNo, reviewer, reviewDateTime, source, sourceFacility);
                        if (docNum == null)
                            docNum = 0;
                        if (binaryFormat)
                            addOneEntry(REPORTBINARY);
                        else
                            addOneEntry(REPORTTEXT);

                        //to dumpsite: Extra report data
                        /*
                        if (org.apache.commons.lang.StringUtils.isBlank(reportExtra)) {
                           reportExtra = Util.addLine("imported.cms4.2011.06", reportExtra);
                           CaseManagementNote rpNote = prepareCMNote("2",null);
                           rpNote.setNote(reportExtra);
                           saveLinkNote(rpNote, CaseManagementNoteLink.DOCUMENT, Long.valueOf(docNum));
                        }*/
                    }
                }
            }
        }
        CreateHRMFile.create(demo, HRMreports, HRMfile);

        //CARE ELEMENTS
        CareElements[] careElems = patientRec.getCareElementsArray();
        for (int i = 0; i < careElems.length; i++) {
            CareElements ce = careElems[i];
            cdsDt.Height[] heights = ce.getHeightArray();
            for (cdsDt.Height ht : heights) {
                Date dateObserved = ht.getDate() != null ? ht.getDate().getTime() : null;
                String dataField = StringUtils.noNull(ht.getHeight());
                String dataUnit = ht.getHeightUnit() != null ? "in " + ht.getHeightUnit().toString() : "";

                if (ht.getDate() == null)
                    err_data.add("Error! No Date for Height in Care Element (" + (i + 1) + ")");
                if (StringUtils.empty(ht.getHeight()))
                    err_data.add("Error! No value for Height in Care Element (" + (i + 1) + ")");
                if (ht.getHeightUnit() == null)
                    err_data.add("Error! No unit for Height in Care Element (" + (i + 1) + ")");
                ImportExportMeasurements.saveMeasurements("HT", demographicNo, admProviderNo, dataField,
                        dataUnit, dateObserved);
                addOneEntry(CAREELEMENTS);
            }
            cdsDt.Weight[] weights = ce.getWeightArray();
            for (cdsDt.Weight wt : weights) {
                Date dateObserved = wt.getDate() != null ? wt.getDate().getTime() : null;
                String dataField = StringUtils.noNull(wt.getWeight());
                String dataUnit = wt.getWeightUnit() != null ? "in " + wt.getWeightUnit().toString() : "";

                if (wt.getDate() == null)
                    err_data.add("Error! No Date for Weight in Care Element (" + (i + 1) + ")");
                if (StringUtils.empty(wt.getWeight()))
                    err_data.add("Error! No value for Weight in Care Element (" + (i + 1) + ")");
                if (wt.getWeightUnit() == null)
                    err_data.add("Error! No unit for Weight in Care Element (" + (i + 1) + ")");
                ImportExportMeasurements.saveMeasurements("WT", demographicNo, admProviderNo, dataField,
                        dataUnit, dateObserved);
                addOneEntry(CAREELEMENTS);
            }
            cdsDt.WaistCircumference[] waists = ce.getWaistCircumferenceArray();
            for (cdsDt.WaistCircumference wc : waists) {
                Date dateObserved = wc.getDate() != null ? wc.getDate().getTime() : null;
                String dataField = StringUtils.noNull(wc.getWaistCircumference());
                String dataUnit = wc.getWaistCircumferenceUnit() != null
                        ? "in " + wc.getWaistCircumferenceUnit().toString()
                        : "";

                if (wc.getDate() == null)
                    err_data.add("Error! No Date for Waist Circumference in Care Element (" + (i + 1) + ")");
                if (StringUtils.empty(wc.getWaistCircumference()))
                    err_data.add("Error! No value for Waist Circumference in Care Element (" + (i + 1) + ")");
                if (wc.getWaistCircumferenceUnit() == null)
                    err_data.add("Error! No unit for Waist Circumference in Care Element (" + (i + 1) + ")");
                ImportExportMeasurements.saveMeasurements("WC", demographicNo, admProviderNo, dataField,
                        dataUnit, dateObserved);
                addOneEntry(CAREELEMENTS);
            }
            cdsDt.BloodPressure[] bloodp = ce.getBloodPressureArray();
            for (cdsDt.BloodPressure bp : bloodp) {
                Date dateObserved = bp.getDate() != null ? bp.getDate().getTime() : null;
                String dataField = StringUtils.noNull(bp.getSystolicBP()) + "/"
                        + StringUtils.noNull(bp.getDiastolicBP());
                String dataUnit = bp.getBPUnit() != null ? "in " + bp.getBPUnit().toString() : "";

                if (bp.getDate() == null)
                    err_data.add("Error! No Date for Blood Pressure in Care Element (" + (i + 1) + ")");
                if (StringUtils.empty(bp.getSystolicBP()))
                    err_data.add(
                            "Error! No value for Systolic Blood Pressure in Care Element (" + (i + 1) + ")");
                if (StringUtils.empty(bp.getDiastolicBP()))
                    err_data.add(
                            "Error! No value for Diastolic Blood Pressure in Care Element (" + (i + 1) + ")");
                if (bp.getBPUnit() == null)
                    err_data.add("Error! No unit for Blood Pressure in Care Element (" + (i + 1) + ")");
                ImportExportMeasurements.saveMeasurements("BP", demographicNo, admProviderNo, dataField,
                        dataUnit, dateObserved);
                addOneEntry(CAREELEMENTS);
            }
            cdsDt.SmokingStatus[] smoks = ce.getSmokingStatusArray();
            for (cdsDt.SmokingStatus ss : smoks) {
                Date dateObserved = ss.getDate() != null ? ss.getDate().getTime() : null;
                String dataField = ss.getStatus() != null ? ss.getStatus().toString() : "";

                if (ss.getDate() == null)
                    err_data.add("Error! No Date for Smoking Status in Care Element (" + (i + 1) + ")");
                if (ss.getStatus() == null)
                    err_data.add("Error! No value for Smoking Status in Care Element (" + (i + 1) + ")");
                ImportExportMeasurements.saveMeasurements("SKST", demographicNo, admProviderNo, dataField,
                        dateObserved);
                addOneEntry(CAREELEMENTS);
            }
            cdsDt.SmokingPacks[] smokp = ce.getSmokingPacksArray();
            for (cdsDt.SmokingPacks sp : smokp) {
                Date dateObserved = sp.getDate() != null ? sp.getDate().getTime() : null;
                String dataField = sp.getPerDay() != null ? sp.getPerDay().toString() : "";

                if (sp.getDate() == null)
                    err_data.add("Error! No Date for Smoking Packs/Day in Care Element (" + (i + 1) + ")");
                if (sp.getPerDay() == null)
                    err_data.add("Error! No value for Smoking Packs/Day in Care Element (" + (i + 1) + ")");
                ImportExportMeasurements.saveMeasurements("POSK", demographicNo, admProviderNo, dataField,
                        dateObserved);
                addOneEntry(CAREELEMENTS);
            }
            cdsDt.SelfMonitoringBloodGlucose[] smbg = ce.getSelfMonitoringBloodGlucoseArray();
            for (cdsDt.SelfMonitoringBloodGlucose sg : smbg) {
                Date dateObserved = sg.getDate() != null ? sg.getDate().getTime() : null;
                String dataField = sg.getSelfMonitoring() != null ? sg.getSelfMonitoring().toString() : "";

                if (sg.getDate() == null)
                    err_data.add("Error! No Date for Self-monitoring Blood Glucose in Care Element (" + (i + 1)
                            + ")");
                if (sg.getSelfMonitoring() == null)
                    err_data.add("Error! No value for Self-monitoring Blood Glucose in Care Element (" + (i + 1)
                            + ")");
                ImportExportMeasurements.saveMeasurements("SMBG", demographicNo, admProviderNo, dataField,
                        dateObserved);
                addOneEntry(CAREELEMENTS);
            }
            cdsDt.DiabetesEducationalSelfManagement[] desm = ce.getDiabetesEducationalSelfManagementArray();
            for (cdsDt.DiabetesEducationalSelfManagement dm : desm) {
                Date dateObserved = dm.getDate() != null ? dm.getDate().getTime() : null;
                String dataField = dm.getEducationalTrainingPerformed() != null
                        ? dm.getEducationalTrainingPerformed().toString()
                        : null;

                if (dm.getDate() == null)
                    err_data.add(
                            "Error! No Date for Diabetes Educational/Self-management Training in Care Element ("
                                    + (i + 1) + ")");
                if (dm.getEducationalTrainingPerformed() == null)
                    err_data.add(
                            "Error! No value for Diabetes Educational/Self-management Training in Care Element ("
                                    + (i + 1) + ")");
                ImportExportMeasurements.saveMeasurements("DMME", demographicNo, admProviderNo, dataField,
                        dateObserved);
                addOneEntry(CAREELEMENTS);
            }
            cdsDt.DiabetesSelfManagementChallenges[] dsmc = ce.getDiabetesSelfManagementChallengesArray();
            for (cdsDt.DiabetesSelfManagementChallenges dc : dsmc) {
                Date dateObserved = dc.getDate().getTime();
                String dataField = dc.getChallengesIdentified().toString();
                String dataCode = dc.getCodeValue() != null ? "LOINC=" + dc.getCodeValue().toString() : "";

                if (dc.getDate() == null)
                    err_data.add("Error! No Date for Diabetes Self-management Challenges in Care Element ("
                            + (i + 1) + ")");
                if (dc.getChallengesIdentified() == null)
                    err_data.add(
                            "Error! No Challenges Identified for Diabetes Self-management Challenges in Care Element ("
                                    + (i + 1) + ")");
                if (dc.getCodeValue() == null)
                    err_data.add(
                            "Error! No Code value for Diabetes Self-management Challenges in Care Element ("
                                    + (i + 1) + ")");
                ImportExportMeasurements.saveMeasurements("SMCD", demographicNo, admProviderNo, dataField,
                        dataCode, dateObserved);
                addOneEntry(CAREELEMENTS);
            }
            cdsDt.DiabetesMotivationalCounselling[] dmc = ce.getDiabetesMotivationalCounsellingArray();
            for (cdsDt.DiabetesMotivationalCounselling dc : dmc) {
                Date dateObserved = dc.getDate() != null ? dc.getDate().getTime() : null;
                String dataField = "Yes";
                if (dc.getCounsellingPerformed() != null) {
                    if (dc.getCounsellingPerformed().equals(CounsellingPerformed.NUTRITION)) {
                        ImportExportMeasurements.saveMeasurements("MCCN", demographicNo, admProviderNo,
                                dataField, dateObserved);
                        addOneEntry(CAREELEMENTS);
                    } else if (dc.getCounsellingPerformed().equals(CounsellingPerformed.EXERCISE)) {
                        ImportExportMeasurements.saveMeasurements("MCCE", demographicNo, admProviderNo,
                                dataField, dateObserved);
                        addOneEntry(CAREELEMENTS);
                    } else if (dc.getCounsellingPerformed().equals(CounsellingPerformed.SMOKING_CESSATION)) {
                        ImportExportMeasurements.saveMeasurements("MCCS", demographicNo, admProviderNo,
                                dataField, dateObserved);
                        addOneEntry(CAREELEMENTS);
                    } else if (dc.getCounsellingPerformed().equals(CounsellingPerformed.OTHER)) {
                        ImportExportMeasurements.saveMeasurements("MCCO", demographicNo, admProviderNo,
                                dataField, dateObserved);
                        addOneEntry(CAREELEMENTS);
                    }
                }
                if (dc.getDate() == null)
                    err_data.add("Error! No Date for Diabetes Motivational Counselling in Care Element ("
                            + (i + 1) + ")");
                if (dc.getCounsellingPerformed() == null)
                    err_data.add(
                            "Error! No Diabetes Motivational Counselling Performed value for Diabetes Self-management Challenges in Care Element ("
                                    + (i + 1) + ")");
            }
            cdsDt.DiabetesComplicationScreening[] dcs = ce.getDiabetesComplicationsScreeningArray();
            for (cdsDt.DiabetesComplicationScreening ds : dcs) {
                Date dateObserved = ds.getDate() != null ? ds.getDate().getTime() : null;
                String dataField = "Yes";
                if (ds.getExamCode() != null) {
                    if (ds.getExamCode().equals(ExamCode.X_32468_1)) {
                        ImportExportMeasurements.saveMeasurements("EYEE", demographicNo, admProviderNo,
                                dataField, dateObserved);
                        addOneEntry(CAREELEMENTS);
                    } else if (ds.getExamCode().equals(ExamCode.X_11397_7)) {
                        ImportExportMeasurements.saveMeasurements("FTE", demographicNo, admProviderNo,
                                dataField, dateObserved);
                        addOneEntry(CAREELEMENTS);
                    } else if (ds.getExamCode().equals(ExamCode.NEUROLOGICAL_EXAM)) {
                        ImportExportMeasurements.saveMeasurements("FTLS", demographicNo, admProviderNo,
                                dataField, dateObserved);
                        addOneEntry(CAREELEMENTS);
                    }
                }
                if (ds.getDate() == null)
                    err_data.add("Error! No Date for Diabetes Complications Screening in Care Element ("
                            + (i + 1) + ")");
                if (ds.getExamCode() == null)
                    err_data.add("Error! No Exam Code for Diabetes Complications Screening in Care Element ("
                            + (i + 1) + ")");
            }
            cdsDt.DiabetesSelfManagementCollaborative[] dsco = ce.getDiabetesSelfManagementCollaborativeArray();
            for (cdsDt.DiabetesSelfManagementCollaborative dc : dsco) {
                Date dateObserved = dc.getDate() != null ? dc.getDate().getTime() : null;
                String dataField = StringUtils.noNull(dc.getDocumentedGoals());
                if (dc.getDate() == null)
                    err_data.add(
                            "Error! No Date for Diabetes Self-management/Collaborative Goal Setting in Care Element ("
                                    + (i + 1) + ")");
                if (StringUtils.empty(dc.getDocumentedGoals()))
                    err_data.add(
                            "Error! No Documented Goal for Diabetes Self-management/Collaborative Goal Setting in Care Element ("
                                    + (i + 1) + ")");
                if (dc.getCodeValue() == null)
                    err_data.add(
                            "Error! No Code Value for Diabetes Self-management/Collaborative Goal Setting in Care Element ("
                                    + (i + 1) + ")");
                ImportExportMeasurements.saveMeasurements("CGSD", demographicNo, admProviderNo, dataField,
                        dateObserved);
                addOneEntry(CAREELEMENTS);
            }
            cdsDt.HypoglycemicEpisodes[] hype = ce.getHypoglycemicEpisodesArray();
            for (cdsDt.HypoglycemicEpisodes he : hype) {
                Date dateObserved = he.getDate() != null ? he.getDate().getTime() : null;
                String dataField = he.getNumOfReportedEpisodes() != null
                        ? he.getNumOfReportedEpisodes().toString()
                        : "";

                if (he.getDate() == null)
                    err_data.add("Error! No Date for Hypoglycemic Episodes in Care Element (" + (i + 1) + ")");
                if (he.getNumOfReportedEpisodes() == null)
                    err_data.add("Error! No Reported Episodes value for Hypoglycemic Episodes in Care Element ("
                            + (i + 1) + ")");
                ImportExportMeasurements.saveMeasurements("HYPE", demographicNo, admProviderNo, dataField,
                        dateObserved);
                addOneEntry(CAREELEMENTS);
            }
        }
    }
    if (demoRes != null) {
        err_demo.addAll(demoRes.getWarningsCollection());
    }
    Util.cleanFile(xmlFile);

    return packMsgs(err_demo, err_data, err_summ, err_othe, err_note, warnings);
}

From source file:lu.fisch.unimozer.Diagram.java

private void printHeaderFooter(Graphics g, PageFormat pageFormat, int page, String className) {
    int origPage = page + 1;

    // Add header
    g.setColor(Color.BLACK);//from w  w w .  j  a  v  a2s .  co m
    int xOffset = (int) pageFormat.getImageableX();
    int topOffset = (int) pageFormat.getImageableY() + 20;
    int bottom = (int) (pageFormat.getImageableY() + pageFormat.getImageableHeight());
    // header line
    g.drawLine(xOffset, topOffset - 8, xOffset + (int) pageFormat.getImageableWidth(), topOffset - 8);
    // footer line
    g.drawLine(xOffset, bottom - 11, xOffset + (int) pageFormat.getImageableWidth(), bottom - 11);
    g.setFont(new Font(Font.SANS_SERIF, Font.ITALIC, 10));

    Graphics2D gg = (Graphics2D) g;
    String pageString = "Page " + origPage;
    int tw = (int) gg.getFont().getStringBounds(pageString, gg.getFontRenderContext()).getWidth();
    // footer text
    g.drawString(pageString, xOffset + (int) pageFormat.getImageableWidth() - tw, bottom - 2);

    //System.err.println("Printing: "+directoryName);
    if (directoryName != null) {
        g.setFont(new Font(g.getFont().getFontName(), Font.ITALIC, 10));
        String filename = directoryName;
        if (!className.equals(""))
            filename += System.getProperty("file.separator") + className + ".java";
        // header text
        g.drawString(filename, xOffset, bottom - 2);
        File f = new File(filename);
        //System.err.println("Printing: "+filename);
        if (f.exists()) {
            DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            java.util.Date date = new java.util.Date();
            date.setTime(f.lastModified());
            String myDate = dateFormat.format(date);
            int w = (int) gg.getFont().getStringBounds(myDate, gg.getFontRenderContext()).getWidth();
            // header text
            g.drawString("File last modified on " + myDate, xOffset, topOffset - 10);
        }
    }
}

From source file:org.apache.lens.server.metastore.TestMetastoreService.java

@Test(dataProvider = "mediaTypeData")
public void testCreateFactTableWithMultipleUpdatePeriods(MediaType mediaType) throws Exception {

    final String table = "testCreateFactTableWithMultipleUpdatePeriods";
    String prevDb = getCurrentDatabase(mediaType);
    final String DB = dbPFX + "testCreateFactTableWithMultipleUpdatePeriods_DB" + mediaType.getSubtype();
    createDatabase(DB, mediaType);/*w  w w .j  a v a  2s.  c o m*/
    setCurrentDatabase(DB, mediaType);
    createStorage("S1", mediaType);
    try {
        final XCube cube = createTestCube("testCube");
        target().path("metastore").path("cubes").queryParam("sessionid", lensSessionId).request(mediaType)
                .post(Entity.entity(new GenericEntity<JAXBElement<XCube>>(cubeObjectFactory.createXCube(cube)) {
                }, mediaType), APIResult.class);
        XFactTable f = createFactTable(table);
        String[] tables = new String[] { "testTable1", "testTable2", "testTable3" };
        String[] updatePeriods = new String[] { "HOURLY", "DAILY", "MONTHLY" };
        f.getStorageTables().getStorageTable()
                .add(createStorageTblWithMultipleTableDescriptors("S1", tables, updatePeriods));
        APIResult result = target().path("metastore").path("facts").queryParam("sessionid", lensSessionId)
                .request(mediaType)
                .post(Entity.entity(new GenericEntity<JAXBElement<XFact>>(cubeObjectFactory.createXFact(f)) {
                }, mediaType), APIResult.class);
        assertSuccess(result);

        StringList factNames = target().path("metastore/facts").queryParam("sessionid", lensSessionId)
                .request(mediaType).get(StringList.class);
        assertTrue(factNames.getElements().contains(table.toLowerCase()));

        // Get the created tables
        JAXBElement<XFact> gotFactElement = target().path("metastore/facts").path(table)
                .queryParam("sessionid", lensSessionId).request(mediaType)
                .get(new GenericType<JAXBElement<XFact>>() {
                });
        XFactTable gotFact = (XFactTable) gotFactElement.getValue();
        assertTrue(gotFact.getName().equalsIgnoreCase(table));
        assertEquals(gotFact.getWeight(), 10.0);

        // Check for the created tables per update period.
        List<XUpdatePeriodTableDescriptor> updatePeriodTableDescriptor = gotFact.getStorageTables()
                .getStorageTable().get(0).getUpdatePeriods().getUpdatePeriodTableDescriptor();
        assertEquals(updatePeriodTableDescriptor.size(), 3);

        CubeFactTable cf = JAXBUtils.cubeFactFromFactTable(gotFact);

        Map<UpdatePeriod, String> updatePeriodTablePrefixMap = cf.getStoragePrefixUpdatePeriodMap().get("S1");
        for (Map.Entry entry : updatePeriodTablePrefixMap.entrySet()) {
            assertEquals(entry.getValue(), entry.getKey() + "_S1");
        }
        // Do some changes to test update
        cf.alterWeight(20.0);
        cf.alterColumn(new FieldSchema("c2", "double", "changed to double"));

        XFactTable update = JAXBUtils.factTableFromCubeFactTable(cf);
        XStorageTableElement s1Tbl = createStorageTblWithMultipleTableDescriptors("S1",
                new String[] { tables[0], tables[1] }, new String[] { updatePeriods[0], updatePeriods[1] });
        update.getStorageTables().getStorageTable().add(s1Tbl);
        // Update
        result = target().path("metastore").path("facts").path(table).queryParam("sessionid", lensSessionId)
                .request(mediaType).put(Entity
                        .entity(new GenericEntity<JAXBElement<XFact>>(cubeObjectFactory.createXFact(update)) {
                        }, mediaType), APIResult.class);
        assertSuccess(result);

        // Get the updated table
        gotFactElement = target().path("metastore/facts").path(table).queryParam("sessionid", lensSessionId)
                .request(mediaType).get(new GenericType<JAXBElement<XFact>>() {
                });
        gotFact = (XFactTable) gotFactElement.getValue();
        CubeFactTable ucf = JAXBUtils.cubeFactFromFactTable(gotFact);
        assertEquals(ucf.weight(), 20.0);
        assertTrue(ucf.getUpdatePeriods().get("S1").contains(HOURLY));
        assertTrue(ucf.getUpdatePeriods().get("S1").contains(DAILY));
        assertFalse(ucf.getUpdatePeriods().get("S1").contains(MONTHLY));

        // Add partitions
        final Date partDate = new Date();
        XPartition xp = createPartition(table, partDate);
        APIResult partAddResult = target().path("metastore/facts/").path(table).path("storages/S1/partition")
                .queryParam("sessionid", lensSessionId).request(mediaType).post(Entity.entity(
                        new GenericEntity<JAXBElement<XPartition>>(cubeObjectFactory.createXPartition(xp)) {
                        }, mediaType), APIResult.class);
        assertSuccess(partAddResult);

        // add same should fail
        partAddResult = target().path("metastore/facts/").path(table).path("storages/S1/partition")
                .queryParam("sessionid", lensSessionId).request(mediaType).post(Entity.entity(
                        new GenericEntity<JAXBElement<XPartition>>(cubeObjectFactory.createXPartition(xp)) {
                        }, mediaType), APIResult.class);
        assertEquals(partAddResult.getStatus(), Status.FAILED);

        xp.setLocation(xp.getLocation() + "/a/b/c");
        APIResult partUpdateResult = target().path("metastore/facts/").path(table).path("storages/S1/partition")
                .queryParam("sessionid", lensSessionId).request(mediaType).put(Entity.entity(
                        new GenericEntity<JAXBElement<XPartition>>(cubeObjectFactory.createXPartition(xp)) {
                        }, mediaType), APIResult.class);
        assertSuccess(partUpdateResult);

        JAXBElement<XPartitionList> partitionsElement = target().path("metastore/facts").path(table)
                .path("storages/S1/partitions").queryParam("sessionid", lensSessionId)
                .queryParam("filter", "dt='" + HOURLY.format(partDate) + "'").request(mediaType)
                .get(new GenericType<JAXBElement<XPartitionList>>() {
                });

        //Getting partitions without filter will throw an error.
        Response res = target().path("metastore/facts").path(table).path("storages/S1/partitions")
                .queryParam("sessionid", lensSessionId).request(mediaType).get();
        assertEquals(res.getStatus(), 400);
        assertEquals(res.readEntity(String.class), "Partition filter can not be null or empty");

        XPartitionList partitions = partitionsElement.getValue();
        assertNotNull(partitions);
        assertEquals(partitions.getPartition().size(), 1);
        XPartition readPartition = partitions.getPartition().get(0);
        assertEquals(readPartition.getLocation(), xp.getLocation());
        assertEquals(readPartition.getTimePartitionSpec(), xp.getTimePartitionSpec());
        assertEquals(readPartition.getNonTimePartitionSpec(), xp.getNonTimePartitionSpec());
        assertNotNull(readPartition.getFullPartitionSpec());
        XTimePartSpecElement timePartSpec = readPartition.getTimePartitionSpec().getPartSpecElement().iterator()
                .next();
        XPartSpecElement fullPartSpec = readPartition.getFullPartitionSpec().getPartSpecElement().iterator()
                .next();
        assertEquals(timePartSpec.getKey(), fullPartSpec.getKey());
        assertEquals(UpdatePeriod.valueOf(xp.getUpdatePeriod().name())
                .format(JAXBUtils.getDateFromXML(timePartSpec.getValue())), fullPartSpec.getValue());
        DateTime date = target().path("metastore/cubes").path("testCube").path("latestdate")
                .queryParam("timeDimension", "dt").queryParam("sessionid", lensSessionId).request(mediaType)
                .get(DateTime.class);

        partDate.setMinutes(0);
        partDate.setSeconds(0);
        partDate.setTime(partDate.getTime() - partDate.getTime() % 1000);
        assertEquals(date.getDate(), partDate);
        // add two partitions, one of them already added. result should be partial
        XPartitionList parts = new XPartitionList();
        parts.getPartition().add(xp);
        parts.getPartition().add(createPartition(table, DateUtils.addHours(partDate, 1)));
        partAddResult = target().path("metastore/facts/").path(table).path("storages/S1/partitions")
                .queryParam("sessionid", lensSessionId).request(mediaType)
                .post(Entity.entity(new GenericEntity<JAXBElement<XPartitionList>>(
                        cubeObjectFactory.createXPartitionList(parts)) {
                }, mediaType), APIResult.class);
        assertEquals(partAddResult.getStatus(), Status.PARTIAL);

        // Drop the partitions
        APIResult dropResult = target().path("metastore/facts").path(table).path("storages/S1/partitions")
                .queryParam("sessionid", lensSessionId).request(mediaType).delete(APIResult.class);

        assertSuccess(dropResult);

        // Verify partition was dropped
        partitionsElement = target().path("metastore/facts").path(table).path("storages/S1/partitions")
                .queryParam("sessionid", lensSessionId)
                .queryParam("filter", "dt='" + HOURLY.format(partDate) + "'").request(mediaType)
                .get(new GenericType<JAXBElement<XPartitionList>>() {
                });

        partitions = partitionsElement.getValue();
        assertNotNull(partitions);
        assertEquals(partitions.getPartition().size(), 0);
        // add null in batch
        Response resp = target().path("metastore/facts/").path(table).path("storages/S1/partitions")
                .queryParam("sessionid", lensSessionId).request(mediaType).post(null);
        Assert.assertEquals(resp.getStatus(), 400);

        // Drop the cube
        WebTarget target = target().path("metastore").path("cubes").path("testCube");
        result = target.queryParam("sessionid", lensSessionId).request(mediaType).delete(APIResult.class);
        assertSuccess(result);
    } finally {
        setCurrentDatabase(prevDb, mediaType);
        dropDatabase(DB, mediaType);
    }
}

From source file:ca.inverse.sogo.engine.source.SOGoUtilities.java

/**
 * This method is used to convert a vCalendar object from v2 to v1.
 * We might lose some information by doing so but we try our best 
 * to not to. Here are the required transformations.
 * //from ww  w . ja v a 2s . c  o m
 * 1- removal of all VTIMEZONE information + UTC offset adjustments
 * 2- start/end (or due) date adjustments if the connecting device
 *    doesn't support UTC
 * 3- if we get TZID in DTSTART/DTEND, convert DTSTART/DTEND to UTC
 *    if our devices supports it, otherwise, leave it as is
 *    
 *    ---------------------------------------------------------------------------
 *    Devices supports UTC   | Input            | TZ to use | Output
 *    ----------------------|-------------------|-----------|--------------------
 *    YES               | UTC            | none      | UTC
 *    YES               | non-UTC         | pref. TZ   | UTC
 *    YES               | non-UTC + TZID   | TZID      | UTC
 *    NO               | UTC            | pref. TZ   | non-UTC
 *    NO               | non-UTC         | none      | non-UTC
 *    NO               | non-UTC + TZID   | none      | non-UTC
 * 
 * @param bytes
 * @return
 */
public static byte[] vCalendarV2toV1(byte[] bytes, String tag, String key, boolean secure, int classification,
        SOGoSyncSource source, SyncContext context, FunambolLogger log) {
    String s;

    log.info("About to convert vCalendar (from v2 to v1): " + new String(bytes));
    s = "";

    try {
        VCalendarConverter converter;
        VComponentWriter writer;
        VCalendar v2, calendar;
        CalendarContent cc;
        ICalendarParser p;
        Calendar cal;

        // We need to proceed with the following steps
        // iCalendar -> VCalendar object -> Calendar object -> VCalendar v1.0 object -> string
        p = new ICalendarParser(
                new ByteArrayInputStream(SOGoSanitizer.sanitizevCalendarInput(bytes, key, log).getBytes()));
        v2 = p.ICalendar();

        converter = new VCalendarConverter(getUserTimeZone(source, context, log), source.getDeviceCharset(),
                false);
        cal = converter.vcalendar2calendar(v2);

        // HACK: Funambol (at least, v6.5-7.1) crashes in VCalendarContentConverter: cc2vcc
        //       since it is trying to get this property's value while it might be null.
        SOGoSanitizer.sanitizeFunambolCalendar(cal);
        cc = cal.getCalendarContent();

        if (tag != null) {
            String summary;

            tag = "[" + tag + "] ";
            summary = cc.getSummary().getPropertyValueAsString();
            if (!summary.startsWith(tag)) {
                summary = tag + summary;
                cc.setSummary(new Property(summary));
            }

            if (secure) {
                secureCalendarContent(cc, tag, classification);
            }
        }

        // The Funambol client for BlackBerry devices is a real piece of crap.
        // It does NOT honor the X-FUNAMBOL-ALLDAY tag so we must do some magic
        // here to make it believe it's an all-day event. Otherwise, the all-day
        // event will span two days on the BlackBerry device.
        if (isBlackBerry(context) && cc.isAllDay()) {
            SimpleDateFormat formatter;
            java.util.Date d;
            String ss;

            // We can either parse 20120828 or 2012-08-28
            ss = cc.getDtStart().getPropertyValueAsString();
            d = null;

            try {
                formatter = new SimpleDateFormat("yyyy-MM-dd");
                d = formatter.parse(ss);
            } catch (Exception pe) {
            }

            if (d == null) {
                try {
                    formatter = new SimpleDateFormat("yyyyMMdd");
                    d = formatter.parse(ss);
                } catch (Exception pe) {
                }
            }

            if (d != null) {
                formatter = new SimpleDateFormat("yyyyMMdd");
                ss = formatter.format(d) + "T000000";
                cc.getDtStart().setPropertyValue(ss);

                ss = formatter.format(d) + "T235900";
                cc.getDtEnd().setPropertyValue(ss);

            }
        }

        // The boolean parameter triggers v1 vs. v2 conversion (v1 == true)
        calendar = converter.calendar2vcalendar(cal, true);
        copyCustomProperties(v2.getVCalendarContent(), calendar.getVCalendarContent());

        // Funambol loses alarm settings when converting to a Calendar object, see
        // http://forge.ow2.org/tracker/index.php?func=detail&aid=314860&group_id=96&atid=100096
        // Try to convert any valarm from v2 to aalarm.
        // This is implementation will probably fix 95% of the cases, but doesn't cover
        // multiple alarms or alarm times set relative to the end of the event
        // Also note that at least Symbian devices does not seem to support relative alarms
        VAlarm valarm = (VAlarm) v2.getVCalendarContent().getComponent("VALARM");
        log.info("VALARM is: " + valarm);
        if (valarm != null) {
            com.funambol.common.pim.model.Property trigger;

            // TRIGGER;VALUE=DURATION:-PT15M
            trigger = valarm.getProperty("TRIGGER");

            log.info("TRIGGER: " + trigger.getValue()); // -PT15

            if (trigger.getParameter("VALUE") != null) {
                String duration;

                duration = trigger.getParameter("VALUE").value;

                if (duration.equalsIgnoreCase("DURATION")) {
                    String value;

                    boolean negate;
                    int len, i, v;
                    char c;

                    value = trigger.getValue(); //  -PT15
                    log.info("VALUE to parse: " + value);
                    len = value.length();
                    negate = false;

                    // v is always in seconds
                    v = 0;

                    // We parse :
                    //
                    // dur-value  = (["+"] / "-") "P" (dur-date / dur-time / dur-week)
                    //
                    // dur-date   = dur-day [dur-time]
                    // dur-time   = "T" (dur-hour / dur-minute / dur-second)
                    // dur-week   = 1*DIGIT "W"
                    // dur-hour   = 1*DIGIT "H" [dur-minute]
                    // dur-minute = 1*DIGIT "M" [dur-second]
                    // dur-second = 1*DIGIT "S"
                    // dur-day    = 1*DIGIT "D"
                    //
                    for (i = 0; i < len; i++) {
                        c = value.charAt(i);

                        if (c == 'P' || c == 'p' || c == 'T' || c == 't') {
                            log.info("Skipping " + c);
                            continue;
                        }

                        if (c == '-') {
                            negate = true;
                            log.info("Negating...");
                            continue;
                        }

                        if (Character.isDigit(c)) {
                            int j, x;

                            log.info("Digit found at " + i);

                            for (j = i; j < len; j++) {
                                c = value.charAt(j);
                                if (!Character.isDigit(c))
                                    break;
                            }

                            log.info("End digit found at " + i);

                            x = Integer.parseInt(value.substring(i, j));
                            log.info("x = " + x);

                            // Char at j is either W, H, M, S or D
                            switch (value.charAt(j)) {
                            case 'W':
                                v += x * (7 * 24 * 3600);
                                break;
                            case 'H':
                                v += x * 3600;
                                break;
                            case 'M':
                                v += x * 60;
                                break;
                            case 'S':
                                v += x;
                                break;
                            case 'D':
                            default:
                                v += x * (24 * 3600);
                                break;

                            }
                            log.info("v1 = " + v);

                            i = j + 1;
                        }
                    } // for (...)

                    log.info("v2 = " + v);

                    if (negate)
                        v = -v;

                    log.info("v3 = " + v);

                    // Let's add this to our start time.. we'll support end time later
                    if (v != 0) {
                        SimpleDateFormat dateFormatter;
                        java.util.Date d;

                        log.info("DTSTART: " + cc.getDtStart().getPropertyValue());
                        log.info("v = " + v);

                        try {
                            dateFormatter = new SimpleDateFormat(TimeUtils.PATTERN_UTC);
                            d = dateFormatter.parse(cc.getDtStart().getPropertyValueAsString());
                            d.setTime(d.getTime() + v * 1000);

                            calendar.getVCalendarContent().addProperty("AALARM", dateFormatter.format(d)); // DURATION
                        } catch (Exception e) {
                            log.error("Exception occured in vCalendarV2toV1(): " + e.toString(), e);
                        }
                    }
                }

            }
        }

        // Write result
        writer = new VComponentWriter(VComponentWriter.NO_FOLDING);
        s = writer.toString(calendar);
        log.info(s);
    } catch (Exception e) {
        log.error("Exception occured in vCalendarV2toV1(): " + e.toString(), e);
        log.info("===== item content (" + key + ") =====");
        log.info(new String(bytes));
        log.info("======================================");
    }

    return s.getBytes();
}

From source file:com.xpn.xwiki.doc.XWikiDocument.java

public void setDate(Date date) {
    if ((date != null) && (!date.equals(this.updateDate))) {
        setMetaDataDirty(true);/*from w  w  w . j  av  a2  s. c  o  m*/
    }
    // Make sure we drop milliseconds for consistency with the database
    if (date != null) {
        date.setTime((date.getTime() / 1000) * 1000);
    }
    this.updateDate = date;
}

From source file:com.xpn.xwiki.doc.XWikiDocument.java

public void setCreationDate(Date date) {
    if ((date != null) && (!date.equals(this.creationDate))) {
        setMetaDataDirty(true);//  w w w.j  a va  2 s .  c  o m
    }

    // Make sure we drop milliseconds for consistency with the database
    if (date != null) {
        date.setTime((date.getTime() / 1000) * 1000);
    }
    this.creationDate = date;
}

From source file:com.xpn.xwiki.doc.XWikiDocument.java

public void setContentUpdateDate(Date date) {
    if ((date != null) && (!date.equals(this.contentUpdateDate))) {
        setMetaDataDirty(true);/*  w  ww.  jav a  2 s  .  c o  m*/
    }

    // Make sure we drop milliseconds for consistency with the database
    if (date != null) {
        date.setTime((date.getTime() / 1000) * 1000);
    }
    this.contentUpdateDate = date;
}

From source file:org.apache.lens.server.metastore.TestMetastoreService.java

@SuppressWarnings("deprecation")
@Test(dataProvider = "mediaTypeData")
public void testFactStoragePartitions(MediaType mediaType) throws Exception {
    final String table = "testFactStoragePartitions";
    final String DB = dbPFX + "testFactStoragePartitions_DB" + mediaType.getSubtype();
    String prevDb = getCurrentDatabase(mediaType);
    createDatabase(DB, mediaType);//w w w  .j av a 2 s .c  om
    setCurrentDatabase(DB, mediaType);
    createStorage("S1", mediaType);
    createStorage("S2", mediaType);

    try {

        final XCube cube = createTestCube("testCube");
        target().path("metastore").path("cubes").queryParam("sessionid", lensSessionId).request(mediaType)
                .post(Entity.entity(new GenericEntity<JAXBElement<XCube>>(cubeObjectFactory.createXCube(cube)) {
                }, mediaType), APIResult.class);

        XFactTable f = createFactTable(table);
        f.getStorageTables().getStorageTable().add(createStorageTblElement("S1", table, "HOURLY"));
        f.getStorageTables().getStorageTable().add(createStorageTblElement("S2", table, "DAILY"));
        f.getStorageTables().getStorageTable().add(createStorageTblElement("S2", table, "HOURLY"));
        APIResult result = target().path("metastore").path("facts").queryParam("sessionid", lensSessionId)
                .request(mediaType)
                .post(Entity.entity(new GenericEntity<JAXBElement<XFact>>(cubeObjectFactory.createXFact(f)) {
                }, mediaType), APIResult.class);
        assertSuccess(result);

        APIResult partAddResult;
        // Add null partition
        Response resp = target().path("metastore/facts/").path(table).path("storages/S2/partition")
                .queryParam("sessionid", lensSessionId).request(mediaType).post(null);
        Assert.assertEquals(resp.getStatus(), 400);

        // Add wrong partition
        final Date partDate = new Date();
        XPartition xp2 = createPartition(table, partDate);
        xp2.getTimePartitionSpec().getPartSpecElement()
                .add(createTimePartSpecElement(partDate, "non_existant_time_part"));
        Response response = target().path("metastore/facts/").path(table).path("storages/S2/partition")
                .queryParam("sessionid", lensSessionId).request(mediaType).post(Entity.entity(
                        new GenericEntity<JAXBElement<XPartition>>(cubeObjectFactory.createXPartition(xp2)) {
                        }, mediaType));
        assertEquals(response.getStatus(), 400);
        partAddResult = response.readEntity(APIResult.class);
        assertEquals(partAddResult.getStatus(), Status.FAILED);
        assertEquals(partAddResult.getMessage(),
                "No timeline found for fact=testFactStoragePartitions, storage=S2, "
                        + "update period=HOURLY, partition column=non_existant_time_part.");
        // Add a partition
        XPartition xp = createPartition(table, partDate);
        partAddResult = target().path("metastore/facts/").path(table).path("storages/S2/partition")
                .queryParam("sessionid", lensSessionId).request(mediaType).post(Entity.entity(
                        new GenericEntity<JAXBElement<XPartition>>(cubeObjectFactory.createXPartition(xp)) {
                        }, mediaType), APIResult.class);
        assertSuccess(partAddResult);

        // add same should fail
        partAddResult = target().path("metastore/facts/").path(table).path("storages/S2/partition")
                .queryParam("sessionid", lensSessionId).request(mediaType).post(Entity.entity(
                        new GenericEntity<JAXBElement<XPartition>>(cubeObjectFactory.createXPartition(xp)) {
                        }, mediaType), APIResult.class);
        assertEquals(partAddResult.getStatus(), Status.FAILED);

        xp.setLocation(xp.getLocation() + "/a/b/c");
        APIResult partUpdateResult = target().path("metastore/facts/").path(table).path("storages/S2/partition")
                .queryParam("sessionid", lensSessionId)
                .queryParam("filter", "dt='" + HOURLY.format(partDate) + "'").request(mediaType)
                .put(Entity.entity(
                        new GenericEntity<JAXBElement<XPartition>>(cubeObjectFactory.createXPartition(xp)) {
                        }, mediaType), APIResult.class);
        assertSuccess(partUpdateResult);

        JAXBElement<XPartitionList> partitionsElement = target().path("metastore/facts").path(table)
                .path("storages/S2/partitions").queryParam("sessionid", lensSessionId)
                .queryParam("filter", "dt='" + HOURLY.format(partDate) + "'").request(mediaType)
                .get(new GenericType<JAXBElement<XPartitionList>>() {
                });

        XPartitionList partitions = partitionsElement.getValue();
        assertNotNull(partitions);
        assertEquals(partitions.getPartition().size(), 1);
        XPartition readPartition = partitions.getPartition().get(0);
        assertEquals(readPartition.getLocation(), xp.getLocation());
        assertEquals(readPartition.getTimePartitionSpec(), xp.getTimePartitionSpec());
        assertEquals(readPartition.getNonTimePartitionSpec(), xp.getNonTimePartitionSpec());
        assertNotNull(readPartition.getFullPartitionSpec());
        XTimePartSpecElement timePartSpec = readPartition.getTimePartitionSpec().getPartSpecElement().iterator()
                .next();
        XPartSpecElement fullPartSpec = readPartition.getFullPartitionSpec().getPartSpecElement().iterator()
                .next();
        assertEquals(timePartSpec.getKey(), fullPartSpec.getKey());
        assertEquals(UpdatePeriod.valueOf(xp.getUpdatePeriod().name())
                .format(JAXBUtils.getDateFromXML(timePartSpec.getValue())), fullPartSpec.getValue());
        DateTime date = target().path("metastore/cubes").path("testCube").path("latestdate")
                .queryParam("timeDimension", "dt").queryParam("sessionid", lensSessionId).request(mediaType)
                .get(DateTime.class);

        partDate.setMinutes(0);
        partDate.setSeconds(0);
        partDate.setTime(partDate.getTime() - partDate.getTime() % 1000);
        assertEquals(date.getDate(), partDate);
        // add two partitions, one of them already added. result should be partial
        XPartitionList parts = new XPartitionList();
        parts.getPartition().add(xp);
        parts.getPartition().add(createPartition(table, DateUtils.addHours(partDate, 1)));
        partAddResult = target().path("metastore/facts/").path(table).path("storages/S2/partitions")
                .queryParam("sessionid", lensSessionId).request(mediaType)
                .post(Entity.entity(new GenericEntity<JAXBElement<XPartitionList>>(
                        cubeObjectFactory.createXPartitionList(parts)) {
                }, mediaType), APIResult.class);
        assertEquals(partAddResult.getStatus(), Status.PARTIAL);

        // Drop the partitions
        APIResult dropResult = target().path("metastore/facts").path(table).path("storages/S2/partitions")
                .queryParam("sessionid", lensSessionId).request(mediaType).delete(APIResult.class);

        assertSuccess(dropResult);

        // Verify partition was dropped
        partitionsElement = target().path("metastore/facts").path(table).path("storages/S2/partitions")
                .queryParam("sessionid", lensSessionId)
                .queryParam("filter", "dt='" + HOURLY.format(partDate) + "'").request(mediaType)
                .get(new GenericType<JAXBElement<XPartitionList>>() {
                });

        partitions = partitionsElement.getValue();
        assertNotNull(partitions);
        assertEquals(partitions.getPartition().size(), 0);
        // add null in batch
        resp = target().path("metastore/facts/").path(table).path("storages/S2/partitions")
                .queryParam("sessionid", lensSessionId).request(mediaType).post(null);
        Assert.assertEquals(resp.getStatus(), 400);

        // Try adding in batch, but to a wrong endpoint
        resp = target().path("metastore/facts/").path(table).path("storages/S2/partition")
                .queryParam("sessionid", lensSessionId).request(mediaType)
                .post(Entity.entity(new GenericEntity<JAXBElement<XPartitionList>>(
                        cubeObjectFactory.createXPartitionList(toXPartitionList(xp))) {
                }, mediaType));
        assertXMLError(resp, mediaType);

        // Try adding in batch, but provide just an XPartition
        resp = target().path("metastore/facts/").path(table).path("storages/S2/partitions")
                .queryParam("sessionid", lensSessionId).request(mediaType).post(Entity.entity(
                        new GenericEntity<JAXBElement<XPartition>>(cubeObjectFactory.createXPartition(xp)) {
                        }, mediaType));
        if (mediaType.equals(MediaType.APPLICATION_XML_TYPE)) {
            assertXMLError(resp, mediaType);
        } else {
            // for json input, XPartitionList is getting created
            assertEquals(resp.getStatus(), 200);
        }

        // Try adding in batch with one partition being wrong wrt partition column.
        response = target().path("metastore/facts/").path(table).path("storages/S2/partitions")
                .queryParam("sessionid", lensSessionId).request(mediaType)
                .post(Entity.entity(new GenericEntity<JAXBElement<XPartitionList>>(
                        cubeObjectFactory.createXPartitionList(toXPartitionList(xp2))) {
                }, mediaType));
        assertEquals(response.getStatus(), 400);
        partAddResult = response.readEntity(APIResult.class);
        assertEquals(partAddResult.getStatus(), Status.FAILED);
        assertEquals(partAddResult.getMessage(),
                "No timeline found for fact=testFactStoragePartitions, storage=S2, "
                        + "update period=HOURLY, partition column=non_existant_time_part.");
        // Add in batch
        partAddResult = target().path("metastore/facts/").path(table).path("storages/S2/partitions")
                .queryParam("sessionid", lensSessionId).request(mediaType)
                .post(Entity.entity(new GenericEntity<JAXBElement<XPartitionList>>(
                        cubeObjectFactory.createXPartitionList(toXPartitionList(xp))) {
                }, mediaType), APIResult.class);
        assertSuccess(partAddResult);

        // Verify partition was added
        partitionsElement = target().path("metastore/facts").path(table).path("storages/S2/partitions")
                .queryParam("sessionid", lensSessionId)
                .queryParam("filter", "dt='" + HOURLY.format(partDate) + "'").request(mediaType)
                .get(new GenericType<JAXBElement<XPartitionList>>() {
                });

        partitions = partitionsElement.getValue();
        assertNotNull(partitions);
        assertEquals(partitions.getPartition().size(), 1);

        // Drop again by values
        String[] val = new String[] { HOURLY.format(partDate) };
        dropResult = target().path("metastore/facts").path(table).path("storages/S2/partition")
                .queryParam("values", StringUtils.join(val, ",")).queryParam("sessionid", lensSessionId)
                .request(mediaType).delete(APIResult.class);
        assertSuccess(dropResult);

        // Verify partition was dropped
        partitionsElement = target().path("metastore/facts").path(table).path("storages/S2/partitions")
                .queryParam("sessionid", lensSessionId)
                .queryParam("filter", "dt='" + HOURLY.format(partDate) + "'").request(mediaType)
                .get(new GenericType<JAXBElement<XPartitionList>>() {
                });

        partitions = partitionsElement.getValue();
        assertNotNull(partitions);
        assertEquals(partitions.getPartition().size(), 0);
    } finally {
        setCurrentDatabase(prevDb, mediaType);
        dropDatabase(DB, mediaType);
    }
}

From source file:com.clark.func.Functions.java

/**
 * <p>//from w ww . j a v  a2s . c  o m
 * Internal calculation method.
 * </p>
 * 
 * @param val
 *            the calendar
 * @param field
 *            the field constant
 * @param modType
 *            type to truncate, round or ceiling
 * @throws ArithmeticException
 *             if the year is over 280 million
 */
private static void modifyDate(Calendar val, int field, int modType) {
    if (val.get(Calendar.YEAR) > 280000000) {
        throw new ArithmeticException("Calendar value too large for accurate calculations");
    }

    if (field == Calendar.MILLISECOND) {
        return;
    }

    // ----------------- Fix for LANG-59 ---------------------- START
    // ---------------
    // see http://issues.apache.org/jira/browse/LANG-59
    //
    // Manually truncate milliseconds, seconds and minutes, rather than
    // using
    // Calendar methods.

    Date date = val.getTime();
    long time = date.getTime();
    boolean done = false;

    // truncate milliseconds
    int millisecs = val.get(Calendar.MILLISECOND);
    if (MODIFY_TRUNCATE == modType || millisecs < 500) {
        time = time - millisecs;
    }
    if (field == Calendar.SECOND) {
        done = true;
    }

    // truncate seconds
    int seconds = val.get(Calendar.SECOND);
    if (!done && (MODIFY_TRUNCATE == modType || seconds < 30)) {
        time = time - (seconds * 1000L);
    }
    if (field == Calendar.MINUTE) {
        done = true;
    }

    // truncate minutes
    int minutes = val.get(Calendar.MINUTE);
    if (!done && (MODIFY_TRUNCATE == modType || minutes < 30)) {
        time = time - (minutes * 60000L);
    }

    // reset time
    if (date.getTime() != time) {
        date.setTime(time);
        val.setTime(date);
    }
    // ----------------- Fix for LANG-59 ----------------------- END
    // ----------------

    boolean roundUp = false;
    for (int i = 0; i < fields.length; i++) {
        for (int j = 0; j < fields[i].length; j++) {
            if (fields[i][j] == field) {
                // This is our field... we stop looping
                if (modType == MODIFY_CEILING || (modType == MODIFY_ROUND && roundUp)) {
                    if (field == SEMI_MONTH) {
                        // This is a special case that's hard to generalize
                        // If the date is 1, we round up to 16, otherwise
                        // we subtract 15 days and add 1 month
                        if (val.get(Calendar.DATE) == 1) {
                            val.add(Calendar.DATE, 15);
                        } else {
                            val.add(Calendar.DATE, -15);
                            val.add(Calendar.MONTH, 1);
                        }
                        // ----------------- Fix for LANG-440
                        // ---------------------- START ---------------
                    } else if (field == Calendar.AM_PM) {
                        // This is a special case
                        // If the time is 0, we round up to 12, otherwise
                        // we subtract 12 hours and add 1 day
                        if (val.get(Calendar.HOUR_OF_DAY) == 0) {
                            val.add(Calendar.HOUR_OF_DAY, 12);
                        } else {
                            val.add(Calendar.HOUR_OF_DAY, -12);
                            val.add(Calendar.DATE, 1);
                        }
                        // ----------------- Fix for LANG-440
                        // ---------------------- END ---------------
                    } else {
                        // We need at add one to this field since the
                        // last number causes us to round up
                        val.add(fields[i][0], 1);
                    }
                }
                return;
            }
        }
        // We have various fields that are not easy roundings
        int offset = 0;
        boolean offsetSet = false;
        // These are special types of fields that require different rounding
        // rules
        switch (field) {
        case SEMI_MONTH:
            if (fields[i][0] == Calendar.DATE) {
                // If we're going to drop the DATE field's value,
                // we want to do this our own way.
                // We need to subtrace 1 since the date has a minimum of
                // 1
                offset = val.get(Calendar.DATE) - 1;
                // If we're above 15 days adjustment, that means we're
                // in
                // the
                // bottom half of the month and should stay accordingly.
                if (offset >= 15) {
                    offset -= 15;
                }
                // Record whether we're in the top or bottom half of
                // that
                // range
                roundUp = offset > 7;
                offsetSet = true;
            }
            break;
        case Calendar.AM_PM:
            if (fields[i][0] == Calendar.HOUR_OF_DAY) {
                // If we're going to drop the HOUR field's value,
                // we want to do this our own way.
                offset = val.get(Calendar.HOUR_OF_DAY);
                if (offset >= 12) {
                    offset -= 12;
                }
                roundUp = offset >= 6;
                offsetSet = true;
            }
            break;
        }
        if (!offsetSet) {
            int min = val.getActualMinimum(fields[i][0]);
            int max = val.getActualMaximum(fields[i][0]);
            // Calculate the offset from the minimum allowed value
            offset = val.get(fields[i][0]) - min;
            // Set roundUp if this is more than half way between the minimum
            // and maximum
            roundUp = offset > ((max - min) / 2);
        }
        // We need to remove this field
        if (offset != 0) {
            val.set(fields[i][0], val.get(fields[i][0]) - offset);
        }
    }
    throw new IllegalArgumentException("The field " + field + " is not supported");

}