Example usage for java.sql PreparedStatement setShort

List of usage examples for java.sql PreparedStatement setShort

Introduction

In this page you can find the example usage for java.sql PreparedStatement setShort.

Prototype

void setShort(int parameterIndex, short x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java short value.

Usage

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

/**
 * Updates a calendar./*w  w w .j av a 2 s .c o  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;
}