Example usage for java.lang Short shortValue

List of usage examples for java.lang Short shortValue

Introduction

In this page you can find the example usage for java.lang Short shortValue.

Prototype

@HotSpotIntrinsicCandidate
public short shortValue() 

Source Link

Document

Returns the value of this Short as a short .

Usage

From source file:com.funambol.foundation.items.dao.PIMCalendarDAO.java

/**
 * Adds a calendar. If necessary, a new ID is generated and set in the
 * CalendarWrapper./*from   w w  w  . j a  va  2 s  .c  o  m*/
 *
 * @param c as a CalendarWrapper object, usually without an ID set.
 * @throws DAOException
 *
 * @see CalendarWrapper
 */
public void addItem(CalendarWrapper cw) throws DAOException {

    if (log.isTraceEnabled()) {
        log.trace("PIMCalendarDAO addItem begin");
    }

    Connection con = null;
    PreparedStatement ps = null;

    long id = 0;
    String allDay = null;
    String body = null;
    Short busyStatus = null;
    String categories = null;
    String companies = null;
    int duration = 0;
    Date dend = null;
    short importance = 0;
    String location = null;
    Short meetingStatus = null;
    String mileage = null;
    Date replyTime = null;
    short sensitivity = 0;
    Date dstart = null;
    String subject = null;
    int interval = 0;
    short monthOfYear = 0;
    short dayOfMonth = 0;
    String dayOfWeekMask = null;
    short instance = 0;
    String startDatePattern = null;
    String endDatePattern = null;
    Reminder reminder = null;
    RecurrencePattern rp = null;
    short recurrenceType = -1;
    String sId = null;
    int occurrences = -1;
    String folder = null;
    String dstartTimeZone = null;
    String dendTimeZone = null;
    String reminderTimeZone = null;
    Date completed = null;
    short percentComplete = -1;

    Timestamp lastUpdate = cw.getLastUpdate();
    if (lastUpdate == null) {
        lastUpdate = new Timestamp(System.currentTimeMillis());
    }

    try {

        sId = cw.getId();
        if (sId == null || sId.length() == 0) { // ...as it should be
            sId = getNextID();
            cw.setId(sId);
        }
        id = Long.parseLong(sId);

        CalendarContent c = cw.getCalendar().getCalendarContent();

        rp = c.getRecurrencePattern();

        boolean allDayB;
        if (c.getAllDay() != null && c.getAllDay().booleanValue()) {
            allDayB = true;
            allDay = "1";
        } else {
            allDayB = false;
            allDay = "0";
        }

        String sd = null;
        if (c.getDtStart() != null) {
            sd = c.getDtStart().getPropertyValueAsString();
            dstart = getDateFromString(allDayB, sd, "000000");
        }

        String ed = null;
        if ((sd != null && sd.length() > 0) || c.getDtEnd() != null) {
            ed = c.getDtEnd().getPropertyValueAsString();

            //
            // Fix for Siemens S56 end date issue only for event
            // @todo: verify if is really need to do this
            //
            if (c instanceof Event) {
                if (ed == null || ed.length() == 0) {
                    ed = sd;
                }
            }

            dend = getDateFromString(allDayB, ed, "235900");
        }

        body = Property.stringFrom(c.getDescription());

        if (c.getBusyStatus() != null) {
            busyStatus = new Short(c.getBusyStatus().shortValue());
        }

        categories = Property.stringFrom(c.getCategories());
        companies = Property.stringFrom(c.getOrganizer());
        location = Property.stringFrom(c.getLocation());
        folder = Property.stringFrom(c.getFolder());
        dstartTimeZone = timeZoneFrom(c.getDtStart());
        dendTimeZone = timeZoneFrom(c.getDtEnd());
        reminderTimeZone = timeZoneFrom(c.getReminder());
        meetingStatus = c.getMeetingStatus();

        Integer mileageInteger = c.getMileage(); // NB: not an int...
        if (mileageInteger != null) { // ...therefore it may be null
            mileage = String.valueOf(mileageInteger);
        }

        if (c instanceof Event) {
            replyTime = getDateFromString(allDayB, // @todo or false?
                    Property.stringFrom(((Event) c).getReplyTime()), "000000");
        }

        try {
            sensitivity = Short.parseShort(Property.stringFrom(c.getAccessClass()));
        } catch (NumberFormatException nfe) {
            sensitivity = 0;
            // The short must go on
        }

        if ((subject = Property.stringFrom(c.getSummary())) == null && body != null) {
            int endOfSentence = body.indexOf('.');
            if (endOfSentence == -1) {
                endOfSentence = body.length();
            }
            if (endOfSentence > SQL_SUBJECT_DIM) {
                endOfSentence = SQL_SUBJECT_DIM;
            }
            subject = body.substring(0, endOfSentence);
        }

        String isInfinite = "0";
        if (rp != null) {
            interval = rp.getInterval();
            recurrenceType = rp.getTypeId();
            monthOfYear = rp.getMonthOfYear();
            dayOfMonth = rp.getDayOfMonth();
            dayOfWeekMask = String.valueOf(rp.getDayOfWeekMask());
            instance = rp.getInstance();
            startDatePattern = rp.getStartDatePattern();
            endDatePattern = rp.getEndDatePattern();
            if (rp.isNoEndDate()) {
                isInfinite = "1";
            }
            occurrences = rp.getOccurrences();
        }

        if (c instanceof Task) {
            Task t = (Task) c;
            if (t.getDateCompleted() != null) {
                completed = getDateFromString(allDayB, ((Task) c).getDateCompleted().getPropertyValueAsString(),
                        "000000");
            }

            try {
                String complete = Property.stringFrom(t.getComplete());
                if (complete != null && complete.equals("1")) {
                    percentComplete = 100;
                } else {
                    percentComplete = Short.parseShort(Property.stringFrom(t.getPercentComplete()));
                }
                if (percentComplete < 0 || percentComplete > 100) {
                    throw new NumberFormatException("A percentage can't be " + percentComplete);
                }
            } catch (NumberFormatException nfe) {
                percentComplete = -1; // the short must go on
            }

            meetingStatus = getTaskStatus(t);
        }

        con = getUserDataSource().getRoutedConnection(userId);

        ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_CALENDAR);

        ps.setLong(1, id);
        ps.setString(2, userId);
        ps.setLong(3, lastUpdate.getTime());
        ps.setString(4, String.valueOf(Def.PIM_STATE_NEW));
        ps.setString(5, allDay);
        ps.setString(6, StringUtils.left(body, SQL_BODY_DIM));
        if (busyStatus != null) {
            ps.setShort(7, busyStatus.shortValue());
        } else {
            ps.setNull(7, Types.SMALLINT);
        }
        ps.setString(8, StringUtils.left(categories, SQL_CATEGORIES_DIM));
        ps.setString(9, StringUtils.left(companies, SQL_COMPANIES_DIM));
        ps.setInt(10, duration);
        if (dend != null) {
            ps.setTimestamp(11, new Timestamp(dend.getTime()));
        } else {
            ps.setNull(11, Types.TIMESTAMP);
        }

        if (c.getPriority() != null) {

            String priority = c.getPriority().getPropertyValueAsString();

            if (priority != null && priority.length() > 0) {
                importance = Short.parseShort(priority);
                ps.setShort(12, importance);
            } else {
                ps.setNull(12, Types.SMALLINT);
            }

        } else {
            ps.setNull(12, Types.SMALLINT);
        }

        ps.setString(13, StringUtils.left(location, SQL_LOCATION_DIM));

        if (meetingStatus != null) {
            ps.setShort(14, meetingStatus.shortValue());
        } else {
            ps.setNull(14, Types.SMALLINT);
        }

        ps.setString(15, mileage);

        reminder = c.getReminder();
        if (reminder != null && reminder.isActive()) {
            Timestamp reminderTime = getReminderTime(dstart, reminder);
            if (reminderTime == null) {
                ps.setNull(16, Types.TIMESTAMP);
            } else {
                ps.setTimestamp(16, reminderTime);
            }
            ps.setInt(17, reminder.getRepeatCount());
            ps.setString(18, (reminder.isActive()) ? "1" : "0");

            String soundFileValue = reminder.getSoundFile();
            ps.setString(19, StringUtils.left(soundFileValue, SQL_SOUNDFILE_DIM));
            ps.setInt(20, reminder.getOptions());
        } else {
            ps.setNull(16, Types.TIMESTAMP);
            ps.setInt(17, 0);
            ps.setString(18, "0");
            ps.setString(19, null);
            ps.setInt(20, 0);
        }

        if (replyTime != null) {
            ps.setTimestamp(21, new Timestamp(replyTime.getTime()));
        } else {
            ps.setNull(21, Types.TIMESTAMP);
        }

        ps.setShort(22, sensitivity);

        if (dstart != null) {
            ps.setTimestamp(23, new Timestamp(dstart.getTime()));
        } else {
            ps.setNull(23, Types.TIMESTAMP);
        }
        ps.setString(24, StringUtils.left(subject, SQL_SUBJECT_DIM));
        ps.setShort(25, recurrenceType);
        ps.setInt(26, interval);
        ps.setShort(27, monthOfYear);
        ps.setShort(28, dayOfMonth);
        ps.setString(29, StringUtils.left(dayOfWeekMask, SQL_DAYOFWEEKMASK_DIM));
        ps.setShort(30, instance);
        ps.setString(31, StringUtils.left(startDatePattern, SQL_STARTDATEPATTERN_DIM));
        ps.setString(32, isInfinite);
        ps.setString(33, StringUtils.left(endDatePattern, SQL_ENDDATEPATTERN_DIM));
        ps.setInt(34, occurrences);

        if (c instanceof Event) {
            ps.setInt(35, CALENDAR_EVENT_TYPE);
            ps.setNull(36, Types.TIMESTAMP); // completed
            ps.setNull(37, Types.SMALLINT); // percent_complete
        } else {
            ps.setInt(35, CALENDAR_TASK_TYPE);

            if (completed != null) {
                ps.setTimestamp(36, new Timestamp(completed.getTime()));
            } else {
                ps.setNull(36, Types.TIMESTAMP);
            }

            if (percentComplete != -1) {
                ps.setShort(37, percentComplete);
            } else {
                ps.setNull(37, Types.SMALLINT);
            }
        }

        ps.setString(38, StringUtils.left(folder, SQL_FOLDER_DIM));
        ps.setString(39, StringUtils.left(dstartTimeZone, SQL_TIME_ZONE_DIM));
        ps.setString(40, StringUtils.left(dendTimeZone, SQL_TIME_ZONE_DIM));
        ps.setString(41, StringUtils.left(reminderTimeZone, SQL_TIME_ZONE_DIM));

        ps.executeUpdate();
        DBTools.close(null, ps, null);

        if (recurrenceType != -1) {
            List<ExceptionToRecurrenceRule> exceptions = rp.getExceptions();
            for (ExceptionToRecurrenceRule etrr : exceptions) {
                ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_CALENDAR_EXCEPTION);

                ps.setLong(1, id);
                ps.setString(2, (etrr.isAddition() ? "1" : "0"));
                ps.setTimestamp(3,
                        new Timestamp(getDateFromString(allDayB, etrr.getDate(), "000000").getTime()));

                ps.executeUpdate();
                DBTools.close(null, ps, null);
            }
        }

    } catch (Exception e) {
        throw new DAOException("Error adding a calendar item: " + e.getMessage(), e);
    } finally {
        DBTools.close(con, ps, null);
    }
}

