Search in sources :

Example 21 with NameValuePairs

use of db.NameValuePairs in project common by zenlunatics.

the class Admin method importFile.

// --------------------------------------------------------------------------
private void importFile(Request request) throws IOException {
    String step = request.getParameter("step");
    HTMLWriter writer = request.writer;
    if (step == null) {
        String id = writer.componentOpen(null, null);
        web.Form form = componentFormOpen(id, request).setSubmitText("Start");
        writer.hiddenInput("action", "import file");
        writer.hiddenInput("step", "2");
        form.rowOpen("filename");
        writer.textInput("filename", null, null);
        form.close();
        writer.tagClose();
    } else if ("2".equals(step)) {
        String filename = request.getParameter("filename");
        BufferedReader br = new BufferedReader(new FileReader(request.site.getBaseFilePath().append(filename).toString()));
        String l = br.readLine();
        br.close();
        writer.write("$('").write(request.getParameter("id")).write("').innerHTML='");
        web.Form form = componentFormOpen(null, request).setSubmitText("Import");
        writer.hiddenInput("action", "import file");
        writer.hiddenInput("filename", filename);
        writer.hiddenInput("step", "3");
        form.rowOpen("table");
        writer.textInput("table", null, null);
        form.rowOpen("columns");
        Table table = new Table(writer);
        String[] types = new String[] { "INT", "VARCHAR" };
        String[] columns = l.split("\t");
        for (String column : columns) {
            table.tr().td();
            writer.checkbox("col" + column, column, null, true, false);
            table.td();
            writer.radioButtons(column, types, null, null);
        }
        table.close();
        form.close();
        writer.write('\'');
    } else if ("3".equals(step)) {
        StringBuilder sb = new StringBuilder();
        Enumeration<String> parameter_names = request.getParameterNames();
        while (parameter_names.hasMoreElements()) {
            String parameter_name = parameter_names.nextElement();
            if (parameter_name.startsWith("col")) {
                if (sb.length() > 0)
                    sb.append(',');
                String column = parameter_name.substring(3);
                sb.append(column);
                sb.append(' ');
                sb.append(request.getParameter(column));
            }
        }
        String table = request.getParameter("table");
        request.db.createTable(table, sb.toString());
        String filename = request.getParameter("filename");
        BufferedReader br = new BufferedReader(new FileReader(request.site.getBaseFilePath().append(filename).toString()));
        String l = br.readLine();
        String[] columns = l.split("\t");
        l = br.readLine();
        NameValuePairs name_value_pairs = new NameValuePairs();
        while (l != null) {
            String[] values = l.split("\t");
            for (String column : columns) if (request.getParameter(column) != null)
                name_value_pairs.set(column, values[Util.indexOf(columns, column)]);
            request.db.insert(table, name_value_pairs);
            l = br.readLine();
        }
        br.close();
    }
}
Also used : HTMLWriter(web.HTMLWriter) Table(web.Table) JDBCTable(db.JDBCTable) NameValuePairs(db.NameValuePairs) Form(db.Form) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 22 with NameValuePairs

use of db.NameValuePairs in project common by zenlunatics.

the class EventProvider method sendReminders.