From source file:com.funambol.foundation.items.dao.PIMContactDAO.java

/**
 * Adds a contact. If necessary, a new ID is generated and set in the
 * ContactWrapper./*from w ww. j  a  va2  s.c  om*/
 *
 * @param cw as a ContactWrapper object, usually without an ID set.
 * @throws DAOException
 *
 * @see ContactWrapper
 */
public void addItem(ContactWrapper cw) throws DAOException {
    if (log.isTraceEnabled()) {
        log.trace("Storing a contact item...");
    }

    Connection con = null;
    PreparedStatement ps = null;

    long id = 0;
    int type = 0;

    PersonalDetail personalDetail = null;
    BusinessDetail businessDetail = null;
    Address homeAddressBook = null;
    Address workAddressBook = null;
    Address otherAddressBook = null;

    Name name = null;
    Phone phone = null;
    Email email = null;
    WebPage webPage = null;

    List<WebPage> webPages = new ArrayList<WebPage>();
    List<Email> emails = new ArrayList<Email>();
    List<Phone> phones = new ArrayList<Phone>();
    List<String[]> labels = new ArrayList<String[]>();

    String webPageType = null;

    Short importance = null;
    Short sensitivity = null;
    String mileage = null;
    String subject = null;
    String folder = null;
    String anniversary = null;
    String firstName = null;
    String middleName = null;
    String lastName = null;
    String displayName = null;
    String birthday = null;
    String categories = null;
    String gender = null;
    String hobbies = null;
    String initials = null;
    String languages = null;
    String nickName = null;
    String spouse = null;
    String suffix = null;
    String assistant = null;
    String officeLocation = null;
    String company = null;
    String companies = null;
    String department = null;
    String manager = null;
    String role = null;
    String children = null;
    String salutation = null;
    String sId = null;

    Timestamp lastUpdate = cw.getLastUpdate();
    if (lastUpdate == null) {
        lastUpdate = new Timestamp(System.currentTimeMillis());
    }

    try {

        // Looks up the data source when the first connection is created
        con = getUserDataSource().getRoutedConnection(userId);

        sId = cw.getId();
        if (sId == null) { // ...as it should be
            sId = getNextID();
            cw.setId(sId);
        }
        id = Long.parseLong(sId);

        Contact c = cw.getContact();
        personalDetail = c.getPersonalDetail();
        businessDetail = c.getBusinessDetail();
        name = c.getName();

        if (personalDetail != null) {
            homeAddressBook = personalDetail.getAddress();
            otherAddressBook = personalDetail.getOtherAddress();
            webPages.addAll(personalDetail.getWebPages());
            emails.addAll(personalDetail.getEmails());
            phones.addAll(personalDetail.getPhones());
        }

        if (businessDetail != null) {
            workAddressBook = businessDetail.getAddress();
            webPages.addAll(businessDetail.getWebPages());
            emails.addAll(businessDetail.getEmails());
            phones.addAll(businessDetail.getPhones());
            companies = businessDetail.getCompanies();

        }

        importance = c.getImportance();
        sensitivity = c.getSensitivity();
        mileage = c.getMileage();
        subject = c.getSubject();
        languages = c.getLanguages();

        categories = Property.stringFrom(c.getCategories());
        folder = c.getFolder();

        if (personalDetail != null) {
            anniversary = personalDetail.getAnniversary();
            birthday = personalDetail.getBirthday();
            children = personalDetail.getChildren();
            spouse = personalDetail.getSpouse();
            hobbies = personalDetail.getHobbies();
            gender = personalDetail.getGender();
        }

        if (businessDetail != null) {
            assistant = businessDetail.getAssistant();
            manager = businessDetail.getManager();
            officeLocation = businessDetail.getOfficeLocation();
            company = Property.stringFrom(businessDetail.getCompany());
            department = Property.stringFrom(businessDetail.getDepartment());
            role = Property.stringFrom(businessDetail.getRole());
        }

        if (name != null) {
            firstName = Property.stringFrom(name.getFirstName());
            middleName = Property.stringFrom(name.getMiddleName());
            lastName = Property.stringFrom(name.getLastName());
            displayName = Property.stringFrom(name.getDisplayName());
            initials = Property.stringFrom(name.getInitials());
            nickName = Property.stringFrom(name.getNickname());
            suffix = Property.stringFrom(name.getSuffix());
            salutation = Property.stringFrom(name.getSalutation());
        }

        ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_CONTACT);

        //
        // GENERAL
        //

        if (log.isTraceEnabled()) {
            log.trace("Preparing statement with ID " + id);
        }
        ps.setLong(1, id);

        if (log.isTraceEnabled()) {
            log.trace("Preparing statement with user ID " + userId);
        }
        ps.setString(2, userId);

        ps.setLong(3, lastUpdate.getTime());
        ps.setString(4, String.valueOf(Def.PIM_STATE_NEW));

        boolean hasPhoto = false;
        Photo photo = personalDetail.getPhotoObject();
        if (photo != null && (photo.getImage() != null || photo.getUrl() != null)) {
            hasPhoto = true;
            ps.setShort(5, photo.getImage() != null ? ContactWrapper.PHOTO_IMAGE : ContactWrapper.PHOTO_URL);
        } else if (photo != null) {
            ps.setShort(5, ContactWrapper.EMPTY_PHOTO);
        } else {
            ps.setNull(5, Types.SMALLINT);
        }

        //
        // CONTACT DETAILS
        //

        if (importance != null) {
            ps.setShort(6, importance.shortValue());
        } else {
            ps.setNull(6, Types.SMALLINT);
        }

        if (sensitivity != null) {
            ps.setShort(7, sensitivity.shortValue());
        } else {
            ps.setNull(7, Types.SMALLINT);
        }

        ps.setString(8, StringUtils.left(subject, SQL_SUBJECT_DIM));
        ps.setString(9, StringUtils.left(folder, SQL_FOLDER_DIM));

        //
        // PERSONAL DETAILS
        //

        ps.setString(10, StringUtils.left(anniversary, SQL_ANNIVERSARY_DIM));
        ps.setString(11, StringUtils.left(firstName, SQL_FIRSTNAME_DIM));
        ps.setString(12, StringUtils.left(middleName, SQL_MIDDLENAME_DIM));
        ps.setString(13, StringUtils.left(lastName, SQL_LASTNAME_DIM));
        ps.setString(14, StringUtils.left(displayName, SQL_DISPLAYNAME_DIM));
        ps.setString(15, StringUtils.left(birthday, SQL_BIRTHDAY_DIM));

        if (c.getNotes() != null && c.getNotes().size() > 0) {
            String noteValue = ((Note) c.getNotes().get(0)).getPropertyValueAsString();
            ps.setString(16, StringUtils.left(noteValue, SQL_NOTE_DIM));
        } else {
            ps.setString(16, null);
        }

        ps.setString(17, StringUtils.left(categories, SQL_CATEGORIES_DIM));
        ps.setString(18, StringUtils.left(children, SQL_CHILDREN_DIM));
        ps.setString(19, StringUtils.left(hobbies, SQL_HOBBIES_DIM));
        ps.setString(20, StringUtils.left(initials, SQL_INITIALS_DIM));
        ps.setString(21, StringUtils.left(languages, SQL_LANGUAGES_DIM));
        ps.setString(22, StringUtils.left(nickName, SQL_NICKNAME_DIM));
        ps.setString(23, StringUtils.left(spouse, SQL_SPOUSE_DIM));
        ps.setString(24, StringUtils.left(suffix, SQL_SUFFIX_DIM));
        ps.setString(25, StringUtils.left(salutation, SQL_SALUTATION_DIM));

        //
        // BUSINESS DETAILS
        //
        ps.setString(26, StringUtils.left(assistant, SQL_ASSISTANT_DIM));
        ps.setString(27, StringUtils.left(company, SQL_COMPANY_DIM));
        ps.setString(28, StringUtils.left(department, SQL_DEPARTMENT_DIM));

        if (businessDetail.getTitles() != null && businessDetail.getTitles().size() > 0) {
            String titleValue = ((Title) businessDetail.getTitles().get(0)).getPropertyValueAsString();
            ps.setString(29, StringUtils.left(titleValue, SQL_TITLE_DIM));
        } else {
            ps.setString(29, null);
        }

        ps.setString(30, StringUtils.left(manager, SQL_MANAGER_DIM));
        if (mileage != null && mileage.length() > SQL_MILEAGE_DIM) {
            mileage = mileage.substring(0, SQL_MILEAGE_DIM);
        }
        ps.setString(31, StringUtils.left(mileage, SQL_MILEAGE_DIM));
        ps.setString(32, StringUtils.left(officeLocation, SQL_OFFICELOCATION_DIM));
        ps.setString(33, StringUtils.left(role, SQL_ROLE_DIM));
        ps.setString(34, StringUtils.left(companies, SQL_COMPANIES_DIM));
        ps.setString(35, StringUtils.left(gender, SQL_GENDER_DIM));

        ps.executeUpdate();

        DBTools.close(null, ps, null);

        //
        // emails
        //
        if (!emails.isEmpty()) {

            ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_CONTACT_ITEM);

            for (int i = 0, l = emails.size(); i < l; i++) {

                email = emails.get(i);

                type = getContactEmailItemTypeFromEmailPropertyType(email.getEmailType());
                // Unknown property: saves nothing
                if (TYPE_UNDEFINED == type)
                    continue;

                String emailValue = email.getPropertyValueAsString();

                if (emailValue != null && emailValue.length() != 0) {
                    if (emailValue.length() > SQL_EMAIL_DIM) {
                        emailValue = emailValue.substring(0, SQL_EMAIL_DIM);
                    }
                    ps.setLong(1, id);
                    ps.setInt(2, type);
                    ps.setString(3, emailValue);

                    ps.executeUpdate();
                }

            }

            DBTools.close(null, ps, null);

        }

        //
        // phones
        //
        if (!phones.isEmpty()) {

            ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_CONTACT_ITEM);

            for (int i = 0, l = phones.size(); i < l; i++) {

                phone = phones.get(i);

                type = getContactPhoneItemTypeFromPhonePropertyType(phone.getPhoneType());
                // Unknown property: saves nothing
                if (TYPE_UNDEFINED == type)
                    continue;

                String phoneValue = phone.getPropertyValueAsString();
                if (phoneValue != null && phoneValue.length() != 0) {
                    if (phoneValue.length() > SQL_PHONE_DIM) {
                        phoneValue = phoneValue.substring(0, SQL_PHONE_DIM);
                    }

                    ps.setLong(1, id);
                    ps.setInt(2, type);
                    ps.setString(3, phoneValue);

                    ps.executeUpdate();
                }

            }

            DBTools.close(null, ps, null);

        }

        //
        // webPages
        //
        if (!webPages.isEmpty()) {

            ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_CONTACT_ITEM);

            for (int i = 0, l = webPages.size(); i < l; i++) {

                webPage = webPages.get(i);

                webPageType = webPage.getWebPageType();

                if ((FIELD_WEB_PAGE).equals(webPageType)) {
                    type = TYPE_WEB_PAGE;
                } else if ((FIELD_HOME_WEB_PAGE).equals(webPageType)) {
                    type = TYPE_HOME_WEB_PAGE;
                } else if ((FIELD_BUSINESS_WEB_PAGE).equals(webPageType)) {
                    type = TYPE_BUSINESS_WEB_PAGE;
                } else {
                    //
                    // Unknown property: saves nothing
                    //
                    continue;
                }

                String webPageValue = webPage.getPropertyValueAsString();
                if (webPageValue != null && webPageValue.length() != 0) {
                    if (webPageValue.length() > SQL_WEBPAGE_DIM) {
                        webPageValue = webPageValue.substring(0, SQL_WEBPAGE_DIM);
                    }

                    ps.setLong(1, id);
                    ps.setInt(2, type);
                    ps.setString(3, webPageValue);

                    ps.executeUpdate();
                }

            }

            DBTools.close(null, ps, null);

        }

        if (homeAddressBook != null) {

            String homeStreet = Property.stringFrom(homeAddressBook.getStreet());
            String homeCity = Property.stringFrom(homeAddressBook.getCity());
            String homePostalCode = Property.stringFrom(homeAddressBook.getPostalCode());
            String homeState = Property.stringFrom(homeAddressBook.getState());
            String homeCountry = Property.stringFrom(homeAddressBook.getCountry());
            String homePostalOfficeAddress = Property.stringFrom(homeAddressBook.getPostOfficeAddress());
            String homeExtendedAddress = Property.stringFrom(homeAddressBook.getExtendedAddress());

            String homeLabel = Property.stringFrom(homeAddressBook.getLabel());
            if (homeLabel != null) {
                String[] label = { homeLabel, FIELD_HOME_LABEL };
                labels.add(label);
            }

            String[] homeAddressFields = { homeStreet, homeCity, homePostalCode, homeCountry, homeState,
                    homePostalOfficeAddress, homeExtendedAddress };

            if (!hasOnlyEmptyOrNullContent(homeAddressFields)) {

                ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_ADDRESS);

                ps.setLong(1, id);
                ps.setInt(2, ADDRESS_TYPE_HOME);
                ps.setString(3, replaceNewLine(StringUtils.left(homeStreet, SQL_STREET_DIM)));
                ps.setString(4, StringUtils.left(homeCity, SQL_CITY_DIM));
                ps.setString(5, StringUtils.left(homeState, SQL_STATE_DIM));
                ps.setString(6, StringUtils.left(homePostalCode, SQL_POSTALCODE_DIM));
                ps.setString(7, StringUtils.left(homeCountry, SQL_COUNTRY_DIM));
                ps.setString(8, StringUtils.left(homePostalOfficeAddress, SQL_POSTALOFFICEADDRESS_DIM));
                ps.setString(9, StringUtils.left(homeExtendedAddress, SQL_EXTENDEDADDRESS_DIM));

                ps.executeUpdate();

                DBTools.close(null, ps, null);
            }
        }

        if (otherAddressBook != null) {

            String otherStreet = Property.stringFrom(otherAddressBook.getStreet());
            String otherCity = Property.stringFrom(otherAddressBook.getCity());
            String otherPostalCode = Property.stringFrom(otherAddressBook.getPostalCode());
            String otherState = Property.stringFrom(otherAddressBook.getState());
            String otherCountry = Property.stringFrom(otherAddressBook.getCountry());
            String otherPostalOfficeAddress = Property.stringFrom(otherAddressBook.getPostOfficeAddress());
            String otherExtendedAddress = Property.stringFrom(otherAddressBook.getExtendedAddress());

            String otherLabel = Property.stringFrom(otherAddressBook.getLabel());
            if (otherLabel != null) {
                String[] label = { otherLabel, FIELD_OTHER_LABEL };
                labels.add(label);
            }

            String[] otherAddressFields = { otherStreet, otherCity, otherPostalCode, otherCountry, otherState,
                    otherPostalOfficeAddress, otherExtendedAddress };

            if (!hasOnlyEmptyOrNullContent(otherAddressFields)) {

                ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_ADDRESS);

                ps.setLong(1, id);
                ps.setInt(2, ADDRESS_TYPE_OTHER);
                ps.setString(3, replaceNewLine(StringUtils.left(otherStreet, SQL_STREET_DIM)));
                ps.setString(4, StringUtils.left(otherCity, SQL_CITY_DIM));
                ps.setString(5, StringUtils.left(otherState, SQL_STATE_DIM));
                ps.setString(6, StringUtils.left(otherPostalCode, SQL_POSTALCODE_DIM));
                ps.setString(7, StringUtils.left(otherCountry, SQL_COUNTRY_DIM));
                ps.setString(8, StringUtils.left(otherPostalOfficeAddress, SQL_POSTALOFFICEADDRESS_DIM));
                ps.setString(9, StringUtils.left(otherExtendedAddress, SQL_EXTENDEDADDRESS_DIM));

                ps.executeUpdate();

                DBTools.close(null, ps, null);

            }
        }

        if (workAddressBook != null) {

            String workStreet = Property.stringFrom(workAddressBook.getStreet());
            String workCity = Property.stringFrom(workAddressBook.getCity());
            String workPostalCode = Property.stringFrom(workAddressBook.getPostalCode());
            String workState = Property.stringFrom(workAddressBook.getState());
            String workCountry = Property.stringFrom(workAddressBook.getCountry());
            String workPostalOfficeAddress = Property.stringFrom(workAddressBook.getPostOfficeAddress());
            String workExtendedAddress = Property.stringFrom(workAddressBook.getExtendedAddress());

            String workLabel = Property.stringFrom(workAddressBook.getLabel());
            if (workLabel != null) {
                String[] label = { workLabel, FIELD_BUSINESS_LABEL };
                labels.add(label);
            }

            String[] workAddressFields = { workStreet, workCity, workPostalCode, workCountry, workState,
                    workPostalOfficeAddress, workExtendedAddress };

            if (!hasOnlyEmptyOrNullContent(workAddressFields)) {

                ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_ADDRESS);

                ps.setLong(1, id);
                ps.setInt(2, ADDRESS_TYPE_WORK);
                ps.setString(3, replaceNewLine(StringUtils.left(workStreet, SQL_STREET_DIM)));
                ps.setString(4, StringUtils.left(workCity, SQL_CITY_DIM));
                ps.setString(5, StringUtils.left(workState, SQL_STATE_DIM));
                ps.setString(6, StringUtils.left(workPostalCode, SQL_POSTALCODE_DIM));
                ps.setString(7, StringUtils.left(workCountry, SQL_COUNTRY_DIM));
                ps.setString(8, StringUtils.left(workPostalOfficeAddress, SQL_POSTALOFFICEADDRESS_DIM));
                ps.setString(9, StringUtils.left(workExtendedAddress, SQL_EXTENDEDADDRESS_DIM));

                ps.executeUpdate();

                DBTools.close(null, ps, null);
            }

        }

        //
        // labels
        //
        if (!labels.isEmpty()) {

            ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_CONTACT_ITEM);

            for (int i = 0, l = labels.size(); i < l; i++) {

                String[] label = labels.get(i);

                String labelType = label[1];

                if ((FIELD_HOME_LABEL).equals(labelType)) {
                    type = TYPE_HOME_LABEL;
                } else if ((FIELD_BUSINESS_LABEL).equals(labelType)) {
                    type = TYPE_BUSINESS_LABEL;
                } else if ((FIELD_OTHER_LABEL).equals(labelType)) {
                    type = TYPE_OTHER_LABEL;
                } else {
                    //
                    // Unknown property: saves nothing
                    //
                    continue;
                }

                String labelValue = label[0];
                if (labelValue != null && labelValue.length() != 0) {
                    if (labelValue.length() > SQL_LABEL_DIM) {
                        labelValue = labelValue.substring(0, SQL_LABEL_DIM);
                    }

                    ps.setLong(1, id);
                    ps.setInt(2, type);
                    ps.setString(3, labelValue);

                    ps.executeUpdate();
                }

            }

            DBTools.close(null, ps, null);

        }

        if (hasPhoto) {
            insertPhoto(con, Long.parseLong(cw.getId()), photo);
        }

    } catch (Exception e) {
        throw new DAOException("Error adding contact.", e);
    } finally {
        DBTools.close(con, ps, null);
    }

    if (log.isTraceEnabled()) {
        log.trace("Added item with ID '" + id + "'");
    }
}