// --------------------------------------------------------------------------
@AdminTask({ "nightly" })
public void sendReminders(LocalDateTime now, boolean nightly, Site site, DBConnection db) {
    Mail mail = new Mail(site);
    ArrayList<Integer> delete_ids = new ArrayList<Integer>();
    String reminders_table = m_events_table + "_reminders";
    try {
        StringBuilder columns = new StringBuilder();
        columns.append(m_events_table).append(".id,").append(reminders_table).append(".id AS reminder_id,").append(m_events_table).append("_id,last_sent,note,email,num,unit,before,date,notes");
        if (m_events_have_event)
            columns.append(",event");
        if (m_events_can_repeat)
            columns.append(",repeat,end_date,repeat_days");
        if (m_events_have_start_time)
            columns.append(",start_time,end_time");
        if (m_events_are_owned)
            columns.append(",").append(m_events_table).append("._owner_");
        Select query = new Select(columns.toString()).from(m_events_table).joinOn(reminders_table, "(" + m_events_table + ".id=" + reminders_table + "." + m_events_table + "_id OR " + reminders_table + "." + m_events_table + "_id IS NULL)").where(nightly ? "(unit='day' OR unit='week')" : "(unit='hour' OR unit='minute')");
        andFilters(query);
        ResultSet rs = db.select(query);
        Calendar calendar = site.newCalendar();
        while (rs.next()) {
            int num = rs.getInt("num");
            String unit = rs.getString("unit");
            boolean before = rs.getBoolean("before");
            int num_days = numDays(num, unit, before);
            LocalDate reminder_date = site.newLocalDate().minusDays(num_days);
            Event event = new Event(this, calendar, null);
            event.load(rs, site);
            if (event.hasEndedBy(reminder_date)) {
                int id = rs.getInt(3);
                if (!rs.wasNull())
                    delete_ids.add(id);
            } else {
                boolean occurs_on = false;
                boolean last_event = false;
                if (m_events_can_repeat) {
                    String repeat_days = rs.getString("repeat_days");
                    if ("first event".equals(repeat_days))
                        occurs_on = reminder_date.isEqual(rs.getDate("date").toLocalDate());
                    else if ("last event".equals(repeat_days)) {
                        occurs_on = reminder_date.isEqual(rs.getDate("end_date").toLocalDate());
                        last_event = true;
                    } else
                        occurs_on = event.occursOn(reminder_date);
                } else
                    occurs_on = event.occursOn(reminder_date);
                if (occurs_on) {
                    Date last_sent = rs.getDate("last_sent");
                    if (last_sent == null || !last_sent.toLocalDate().equals(now.toLocalDate())) {
                        if (m_events_have_start_time && (unit.equals("hour") || unit.equals("minute"))) {
                            LocalTime time = before ? rs.getTime("start_time").toLocalTime() : rs.getTime("end_time").toLocalTime();
                            if (unit.equals("hour"))
                                if (before)
                                    time = time.minusHours(num);
                                else
                                    time = time.plusHours(num);
                            else if (before)
                                time = time.minusMinutes(num);
                            else
                                time = time.plusMinutes(num);
                            if (now.toLocalTime().isBefore(time))
                                continue;
                        }
                        String recipient = rs.getString("email");
                        sendReminder(mail, recipient, event, rs.getString("note"), reminder_date, num_days, last_event, site, db);
                        if (!event.isRepeating()) {
                            int id = rs.getInt(3);
                            if (!rs.wasNull()) {
                                delete_ids.add(id);
                                continue;
                            }
                        }
                        db.update(reminders_table, new NameValuePairs().setDate("last_sent", now.toLocalDate()), rs.getInt("reminder_id"));
                    }
                }
            }
        }
        rs.getStatement().close();
    } catch (Exception e) {
        System.out.println("EventProvider.sendReminders");
        e.printStackTrace(System.out);
    }
    for (Integer id : delete_ids) db.deleteById(reminders_table, id);
}
Also used : FilePathStringBuilder(web.FilePathStringBuilder) LocalTime(java.time.LocalTime) NameValuePairs(db.NameValuePairs) Calendar(java.util.Calendar) ICalendar(biweekly.ICalendar) ArrayList(java.util.ArrayList) LocalDate(java.time.LocalDate) LocalDate(java.time.LocalDate) Date(java.sql.Date) SQLException(java.sql.SQLException) IOException(java.io.IOException) Mail(email.Mail) Select(db.Select) ResultSet(java.sql.ResultSet) VEvent(biweekly.component.VEvent) AdminTask(web.AdminTask)

Example 23 with NameValuePairs

use of db.NameValuePairs in project common by zenlunatics.

the class Module method store.

// --------------------------------------------------------------------------
public void store(DBConnection db) {
    NameValuePairs name_value_pairs = new NameValuePairs();
    name_value_pairs.set("class", getClass().getName());
    name_value_pairs.set("name", m_name);
    DBObject.store(this, "_modules_", name_value_pairs, db);
}
Also used : NameValuePairs(db.NameValuePairs)

Aggregations

NameValuePairs (db.NameValuePairs)23 Select (db.Select)5 Request (app.Request)4 JDBCColumn (db.JDBCColumn)4 ViewDef (db.ViewDef)4 Column (db.column.Column)3 LookupColumn (db.column.LookupColumn)3 IOException (java.io.IOException)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 Form (db.Form)2 LinkValueRenderer (db.LinkValueRenderer)2 ManyToMany (db.ManyToMany)2 OneToMany (db.OneToMany)2 View (db.View)2 AccessPolicy (db.access.AccessPolicy)2 RecordOwnerAccessPolicy (db.access.RecordOwnerAccessPolicy)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2