From source file:com.funambol.foundation.items.dao.PIMContactDAO.java

/**
 * Updates a contact.//w w w  . j a va 2  s. co m
 *
 * @param cw as a ContactWrapper object. If its last update time is null,
 *           then it's set to the current time.
 * @return the UID of the contact
 * @throws DAOException
 *
 * @see ContactWrapper
 */
public String updateItem(ContactWrapper cw) throws DAOException {

    Connection con = null;
    PreparedStatement ps = null;
    PreparedStatement ps1 = null;
    ResultSet rs = null;

    int type = 0;

    PersonalDetail personalDetail = null;
    BusinessDetail businessDetail = null;
    Address homeAddressBook = null;
    Address workAddressBook = null;
    Address otherAddressBook = null;

    Name name = null;
    Phone phone = null;
    Email email = null;
    WebPage webPage = null;

    List<WebPage> webPages = new ArrayList<WebPage>();
    List<Email> emails = new ArrayList<Email>();
    List<Phone> phones = new ArrayList<Phone>();
    List<String[]> labels = new ArrayList<String[]>();

    String phoneType = null;
    String webPageType = null;

    StringBuffer queryUpdateFunPimContact = null;

    Short importance = null;
    Short sensitivity = null;
    String mileage = null;
    String subject = null;
    String folder = null;
    String anniversary = null;
    String firstName = null;
    String middleName = null;
    String lastName = null;
    String displayName = null;
    String birthday = null;
    String note = null;
    String categories = null;
    String hobbies = null;
    String gender = null;
    String initials = null;
    String languages = null;
    String nickName = null;
    String spouse = null;
    String suffix = null;
    String assistant = null;
    String company = null;
    String companies = null;
    String department = null;
    String jobTitle = null;
    String manager = null;
    String city = null;
    String state = null;
    String role = null;
    String children = null;
    String salutation = null;
    String officeLocation = null;
    String street = null;
    String postalCode = null;
    String country = null;
    String postOfficeAddress = null;
    String extendedAddress = null;

    String[] addressFields = null;

    boolean findRecord = false;
    boolean emptyAddress = false;

    short photoType = ContactWrapper.EMPTY_PHOTO;
    boolean photoToRemove = false;
    boolean photoToSet = false;
    boolean photoNothingToDo = false;

    StringBuffer sqlUpdateFunPimAddress = null;

    try {

        Timestamp lastUpdate = (cw.getLastUpdate() == null) ? new Timestamp(System.currentTimeMillis())
                : cw.getLastUpdate();

        // Looks up the data source when the first connection is created
        con = getUserDataSource().getRoutedConnection(userId);

        Contact c = cw.getContact();

        personalDetail = c.getPersonalDetail();
        businessDetail = c.getBusinessDetail();
        name = c.getName();
        importance = c.getImportance();
        sensitivity = c.getSensitivity();
        mileage = c.getMileage();
        subject = c.getSubject();
        languages = c.getLanguages();
        folder = c.getFolder();
        categories = Property.stringFrom(c.getCategories());

        if (personalDetail != null) {

            homeAddressBook = personalDetail.getAddress();
            otherAddressBook = personalDetail.getOtherAddress();
            anniversary = personalDetail.getAnniversary();
            birthday = personalDetail.getBirthday();
            children = personalDetail.getChildren();
            spouse = personalDetail.getSpouse();
            hobbies = personalDetail.getHobbies();
            gender = personalDetail.getGender();
            webPages.addAll(personalDetail.getWebPages());
            emails.addAll(personalDetail.getEmails());
            phones.addAll(personalDetail.getPhones());
        }

        if (businessDetail != null) {

            assistant = businessDetail.getAssistant();
            manager = businessDetail.getManager();
            workAddressBook = businessDetail.getAddress();
            companies = businessDetail.getCompanies();
            company = Property.stringFrom(businessDetail.getCompany());
            department = Property.stringFrom(businessDetail.getDepartment());
            role = Property.stringFrom(businessDetail.getRole());
            officeLocation = businessDetail.getOfficeLocation();
            webPages.addAll(businessDetail.getWebPages());
            emails.addAll(businessDetail.getEmails());
            phones.addAll(businessDetail.getPhones());
        }

        if (name != null) {
            firstName = Property.stringFrom(name.getFirstName());
            middleName = Property.stringFrom(name.getMiddleName());
            lastName = Property.stringFrom(name.getLastName());
            displayName = Property.stringFrom(name.getDisplayName());
            initials = Property.stringFrom(name.getInitials());
            nickName = Property.stringFrom(name.getNickname());
            suffix = Property.stringFrom(name.getSuffix());
            salutation = Property.stringFrom(name.getSalutation());
        }

        if (c.getNotes() != null && c.getNotes().size() > 0) {
            note = ((Note) c.getNotes().get(0)).getPropertyValueAsString();
        } else {
            note = null;
        }

        if (businessDetail.getTitles() != null && businessDetail.getTitles().size() > 0) {
            jobTitle = ((Title) businessDetail.getTitles().get(0)).getPropertyValueAsString();
        } else {
            jobTitle = null;
        }

        queryUpdateFunPimContact = new StringBuffer();

        queryUpdateFunPimContact.append(
                SQL_UPDATE_FNBL_PIM_CONTACT_BEGIN + SQL_FIELD_LAST_UPDATE + SQL_EQUALS_QUESTIONMARK_COMMA);

        //
        // Updating photo:
        // 1. if the contact doesn't have a photo (photo null),
        //    nothing should be done (If there is a photo in the db this will
        //    be kept)
        // 2. if the contact has a photo (image or url) it must be inserted
        //    in the db
        // 3. if the photo has a photo but the image and the url are null,
        //    the one in the db must be removed
        //
        Photo photo = personalDetail.getPhotoObject();
        if (photo == null) {
            //
            // nothing to do
            //
            photoNothingToDo = true;
        } else {
            if (photo.getImage() != null) {
                photoType = ContactWrapper.PHOTO_IMAGE;
                photoToSet = true;
            } else if (photo.getUrl() != null) {
                photoType = ContactWrapper.PHOTO_URL;
                photoToSet = true;
            } else {
                photoToRemove = true;
                photoType = ContactWrapper.EMPTY_PHOTO;
            }
            queryUpdateFunPimContact.append(SQL_FIELD_PHOTO_TYPE).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        if (importance != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_IMPORTANCE + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (sensitivity != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_SENSITIVITY + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (subject != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_SUBJECT + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (folder != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_FOLDER + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (anniversary != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_ANNIVERSARY + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (firstName != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_FIRST_NAME + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (middleName != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_MIDDLE_NAME + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (lastName != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_LAST_NAME + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (displayName != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_DISPLAY_NAME + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (birthday != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_BIRTHDAY + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (note != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_BODY + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (categories != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_CATEGORIES + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (children != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_CHILDREN + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (hobbies != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_HOBBIES + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (initials != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_INITIALS + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (languages != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_LANGUAGES + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (nickName != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_NICKNAME + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (spouse != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_SPOUSE + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (suffix != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_SUFFIX + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (salutation != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_TITLE + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (assistant != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_ASSISTANT + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (company != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_COMPANY + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (department != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_DEPARTMENT + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (jobTitle != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_JOB_TITLE + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (manager != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_MANAGER + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (mileage != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_MILEAGE + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (officeLocation != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_OFFICE_LOCATION + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (role != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_PROFESSION + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (companies != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_COMPANIES + SQL_EQUALS_QUESTIONMARK_COMMA);
        }
        if (gender != null) {
            queryUpdateFunPimContact.append(SQL_FIELD_GENDER + SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        queryUpdateFunPimContact
                .append(SQL_FIELD_STATUS + SQL_EQUALS_QUESTIONMARK + SQL_UPDATE_FNBL_PIM_CONTACT_END);

        ps = con.prepareStatement(queryUpdateFunPimContact.toString());

        int k = 1;

        //
        // GENERAL
        //
        ps.setLong(k++, lastUpdate.getTime());

        //
        // PHOTO TYPE
        //
        if (!photoNothingToDo) {
            ps.setShort(k++, photoType);
        }

        //
        // CONTACT DETAILS
        //
        if (importance != null) {
            ps.setShort(k++, importance.shortValue());
        }
        if (sensitivity != null) {
            ps.setShort(k++, sensitivity.shortValue());
        }

        if (subject != null) {
            if (subject.length() > SQL_SUBJECT_DIM) {
                subject = subject.substring(0, SQL_SUBJECT_DIM);
            }
            ps.setString(k++, subject);
        }
        //
        // folder
        //
        if (folder != null) {
            if (folder.length() > SQL_FOLDER_DIM) {
                folder = folder.substring(0, SQL_FOLDER_DIM);
            }
            ps.setString(k++, folder);
        }

        //
        // PERSONAL DETAILS
        //

        //
        // anniversary
        //
        if (anniversary != null) {
            if (anniversary.length() > SQL_ANNIVERSARY_DIM) {
                anniversary = anniversary.substring(0, SQL_ANNIVERSARY_DIM);
            }
            ps.setString(k++, anniversary);
        }
        //
        // firstName
        //
        if (firstName != null) {
            if (firstName.length() > SQL_FIRSTNAME_DIM) {
                firstName = firstName.substring(0, SQL_FIRSTNAME_DIM);
            }
            ps.setString(k++, firstName);
        }
        //
        // middleName
        //
        if (middleName != null) {
            if (middleName.length() > SQL_MIDDLENAME_DIM) {
                middleName = middleName.substring(0, SQL_MIDDLENAME_DIM);
            }
            ps.setString(k++, middleName);
        }
        //
        // lastName
        //
        if (lastName != null) {
            if (lastName.length() > SQL_LASTNAME_DIM) {
                lastName = lastName.substring(0, SQL_LASTNAME_DIM);
            }
            ps.setString(k++, lastName);
        }
        //
        // displayName
        //
        if (displayName != null) {
            if (displayName.length() > SQL_DISPLAYNAME_DIM) {
                displayName = displayName.substring(0, SQL_DISPLAYNAME_DIM);
            }
            ps.setString(k++, displayName);
        }
        //
        // birthday
        //
        if (birthday != null) {
            if (birthday.length() > SQL_BIRTHDAY_DIM) {
                birthday = birthday.substring(0, SQL_BIRTHDAY_DIM);
            }
            ps.setString(k++, birthday);
        }
        //
        // note
        //
        if (note != null) {
            if (note.length() > SQL_NOTE_DIM) {
                note = note.substring(0, SQL_NOTE_DIM);
            }
            ps.setString(k++, note);
        }
        //
        // categories
        //
        if (categories != null) {
            if (categories.length() > SQL_CATEGORIES_DIM) {
                categories = categories.substring(0, SQL_CATEGORIES_DIM);
            }
            ps.setString(k++, categories);
        }
        //
        // children
        //
        if (children != null) {
            if (children.length() > SQL_CHILDREN_DIM) {
                children = children.substring(0, SQL_CHILDREN_DIM);
            }
            ps.setString(k++, children);
        }
        //
        // hobbies
        //
        if (hobbies != null) {
            if (hobbies.length() > SQL_HOBBIES_DIM) {
                hobbies = hobbies.substring(0, SQL_HOBBIES_DIM);
            }
            ps.setString(k++, hobbies);
        }
        //
        // initials
        //
        if (initials != null) {
            if (initials.length() > SQL_INITIALS_DIM) {
                initials = initials.substring(0, SQL_INITIALS_DIM);
            }
            ps.setString(k++, initials);
        }
        //
        // languages
        //
        if (languages != null) {
            if (languages.length() > SQL_LANGUAGES_DIM) {
                languages = initials.substring(0, SQL_LANGUAGES_DIM);
            }
            ps.setString(k++, languages);
        }
        //
        // nickName
        //
        if (nickName != null) {
            if (nickName.length() > SQL_NICKNAME_DIM) {
                nickName = nickName.substring(0, SQL_NICKNAME_DIM);
            }
            ps.setString(k++, nickName);
        }
        //
        // spouse
        //
        if (spouse != null) {
            if (spouse.length() > SQL_SPOUSE_DIM) {
                spouse = spouse.substring(0, SQL_SPOUSE_DIM);
            }
            ps.setString(k++, spouse);
        }
        //
        // suffix
        //
        if (suffix != null) {
            if (suffix.length() > SQL_SUFFIX_DIM) {
                suffix = suffix.substring(0, SQL_SUFFIX_DIM);
            }
            ps.setString(k++, suffix);
        }
        //
        // salutation
        //
        if (salutation != null) {
            if (salutation.length() > SQL_SALUTATION_DIM) {
                salutation = salutation.substring(0, SQL_SALUTATION_DIM);
            }
            ps.setString(k++, salutation);
        }
        //
        // assistant
        //
        if (assistant != null) {
            if (assistant.length() > SQL_ASSISTANT_DIM) {
                assistant = assistant.substring(0, SQL_ASSISTANT_DIM);
            }
            ps.setString(k++, assistant);
        }
        //
        // company
        //
        if (company != null) {
            if (company.length() > SQL_COMPANY_DIM) {
                company = company.substring(0, SQL_COMPANY_DIM);
            }
            ps.setString(k++, company);
        }
        //
        // department
        //
        if (department != null) {
            if (department.length() > SQL_DEPARTMENT_DIM) {
                department = department.substring(0, SQL_DEPARTMENT_DIM);
            }
            ps.setString(k++, department);
        }
        //
        // jobTitle
        //
        if (jobTitle != null) {
            if (jobTitle.length() > SQL_TITLE_DIM) {
                jobTitle = jobTitle.substring(0, SQL_TITLE_DIM);
            }
            ps.setString(k++, jobTitle);
        }
        //
        // manager
        //
        if (manager != null) {
            if (manager.length() > SQL_MANAGER_DIM) {
                manager = manager.substring(0, SQL_MANAGER_DIM);
            }
            ps.setString(k++, manager);
        }
        //
        // mileage
        //
        if (mileage != null) {
            if (mileage.length() > SQL_MILEAGE_DIM) {
                mileage = mileage.substring(0, SQL_MILEAGE_DIM);
            }
            ps.setString(k++, mileage);
        }
        if (officeLocation != null) {
            if (officeLocation.length() > SQL_OFFICELOCATION_DIM) {
                officeLocation = officeLocation.substring(0, SQL_OFFICELOCATION_DIM);
            }
            ps.setString(k++, officeLocation);
        }
        //
        // role
        //
        if (role != null) {
            if (role.length() > SQL_ROLE_DIM) {
                role = role.substring(0, SQL_ROLE_DIM);
            }
            ps.setString(k++, role);
        }

        //
        // companies
        //
        if (companies != null) {
            if (companies.length() > SQL_COMPANIES_DIM) {
                companies = companies.substring(0, SQL_COMPANIES_DIM);
            }
            ps.setString(k++, companies);
        }

        //
        // gender
        //
        if (gender != null) {
            if (gender.length() > SQL_GENDER_DIM) {
                gender = gender.substring(0, SQL_GENDER_DIM);
            }
            ps.setString(k++, gender);
        }

        //
        // status
        //
        ps.setString(k++, String.valueOf('U'));
        //
        // id
        //
        ps.setLong(k++, Long.parseLong(cw.getId()));
        //
        // userId
        //
        ps.setString(k++, userId);

        ps.executeUpdate();

        DBTools.close(null, ps, null);

        //
        // emails
        //
        if (!emails.isEmpty()) {
            ps1 = con.prepareStatement(SQL_CHECK_IF_IN_FNBL_PIM_CONTACT_ITEM);

            for (int i = 0, l = emails.size(); i < l; i++) {

                email = emails.get(i);

                if ((FIELD_EMAIL_1_ADDRESS).equals(email.getEmailType())) {
                    type = TYPE_EMAIL_1_ADDRESS;
                } else if ((FIELD_EMAIL_2_ADDRESS).equals(email.getEmailType())) {
                    type = TYPE_EMAIL_2_ADDRESS;
                } else if ((FIELD_EMAIL_3_ADDRESS).equals(email.getEmailType())) {
                    type = TYPE_EMAIL_3_ADDRESS;
                } else if ((FIELD_INSTANT_MESSENGER).equals(email.getEmailType())) {
                    type = TYPE_INSTANT_MESSENGER;
                } else {
                    //
                    // no save unknown property
                    //
                    continue;
                }

                ps1.setLong(1, Long.parseLong(cw.getId()));
                ps1.setInt(2, type);

                rs = ps1.executeQuery();

                findRecord = rs.next();

                rs.close();
                rs = null;

                String emailValue = email.getPropertyValueAsString();

                emailValue = StringUtils.left(emailValue, SQL_EMAIL_DIM);

                if (!findRecord) {

                    if (emailValue != null && emailValue.length() != 0) {
                        ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_CONTACT_ITEM);

                        ps.setLong(1, Long.parseLong(cw.getId()));
                        ps.setInt(2, type);
                        ps.setString(3, emailValue);

                        ps.executeUpdate();

                        DBTools.close(null, ps, null);
                    }

                } else {

                    if (emailValue != null) {
                        ps = con.prepareStatement(SQL_UPDATE_FNBL_PIM_CONTACT_ITEM);

                        ps.setString(1, emailValue);
                        ps.setLong(2, Long.parseLong(cw.getId()));
                        ps.setInt(3, type);

                    } else {

                        ps = con.prepareStatement(SQL_DELETE_FNBL_PIM_CONTACT_ITEM);

                        ps.setLong(1, Long.parseLong(cw.getId()));
                        ps.setInt(2, type);

                    }

                    ps.executeUpdate();

                    DBTools.close(null, ps, null);
                }
            }

            DBTools.close(null, ps1, null);

        }

        //
        // phones
        //
        if (!phones.isEmpty()) {

            ps1 = con.prepareStatement(SQL_CHECK_IF_IN_FNBL_PIM_CONTACT_ITEM);

            for (int i = 0, l = phones.size(); i < l; i++) {

                phone = phones.get(i);

                phoneType = phone.getPhoneType();

                if ((FIELD_HOME_TELEPHONE_NUMBER).equals(phoneType)) {
                    type = TYPE_HOME_TELEPHONE_NUMBER;
                } else if ((FIELD_HOME_2_TELEPHONE_NUMBER).equals(phoneType)) {
                    type = TYPE_HOME_2_TELEPHONE_NUMBER;
                } else if ((FIELD_HOME_FAX_NUMBER).equals(phoneType)) {
                    type = TYPE_HOME_FAX_NUMBER;
                } else if ((FIELD_MOBILE_TELEPHONE_NUMBER).equals(phoneType)) {
                    type = TYPE_MOBILE_TELEPHONE_NUMBER;
                } else if ((FIELD_CAR_TELEPHONE_NUMBER).equals(phoneType)) {
                    type = TYPE_CAR_TELEPHONE_NUMBER;
                } else if ((FIELD_OTHER_TELEPHONE_NUMBER).equals(phoneType)) {
                    type = TYPE_OTHER_TELEPHONE_NUMBER;
                } else if ((FIELD_OTHER_FAX_NUMBER).equals(phoneType)) {
                    type = TYPE_OTHER_FAX_NUMBER;
                } else if ((FIELD_PRIMARY_TELEPHONE_NUMBER).equals(phoneType)) {
                    type = TYPE_PRIMARY_TELEPHONE_NUMBER;
                } else if ((FIELD_BUSINESS_TELEPHONE_NUMBER).equals(phoneType)) {
                    type = TYPE_BUSINESS_TELEPHONE_NUMBER;
                } else if ((FIELD_BUSINESS_2_TELEPHONE_NUMBER).equals(phoneType)) {
                    type = TYPE_BUSINESS_2_TELEPHONE_NUMBER;
                } else if ((FIELD_BUSINESS_FAX_NUMBER).equals(phoneType)) {
                    type = TYPE_BUSINESS_FAX_NUMBER;
                } else if ((FIELD_COMPANY_MAIN_TELEPHONE_NUMBER).equals(phoneType)) {
                    type = TYPE_COMPANY_MAIN_TELEPHONE_NUMBER;
                } else if ((FIELD_PAGER_NUMBER).equals(phoneType)) {
                    type = TYPE_PAGER_NUMBER;
                } else if ((FIELD_ASSISTANT_NUMBER).equals(phoneType)) {
                    type = TYPE_ASSISTANT_NUMBER;
                } else if ((FIELD_CALLBACK_NUMBER).equals(phoneType)) {
                    type = TYPE_CALLBACK_NUMBER;
                } else {
                    //
                    // Unknown property: saves nothing
                    //
                    continue;
                }

                ps1.setLong(1, Long.parseLong(cw.getId()));
                ps1.setInt(2, type);

                rs = ps1.executeQuery();

                findRecord = rs.next();

                String phoneValue = phone.getPropertyValueAsString();
                phoneValue = StringUtils.left(phoneValue, SQL_EMAIL_DIM);

                if (!findRecord) {
                    if (phoneValue != null && phoneValue.length() != 0) {
                        ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_CONTACT_ITEM);

                        ps.setLong(1, Long.parseLong(cw.getId()));
                        ps.setInt(2, type);
                        ps.setString(3, phoneValue);

                        ps.executeUpdate();

                        DBTools.close(null, ps, null);
                    }

                } else {

                    if (phoneValue != null) {
                        ps = con.prepareStatement(SQL_UPDATE_FNBL_PIM_CONTACT_ITEM);

                        ps.setString(1, phoneValue);
                        ps.setLong(2, Long.parseLong(cw.getId()));
                        ps.setInt(3, type);

                    } else {
                        ps = con.prepareStatement(SQL_DELETE_FNBL_PIM_CONTACT_ITEM);

                        ps.setLong(1, Long.parseLong(cw.getId()));
                        ps.setInt(2, type);
                    }

                    ps.executeUpdate();

                    DBTools.close(null, ps, null);
                }

                DBTools.close(null, null, rs);

            }

            DBTools.close(null, ps1, null);

        }

        //
        // web pages
        //
        if (!webPages.isEmpty()) {

            ps1 = con.prepareStatement(SQL_CHECK_IF_IN_FNBL_PIM_CONTACT_ITEM);

            for (int i = 0, l = webPages.size(); i < l; i++) {

                webPage = webPages.get(i);

                webPageType = webPage.getWebPageType();

                if ((FIELD_WEB_PAGE).equals(webPageType)) {
                    type = TYPE_WEB_PAGE;
                } else if ((FIELD_HOME_WEB_PAGE).equals(webPageType)) {
                    type = TYPE_HOME_WEB_PAGE;
                } else if ((FIELD_BUSINESS_WEB_PAGE).equals(webPageType)) {
                    type = TYPE_BUSINESS_WEB_PAGE;
                } else {
                    //
                    // Unknown property: saves nothing
                    //
                    continue;
                }

                ps1.setLong(1, Long.parseLong(cw.getId()));
                ps1.setInt(2, type);

                rs = ps1.executeQuery();

                findRecord = rs.next();

                String webPageValue = webPage.getPropertyValueAsString();
                webPageValue = StringUtils.left(webPageValue, SQL_WEBPAGE_DIM);

                if (!findRecord) {

                    if (webPageValue != null && webPageValue.length() != 0) {
                        ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_CONTACT_ITEM);

                        ps.setLong(1, Long.parseLong(cw.getId()));
                        ps.setInt(2, type);
                        ps.setString(3, webPageValue);

                        ps.executeUpdate();

                        DBTools.close(null, ps, null);
                    }

                } else {

                    if (webPageValue != null) {
                        ps = con.prepareStatement(SQL_UPDATE_FNBL_PIM_CONTACT_ITEM);

                        ps.setString(1, webPageValue);
                        ps.setLong(2, Long.parseLong(cw.getId()));
                        ps.setInt(3, type);
                    } else {
                        ps = con.prepareStatement(SQL_DELETE_FNBL_PIM_CONTACT_ITEM);

                        ps.setLong(1, Long.parseLong(cw.getId()));
                        ps.setInt(2, type);
                    }

                    ps.executeUpdate();

                    DBTools.close(null, ps, null);
                }

                DBTools.close(null, null, rs);

            }

            DBTools.close(null, ps1, null);

        }

        //
        // home address
        //
        if (homeAddressBook != null) {

            ps = con.prepareStatement(SQL_SELECT_FROM_FNBL_PIM_ADDRESS);

            ps.setLong(1, Long.parseLong(cw.getId()));
            ps.setInt(2, ADDRESS_TYPE_HOME);

            rs = ps.executeQuery();

            findRecord = rs.next();

            DBTools.close(null, ps, rs);

            street = Property.stringFrom(homeAddressBook.getStreet());
            city = Property.stringFrom(homeAddressBook.getCity());
            postalCode = Property.stringFrom(homeAddressBook.getPostalCode());
            state = Property.stringFrom(homeAddressBook.getState());
            country = Property.stringFrom(homeAddressBook.getCountry());
            postOfficeAddress = Property.stringFrom(homeAddressBook.getPostOfficeAddress());
            extendedAddress = Property.stringFrom(homeAddressBook.getExtendedAddress());

            street = replaceNewLine(StringUtils.left(street, SQL_STREET_DIM));
            city = StringUtils.left(city, SQL_CITY_DIM);
            state = StringUtils.left(state, SQL_STATE_DIM);
            postalCode = StringUtils.left(postalCode, SQL_POSTALCODE_DIM);
            country = StringUtils.left(country, SQL_COUNTRY_DIM);
            postOfficeAddress = StringUtils.left(postOfficeAddress, SQL_POSTALOFFICEADDRESS_DIM);
            extendedAddress = StringUtils.left(extendedAddress, SQL_EXTENDEDADDRESS_DIM);

            String homeLabel = Property.stringFrom(homeAddressBook.getLabel());
            if (homeLabel != null) {
                String[] label = { homeLabel, FIELD_HOME_LABEL };
                labels.add(label);
            }

            addressFields = new String[] { street, city, postalCode, country, state, postOfficeAddress,
                    extendedAddress };

            emptyAddress = hasOnlyNullContent(addressFields);

            if (!emptyAddress) {

                if (!findRecord && !hasOnlyEmptyOrNullContent(addressFields)) {
                    ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_ADDRESS);

                    ps.setLong(1, Long.parseLong(cw.getId()));
                    ps.setInt(2, ADDRESS_TYPE_HOME);
                    ps.setString(3, street);
                    ps.setString(4, city);
                    ps.setString(5, state);
                    ps.setString(6, postalCode);
                    ps.setString(7, country);
                    ps.setString(8, postOfficeAddress);
                    ps.setString(9, extendedAddress);

                    ps.executeUpdate();

                    DBTools.close(null, ps, null);

                } else {

                    sqlUpdateFunPimAddress = new StringBuffer();

                    sqlUpdateFunPimAddress.append(SQL_UPDATE_FNBL_PIM_ADDRESS_BEGIN);

                    if (street != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_STREET + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (city != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_CITY + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (state != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_STATE + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (postalCode != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_POSTAL_CODE + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (country != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_COUNTRY + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (postOfficeAddress != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_PO_BOX + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (extendedAddress != null) {
                        sqlUpdateFunPimAddress
                                .append(SQL_FIELD_EXTENDED_ADDRESS + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }

                    sqlUpdateFunPimAddress
                            .append(SQL_FIELD_TYPE + SQL_EQUALS_QUESTIONMARK + SQL_UPDATE_FNBL_PIM_ADDRESS_END);

                    ps = con.prepareStatement(sqlUpdateFunPimAddress.toString());

                    k = 1;

                    if (street != null) {
                        ps.setString(k++, street);
                    }
                    if (city != null) {
                        ps.setString(k++, city);
                    }
                    if (state != null) {
                        ps.setString(k++, state);
                    }
                    if (postalCode != null) {
                        ps.setString(k++, postalCode);
                    }
                    if (country != null) {
                        ps.setString(k++, country);
                    }
                    if (postOfficeAddress != null) {
                        ps.setString(k++, postOfficeAddress);
                    }
                    if (extendedAddress != null) {
                        ps.setString(k++, extendedAddress);
                    }

                    ps.setInt(k++, ADDRESS_TYPE_HOME);
                    ps.setLong(k++, Long.parseLong(cw.getId()));
                    ps.setInt(k++, ADDRESS_TYPE_HOME);

                    ps.executeUpdate();

                    DBTools.close(null, ps, null);
                }

            }

        }

        //
        // other address
        //
        if (otherAddressBook != null) {

            ps = con.prepareStatement(SQL_SELECT_FROM_FNBL_PIM_ADDRESS);

            ps.setLong(1, Long.parseLong(cw.getId()));
            ps.setInt(2, ADDRESS_TYPE_OTHER);

            rs = ps.executeQuery();

            findRecord = rs.next();

            DBTools.close(null, ps, rs);

            street = Property.stringFrom(otherAddressBook.getStreet());
            city = Property.stringFrom(otherAddressBook.getCity());
            postalCode = Property.stringFrom(otherAddressBook.getPostalCode());
            state = Property.stringFrom(otherAddressBook.getState());
            country = Property.stringFrom(otherAddressBook.getCountry());
            postOfficeAddress = Property.stringFrom(otherAddressBook.getPostOfficeAddress());
            extendedAddress = Property.stringFrom(otherAddressBook.getExtendedAddress());

            street = replaceNewLine(StringUtils.left(street, SQL_STREET_DIM));
            city = StringUtils.left(city, SQL_CITY_DIM);
            state = StringUtils.left(state, SQL_STATE_DIM);
            postalCode = StringUtils.left(postalCode, SQL_POSTALCODE_DIM);
            country = StringUtils.left(country, SQL_COUNTRY_DIM);
            postOfficeAddress = StringUtils.left(postOfficeAddress, SQL_POSTALOFFICEADDRESS_DIM);
            extendedAddress = StringUtils.left(extendedAddress, SQL_EXTENDEDADDRESS_DIM);

            addressFields = new String[] { street, city, postalCode, country, state, postOfficeAddress,
                    extendedAddress };

            String otherLabel = Property.stringFrom(otherAddressBook.getLabel());
            if (otherLabel != null) {
                String[] label = { otherLabel, FIELD_OTHER_LABEL };
                labels.add(label);
            }

            emptyAddress = hasOnlyNullContent(addressFields);

            if (!emptyAddress) {

                if (!findRecord && !hasOnlyEmptyOrNullContent(addressFields)) {

                    ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_ADDRESS);

                    ps.setLong(1, Long.parseLong(cw.getId()));
                    ps.setInt(2, ADDRESS_TYPE_OTHER);
                    ps.setString(3, street);
                    ps.setString(4, city);
                    ps.setString(5, state);
                    ps.setString(6, postalCode);
                    ps.setString(7, country);
                    ps.setString(8, postOfficeAddress);
                    ps.setString(9, extendedAddress);

                    ps.executeUpdate();

                    DBTools.close(null, ps, null);
                } else {

                    sqlUpdateFunPimAddress = new StringBuffer();

                    sqlUpdateFunPimAddress.append(SQL_UPDATE_FNBL_PIM_ADDRESS_BEGIN);

                    if (street != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_STREET + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (city != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_CITY + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (state != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_STATE + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (postalCode != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_POSTAL_CODE + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (country != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_COUNTRY + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (postOfficeAddress != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_PO_BOX + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (extendedAddress != null) {
                        sqlUpdateFunPimAddress
                                .append(SQL_FIELD_EXTENDED_ADDRESS + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }

                    sqlUpdateFunPimAddress
                            .append(SQL_FIELD_TYPE + SQL_EQUALS_QUESTIONMARK + SQL_UPDATE_FNBL_PIM_ADDRESS_END);

                    ps = con.prepareStatement(sqlUpdateFunPimAddress.toString());

                    k = 1;

                    if (street != null) {
                        ps.setString(k++, street);
                    }
                    if (city != null) {
                        ps.setString(k++, city);
                    }
                    if (state != null) {
                        ps.setString(k++, state);
                    }
                    if (postalCode != null) {
                        ps.setString(k++, postalCode);
                    }
                    if (country != null) {
                        ps.setString(k++, country);
                    }
                    if (postOfficeAddress != null) {
                        ps.setString(k++, postOfficeAddress);
                    }
                    if (extendedAddress != null) {
                        ps.setString(k++, extendedAddress);
                    }

                    ps.setInt(k++, ADDRESS_TYPE_OTHER);
                    ps.setLong(k++, Long.parseLong(cw.getId()));
                    ps.setInt(k++, ADDRESS_TYPE_OTHER);

                    ps.executeUpdate();

                    DBTools.close(null, ps, null);

                }

            }

        }

        //
        // work address
        //
        if (workAddressBook != null) {

            ps = con.prepareStatement(SQL_SELECT_FROM_FNBL_PIM_ADDRESS);

            ps.setLong(1, Long.parseLong(cw.getId()));
            ps.setInt(2, ADDRESS_TYPE_WORK);

            rs = ps.executeQuery();

            findRecord = rs.next();

            DBTools.close(null, ps, rs);

            street = Property.stringFrom(workAddressBook.getStreet());
            city = Property.stringFrom(workAddressBook.getCity());
            postalCode = Property.stringFrom(workAddressBook.getPostalCode());
            state = Property.stringFrom(workAddressBook.getState());
            country = Property.stringFrom(workAddressBook.getCountry());
            postOfficeAddress = Property.stringFrom(workAddressBook.getPostOfficeAddress());
            extendedAddress = Property.stringFrom(workAddressBook.getExtendedAddress());

            street = replaceNewLine(StringUtils.left(street, SQL_STREET_DIM));
            city = StringUtils.left(city, SQL_CITY_DIM);
            state = StringUtils.left(state, SQL_STATE_DIM);
            postalCode = StringUtils.left(postalCode, SQL_POSTALCODE_DIM);
            country = StringUtils.left(country, SQL_COUNTRY_DIM);
            postOfficeAddress = StringUtils.left(postOfficeAddress, SQL_POSTALOFFICEADDRESS_DIM);
            extendedAddress = StringUtils.left(extendedAddress, SQL_EXTENDEDADDRESS_DIM);

            String workLabel = Property.stringFrom(workAddressBook.getLabel());
            if (workLabel != null) {
                String[] label = { workLabel, FIELD_BUSINESS_LABEL };
                labels.add(label);
            }

            addressFields = new String[] { street, city, postalCode, country, state, postOfficeAddress,
                    extendedAddress };

            emptyAddress = hasOnlyNullContent(addressFields);

            if (!emptyAddress) {

                if (!findRecord && !hasOnlyEmptyOrNullContent(addressFields)) {

                    ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_ADDRESS);

                    ps.setLong(1, Long.parseLong(cw.getId()));
                    ps.setInt(2, ADDRESS_TYPE_WORK);
                    ps.setString(3, street);
                    ps.setString(4, city);
                    ps.setString(5, state);
                    ps.setString(6, postalCode);
                    ps.setString(7, country);
                    ps.setString(8, postOfficeAddress);
                    ps.setString(9, extendedAddress);

                    ps.executeUpdate();

                    DBTools.close(null, ps, null);

                } else {

                    sqlUpdateFunPimAddress = new StringBuffer();

                    sqlUpdateFunPimAddress.append(SQL_UPDATE_FNBL_PIM_ADDRESS_BEGIN);

                    if (street != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_STREET + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (city != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_CITY + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (state != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_STATE + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (postalCode != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_POSTAL_CODE + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (country != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_COUNTRY + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (postOfficeAddress != null) {
                        sqlUpdateFunPimAddress.append(SQL_FIELD_PO_BOX + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }
                    if (extendedAddress != null) {
                        sqlUpdateFunPimAddress
                                .append(SQL_FIELD_EXTENDED_ADDRESS + SQL_EQUALS_QUESTIONMARK_COMMA);
                    }

                    sqlUpdateFunPimAddress
                            .append(SQL_FIELD_TYPE + SQL_EQUALS_QUESTIONMARK + SQL_UPDATE_FNBL_PIM_ADDRESS_END);

                    ps = con.prepareStatement(sqlUpdateFunPimAddress.toString());

                    k = 1;

                    if (street != null) {
                        ps.setString(k++, street);
                    }
                    if (city != null) {
                        ps.setString(k++, city);
                    }
                    if (state != null) {
                        ps.setString(k++, state);
                    }
                    if (postalCode != null) {
                        ps.setString(k++, postalCode);
                    }
                    if (country != null) {
                        ps.setString(k++, country);
                    }
                    if (postOfficeAddress != null) {
                        ps.setString(k++, postOfficeAddress);
                    }
                    if (extendedAddress != null) {
                        ps.setString(k++, extendedAddress);
                    }

                    ps.setInt(k++, ADDRESS_TYPE_WORK);
                    ps.setLong(k++, Long.parseLong(cw.getId()));
                    ps.setInt(k++, ADDRESS_TYPE_WORK);

                    ps.executeUpdate();

                    DBTools.close(null, ps, null);

                }

            }

        }

        //
        // labels
        //
        if (!labels.isEmpty()) {

            ps1 = con.prepareStatement(SQL_CHECK_IF_IN_FNBL_PIM_CONTACT_ITEM);

            for (int i = 0, l = labels.size(); i < l; i++) {

                String[] label = labels.get(i);

                String labelType = label[1];

                if ((FIELD_HOME_LABEL).equals(labelType)) {
                    type = TYPE_HOME_LABEL;
                } else if ((FIELD_BUSINESS_LABEL).equals(labelType)) {
                    type = TYPE_BUSINESS_LABEL;
                } else if ((FIELD_OTHER_LABEL).equals(labelType)) {
                    type = TYPE_OTHER_LABEL;
                } else {
                    //
                    // Unknown property: saves nothing
                    //
                    continue;
                }

                ps1.setLong(1, Long.parseLong(cw.getId()));
                ps1.setInt(2, type);

                rs = ps1.executeQuery();

                findRecord = rs.next();

                String labelValue = StringUtils.left(label[0], SQL_LABEL_DIM);

                if (!findRecord) {

                    if (labelValue != null && labelValue.length() != 0) {
                        ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_CONTACT_ITEM);

                        ps.setLong(1, Long.parseLong(cw.getId()));
                        ps.setInt(2, type);
                        ps.setString(3, labelValue);

                        ps.executeUpdate();

                        DBTools.close(null, ps, null);
                    }

                } else {

                    if (labelValue != null) {
                        ps = con.prepareStatement(SQL_UPDATE_FNBL_PIM_CONTACT_ITEM);

                        ps.setString(1, labelValue);
                        ps.setLong(2, Long.parseLong(cw.getId()));
                        ps.setInt(3, type);
                    } else {
                        ps = con.prepareStatement(SQL_DELETE_FNBL_PIM_CONTACT_ITEM);

                        ps.setLong(1, Long.parseLong(cw.getId()));
                        ps.setInt(2, type);
                    }

                    ps.executeUpdate();

                    DBTools.close(null, ps, null);
                }

                DBTools.close(null, null, rs);

            }

            DBTools.close(null, ps1, null);

        }
        if (photoToSet) {
            setPhoto(con, Long.parseLong(cw.getId()), photo);
        } else if (photoToRemove) {
            deletePhoto(con, Long.parseLong(cw.getId()));
        }

    } catch (Exception e) {
        throw new DAOException("Error updating contact.", e);
    } finally {
        DBTools.close(con, ps, rs);
    }

    return cw.getId();
}

From source file:com.funambol.foundation.items.dao.PIMCalendarDAO.java

/**
 * Updates a calendar.//from ww  w. j a va 2s .  co m
 *
 * @param cw as a CalendarWrapper object. If its last update time is null,
 *          then it's set to the current time.
 * @return the UID of the contact
 * @throws DAOException
 * @throws java.lang.Exception 
 * @see CalendarWrapper
 */
public String updateItem(CalendarWrapper cw) throws DAOException, Exception {

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    RecurrencePattern rp = null;
    String id = null;
    String allDay = null;
    String body = null;
    Boolean allDayBoolean = null;
    Short busyStatus = null;
    String categories = null;
    String companies = null;
    int duration = 0;
    Date dend = null;
    short importance = 0;
    String location = null;
    Short meetingStatus = null;
    String mileage = null;
    Date replyTime = null;
    short sensitivity = 0;
    Date dstart = null;
    String subject = null;
    short recurrenceType = -1;
    int interval = 0;
    short monthOfYear = 0;
    short dayOfMonth = 0;
    String dayOfWeekMask = null;
    String priority = null;
    short instance = 0;
    String startDatePattern = null;
    String noEndDate = null;
    String endDatePattern = null;
    int occurrences = -1;
    Reminder reminder = null;
    CalendarContent c = null;
    Date completed = null;
    String complete = null;
    short percentComplete = -1;
    String folder = null;
    String dstartTimeZone = null;
    String dendTimeZone = null;
    String reminderTimeZone = null;

    StringBuffer queryUpdateFunPimCalendar = null;

    try {

        Timestamp lastUpdate = (cw.getLastUpdate() == null) ? new Timestamp(System.currentTimeMillis())
                : cw.getLastUpdate();

        c = cw.getCalendar().getCalendarContent();

        rp = c.getRecurrencePattern();

        id = cw.getId();

        boolean allDayB;
        allDayBoolean = c.getAllDay();
        if (allDayBoolean != null && allDayBoolean.booleanValue()) {
            allDayB = true;
            allDay = "1";
        } else {
            allDayB = false;
            allDay = "0";
        }

        body = Property.stringFrom(c.getDescription());

        if (c.getBusyStatus() != null) {
            busyStatus = new Short(c.getBusyStatus().shortValue());
        }
        categories = Property.stringFrom(c.getCategories());
        companies = Property.stringFrom(c.getOrganizer());
        if (c.getPriority() != null) {
            priority = c.getPriority().getPropertyValueAsString();
            if (priority != null && priority.length() > 0) {
                importance = Short.parseShort(priority);
            } // priority / importance ??
        }
        location = Property.stringFrom(c.getLocation());
        meetingStatus = c.getMeetingStatus();
        if (c.getMileage() != null) {
            mileage = String.valueOf(c.getMileage());
        }

        reminder = c.getReminder();

        String rt = null;
        if (c instanceof Event) {
            rt = Property.stringFrom(((Event) c).getReplyTime());
            replyTime = getDateFromString(allDayB, // @todo or false?
                    rt, "000000");
        }

        if (c.getAccessClass() != null) {

            String classEvent = null;
            classEvent = c.getAccessClass().getPropertyValueAsString();

            if (classEvent != null && classEvent.length() > 0) {
                sensitivity = Short.parseShort(classEvent);
            }
        }

        if (c.getSummary() != null) {

            subject = c.getSummary().getPropertyValueAsString();

        } else if (body != null && body.length() > 0) {

            String tmpBody = body;

            if (tmpBody.indexOf('.') != -1) {
                tmpBody = tmpBody.substring(0, tmpBody.indexOf('.'));
            }

            if (tmpBody.length() > SQL_SUBJECT_DIM) {
                tmpBody = tmpBody.substring(0, SQL_SUBJECT_DIM);
            }

            subject = tmpBody;
        }

        folder = Property.stringFrom(c.getFolder());
        dstartTimeZone = timeZoneFrom(c.getDtStart());
        dendTimeZone = timeZoneFrom(c.getDtEnd());
        reminderTimeZone = timeZoneFrom(c.getReminder());

        String sd = null;
        if (c.getDtStart() != null) {
            sd = c.getDtStart().getPropertyValueAsString();
            dstart = getDateFromString(allDayB, sd, "000000");
        }

        String ed = null;
        if ((sd != null && sd.length() > 0) || (c.getDtEnd() != null)) {

            ed = c.getDtEnd().getPropertyValueAsString();

            //
            // Fix for Siemens S56 end date issue only for event
            // @todo: verify if is really need to do this
            // Due to this fix, in  method getTwinItems() if the incoming 
            // Event has an empty EndDate we seek into the db for Events 
            // with EndDate equal to the StartDate value.
            //
            if (c instanceof Event) {
                if (ed == null || ed.length() == 0) {
                    ed = sd;
                }
            }

            dend = getDateFromString(allDayB, ed, "235900");
        }

        if (rp != null) {

            recurrenceType = rp.getTypeId();
            interval = rp.getInterval();
            monthOfYear = rp.getMonthOfYear();
            dayOfMonth = rp.getDayOfMonth();
            dayOfWeekMask = String.valueOf(rp.getDayOfWeekMask());
            instance = rp.getInstance();
            startDatePattern = rp.getStartDatePattern();

            boolean noEndDateB = rp.isNoEndDate();
            if (noEndDateB) {
                noEndDate = "1";
            } else {
                noEndDate = "0";
            }

            endDatePattern = rp.getEndDatePattern();
            occurrences = rp.getOccurrences();
        }

        String dc = null;
        if (c instanceof Task) {

            Task t = (Task) c;
            if (t.getDateCompleted() != null) {
                dc = t.getDateCompleted().getPropertyValueAsString();
                completed = getDateFromString(allDayB, dc, "000000");
            }

            complete = Property.stringFrom(t.getComplete());
            if (complete != null && "1".equals(complete)) {
                percentComplete = 100;
            } else {
                try {
                    percentComplete = Short.parseShort(Property.stringFrom(t.getPercentComplete()));
                    if (percentComplete < 0 || percentComplete > 100) {
                        throw new NumberFormatException("A percentage can't be " + percentComplete);
                    }
                } catch (NumberFormatException nfe) {
                    percentComplete = -1; // the short must go on
                }
            }

            meetingStatus = getTaskStatus(t);

        }

        queryUpdateFunPimCalendar = new StringBuffer();

        queryUpdateFunPimCalendar.append(SQL_UPDATE_FNBL_PIM_CALENDAR_BEGIN).append(SQL_FIELD_LAST_UPDATE)
                .append(SQL_EQUALS_QUESTIONMARK_COMMA);

        if (allDayBoolean != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_ALL_DAY).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        if (body != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_BODY).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        queryUpdateFunPimCalendar.append(SQL_FIELD_BUSY_STATUS).append(SQL_EQUALS_QUESTIONMARK_COMMA);

        if (categories != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_CATEGORIES).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        if (companies != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_COMPANIES).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        queryUpdateFunPimCalendar.append(SQL_FIELD_DURATION).append(SQL_EQUALS_QUESTIONMARK_COMMA);

        if (dend != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_DATE_END).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        } else if (ed != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_DATE_END).append(SQL_EQUALS_NULL_COMMA);
        }

        if (priority != null && priority.length() > 0) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_IMPORTANCE).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        if (location != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_LOCATION).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        if (meetingStatus != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_MEETING_STATUS).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        if (mileage != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_MILEAGE).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        if (reminder != null) {
            if (reminder.isActive()) {
                queryUpdateFunPimCalendar.append(SQL_FIELD_REMINDER).append(SQL_EQUALS_ONE_COMMA);
            } else {
                queryUpdateFunPimCalendar.append(SQL_FIELD_REMINDER).append(SQL_EQUALS_ZERO_COMMA);
            }
            queryUpdateFunPimCalendar.append(SQL_FIELD_REMINDER_TIME).append(SQL_EQUALS_QUESTIONMARK_COMMA);
            queryUpdateFunPimCalendar.append(SQL_FIELD_REMINDER_REPEAT_COUNT)
                    .append(SQL_EQUALS_QUESTIONMARK_COMMA);
            queryUpdateFunPimCalendar.append(SQL_FIELD_REMINDER_SOUND_FILE)
                    .append(SQL_EQUALS_QUESTIONMARK_COMMA);
            queryUpdateFunPimCalendar.append(SQL_FIELD_REMINDER_OPTIONS).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        if (replyTime != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_REPLY_TIME).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        } else if (rt != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_REPLY_TIME).append(SQL_EQUALS_NULL_COMMA);
        }

        queryUpdateFunPimCalendar.append(SQL_FIELD_SENSITIVITY).append(SQL_EQUALS_QUESTIONMARK_COMMA);

        if (dstart != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_DATE_START).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        } else if (sd != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_DATE_START).append(SQL_EQUALS_NULL_COMMA);
        }

        if (subject != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_SUBJECT).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        queryUpdateFunPimCalendar.append(SQL_FIELD_RECURRENCE_TYPE).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        queryUpdateFunPimCalendar.append(SQL_FIELD_INTERVAL).append(SQL_EQUALS_QUESTIONMARK_COMMA);

        queryUpdateFunPimCalendar.append(SQL_FIELD_MONTH_OF_YEAR).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        queryUpdateFunPimCalendar.append(SQL_FIELD_DAY_OF_MONTH).append(SQL_EQUALS_QUESTIONMARK_COMMA);

        if (dayOfWeekMask != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_DAY_OF_WEEK_MASK).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        queryUpdateFunPimCalendar.append(SQL_FIELD_INSTANCE).append(SQL_EQUALS_QUESTIONMARK_COMMA);

        if (startDatePattern != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_START_DATE_PATTERN)
                    .append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        if (noEndDate != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_NO_END_DATE).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        if (endDatePattern != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_END_DATE_PATTERN).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        } else {
            //
            // When NoEndDate is true, the PatterEndDate must be empty.
            //
            if ("1".equals(noEndDate)) {
                queryUpdateFunPimCalendar.append(SQL_FIELD_END_DATE_PATTERN).append(SQL_EQUALS_EMPTY_COMMA);
            }
        }

        queryUpdateFunPimCalendar.append(SQL_FIELD_OCCURRENCES).append(SQL_EQUALS_QUESTIONMARK_COMMA);

        if (completed != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_COMPLETED).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        } else if (dc != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_COMPLETED).append(SQL_EQUALS_NULL_COMMA);
        }

        if (percentComplete != -1) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_PERCENT_COMPLETE).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        } else if ("0".equals(complete)) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_PERCENT_COMPLETE).append(SQL_PERCENT_COMPLETE_FORMULA);
        }

        if (folder != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_FOLDER).append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        if (dstartTimeZone != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_START_DATE_TIME_ZONE)
                    .append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        if (dendTimeZone != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_END_DATE_TIME_ZONE)
                    .append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        if (reminderTimeZone != null) {
            queryUpdateFunPimCalendar.append(SQL_FIELD_REMINDER_TIME_ZONE)
                    .append(SQL_EQUALS_QUESTIONMARK_COMMA);
        }

        queryUpdateFunPimCalendar.append(SQL_FIELD_STATUS).append(SQL_EQUALS_QUESTIONMARK)
                .append(SQL_UPDATE_FNBL_PIM_CALENDAR_END);

        con = getUserDataSource().getRoutedConnection(userId);

        ps = con.prepareStatement(queryUpdateFunPimCalendar.toString());

        int k = 1;

        //
        // lastUpdate
        //
        ps.setLong(k++, lastUpdate.getTime());
        //
        // allDay
        //
        if (allDayBoolean != null) {
            ps.setString(k++, allDay);
        }
        //
        // body
        //
        if (body != null) {
            if (body.length() > SQL_BODY_DIM) {
                body = body.substring(0, SQL_BODY_DIM);
            }
            ps.setString(k++, body);
        }
        //
        // busy status
        //
        if (busyStatus != null) {
            ps.setShort(k++, busyStatus.shortValue());
        } else {
            ps.setNull(k++, Types.SMALLINT);
        }
        //
        // categories
        //
        if (categories != null) {
            if (categories.length() > SQL_CATEGORIES_DIM) {
                categories = categories.substring(0, SQL_CATEGORIES_DIM);
            }
            ps.setString(k++, categories);
        }
        //
        // companies
        //
        if (companies != null) {
            if (companies.length() > SQL_COMPANIES_DIM) {
                companies = companies.substring(0, SQL_COMPANIES_DIM);
            }
            ps.setString(k++, companies);
        }
        //
        // duration
        //
        ps.setInt(k++, duration);
        //
        // date End
        //
        if (dend != null) {
            ps.setTimestamp(k++, new Timestamp(dend.getTime()));
        }
        //
        // priority
        //
        if (priority != null && priority.length() > 0) {
            ps.setShort(k++, importance);
        }
        //
        // location
        //
        if (location != null) {
            if (location.length() > SQL_COMPANIES_DIM) {
                location = location.substring(0, SQL_LOCATION_DIM);
            }
            ps.setString(k++, location);
        }
        //
        // meeting status
        //
        if (meetingStatus != null) {
            ps.setShort(k++, meetingStatus.shortValue());
        }
        //
        // mileage
        //
        if (mileage != null) {
            ps.setString(k++, mileage);
        }
        //
        // reminder
        //
        if (reminder != null) {
            if (reminder.isActive()) {
                ps.setTimestamp(k++, getReminderTime(dstart, reminder));
                ps.setInt(k++, reminder.getRepeatCount());
                String soundFileValue = reminder.getSoundFile();
                if (soundFileValue != null && soundFileValue.length() > SQL_SOUNDFILE_DIM) {

                    soundFileValue = soundFileValue.substring(0, SQL_SOUNDFILE_DIM);
                }
                ps.setString(k++, soundFileValue);
                ps.setInt(k++, reminder.getOptions());
            } else {
                ps.setNull(k++, Types.TIMESTAMP);
                ps.setInt(k++, 0);
                ps.setString(k++, null);
                ps.setInt(k++, 0);
            }
        }
        //
        // reply time
        //
        if (replyTime != null) {
            ps.setTimestamp(k++, new Timestamp(replyTime.getTime()));
        }
        //
        // sensitivity
        //
        ps.setShort(k++, sensitivity);
        //
        // date start
        //
        if (dstart != null) {
            ps.setTimestamp(k++, new Timestamp(dstart.getTime()));
        }
        //
        // subject
        //
        if (subject != null) {
            if (subject.length() > SQL_SUBJECT_DIM) {
                subject = subject.substring(0, SQL_SUBJECT_DIM);
            }
            ps.setString(k++, subject);
        }
        //
        // recurrence Type
        //
        ps.setShort(k++, recurrenceType);
        //
        // interval
        //
        ps.setInt(k++, interval);
        //
        // month of year
        //
        ps.setShort(k++, monthOfYear);
        //
        // day of month
        //
        ps.setShort(k++, dayOfMonth);
        //
        // day of week mask
        //
        if (dayOfWeekMask != null) {
            if (dayOfWeekMask.length() > SQL_DAYOFWEEKMASK_DIM) {
                dayOfWeekMask = dayOfWeekMask.substring(0, SQL_DAYOFWEEKMASK_DIM);
            }
            ps.setString(k++, dayOfWeekMask);
        }
        //
        // instance
        //
        ps.setShort(k++, instance);
        //
        // start date pattern
        //
        if (startDatePattern != null) {
            if (startDatePattern.length() > SQL_STARTDATEPATTERN_DIM) {
                startDatePattern = startDatePattern.substring(0, SQL_STARTDATEPATTERN_DIM);
            }
            ps.setString(k++, startDatePattern);
        }
        //
        // no end date
        //
        if (noEndDate != null) {
            ps.setString(k++, noEndDate);
        }
        //
        // end date pattern
        //
        if (endDatePattern != null) {
            if (endDatePattern.length() > SQL_ENDDATEPATTERN_DIM) {
                endDatePattern = endDatePattern.substring(0, SQL_ENDDATEPATTERN_DIM);
            }
            ps.setString(k++, endDatePattern);
        }
        //
        // occurrences
        //
        ps.setInt(k++, occurrences);
        //
        // completed
        //
        if (completed != null) {
            ps.setTimestamp(k++, new Timestamp(completed.getTime()));
        }
        //
        // percent completed
        //
        if (percentComplete != -1) {
            ps.setShort(k++, percentComplete);
        }
        //
        // folder
        //
        if (folder != null) {
            if (folder.length() > SQL_FOLDER_DIM) {
                folder = folder.substring(0, SQL_FOLDER_DIM);
            }
            ps.setString(k++, folder);
        }
        //
        // time zones
        //
        if (dstartTimeZone != null) {
            if (dstartTimeZone.length() > SQL_TIME_ZONE_DIM) {
                dstartTimeZone = dstartTimeZone.substring(0, SQL_TIME_ZONE_DIM);
            }
            ps.setString(k++, dstartTimeZone);
        }
        if (dendTimeZone != null) {
            if (dendTimeZone.length() > SQL_TIME_ZONE_DIM) {
                dendTimeZone = dendTimeZone.substring(0, SQL_TIME_ZONE_DIM);
            }
            ps.setString(k++, dendTimeZone);
        }
        if (reminderTimeZone != null) {
            if (reminderTimeZone.length() > SQL_TIME_ZONE_DIM) {
                reminderTimeZone = reminderTimeZone.substring(0, SQL_TIME_ZONE_DIM);
            }
            ps.setString(k++, reminderTimeZone);
        }
        //
        // status
        //
        ps.setString(k++, String.valueOf('U'));
        //
        // id
        //
        ps.setLong(k++, Long.parseLong(id));
        //
        // user id
        //
        ps.setString(k++, cw.getUserId());

        ps.executeUpdate();

        DBTools.close(null, ps, null);

        ps = con.prepareStatement(SQL_DELETE_CALENDAR_EXCEPTIONS_BY_CALENDAR);

        ps.setLong(1, Long.parseLong(id));

        ps.executeUpdate();

        DBTools.close(null, ps, null);

        if (recurrenceType != -1) {
            List<ExceptionToRecurrenceRule> exceptions = rp.getExceptions();
            for (ExceptionToRecurrenceRule etrr : exceptions) {

                ps = con.prepareStatement(SQL_INSERT_INTO_FNBL_PIM_CALENDAR_EXCEPTION);

                ps.setLong(1, Long.parseLong(id));
                ps.setString(2, (etrr.isAddition() ? "1" : "0"));
                ps.setTimestamp(3,
                        new Timestamp(getDateFromString(allDayB, etrr.getDate(), "000000").getTime()));

                ps.executeUpdate();

                DBTools.close(null, ps, null);
            }
        }

    } catch (Exception e) {
        throw new DAOException("Error updating a calendar item: " + e.getMessage());
    } finally {
        DBTools.close(con, ps, rs);
    }

    return id;
}