Search in sources :

Example 1 with DBConnection

use of db.DBConnection in project common by zenlunatics.

the class EventProvider method _newViewDef.

// --------------------------------------------------------------------------
@Override
public ViewDef _newViewDef(String name, Site site) {
    if (name.equals(m_name)) {
        ViewDef view_def = new EventViewDef(m_name, m_support_registrations);
        view_def.addFormHook(new FormHook() {

            @Override
            public void after(View view, View.Mode mode, boolean printer_friendly, Request request) throws IOException {
                writeAutomaticReminders(view, request);
            }
        });
        if (m_access_policy != null)
            view_def.setAccessPolicy(m_access_policy);
        view_def.setDialogModes(View.Mode.ADD_FORM, View.Mode.EDIT_FORM, View.Mode.READ_ONLY_FORM);
        view_def.setFrom(m_events_table);
        view_def.setRecordName("Event");
        ArrayList<String> column_names_form = new ArrayList<String>();
        DBConnection db = new DBConnection(site);
        for (int i = 0; i < m_filter_columns.size(); i += 2) {
            column_names_form.add(m_filter_columns.get(i));
            view_def.setColumn(new Column(m_filter_columns.get(i)).setDefaultValue(m_filter_columns.get(i + 1)).setIsHidden(true));
        }
        db.close();
        column_names_form.add("date");
        view_def.setColumn(new Column("date").setDisplayName(m_start_date_label).setIsRequired(true));
        if (m_events_have_start_time) {
            column_names_form.add("start_time");
            column_names_form.add("end_time");
        }
        if (m_events_have_event) {
            column_names_form.add("event");
            view_def.setColumn(new Column("event").setIsRequired(true));
        }
        column_names_form.add("notes");
        if (m_events_can_repeat) {
            column_names_form.add("repeat");
            view_def.setColumn(new Column("repeat").setInputAndValueRenderers(new SelectRenderer(s_repeat_periods).setOnChange("var i=this.selectedIndex;var d=document.querySelector('#date_row input');var ed=document.querySelector('#end_date_row input');if(i==0)ed.value='';else if(i==1){if(ed.value=='')ed.value=d.value;}else if(ed.value==d.value)ed.value='';$('end_date_row').style.display=i==0?'none':null;return false"), true));
            column_names_form.add("end_date");
            view_def.setColumn(new Column("end_date").setDisplayName(m_end_date_label));
        }
        if (m_events_have_category) {
            column_names_form.add(m_events_table + "_categories_id");
            m_categories.setAllowEditing(!m_only_admins_can_edit_categories);
            view_def.setColumn(m_categories.newColumn(m_events_table + "_categories_id").setDisplayName("category"));
        }
        if (m_events_have_location) {
            String locations_table = getLocationsTable();
            column_names_form.add(locations_table + "_id");
            m_locations.setAllowEditing(!m_only_admins_can_edit_locations);
            view_def.setColumn(m_locations.newColumn(locations_table + "_id").setDisplayName("location"));
        }
        if (m_events_have_color) {
            column_names_form.add("color");
            view_def.setColumn(new ColorColumn("color"));
        }
        if (m_support_reminders)
            view_def.addRelationshipDef(new OneToMany(m_name + "_reminders").setSpanFormCols(false));
        if (m_support_registrations) {
            column_names_form.add("register_people");
            view_def.setColumn(new Column("register_people").setDisplayName("allow people to register for this " + getEventCrumb()));
        }
        if (m_events_are_owned) {
            if (m_access_policy == null)
                view_def.setAccessPolicy(new Or(new RecordOwnerAccessPolicy().add().delete().edit(), new RoleAccessPolicy("calendar editor")));
            column_names_form.add("_owner_");
            LookupColumn owner_column = new PersonColumn("_owner_", false, site.getPeopleFilter()).setDefaultToUserId().setDisplayName("posted by");
            if (m_role == null)
                owner_column.setIsReadOnly(true);
            view_def.setColumn(owner_column);
        }
        column_names_form.add("_timestamp_");
        view_def.setColumn(new Column("_timestamp_").setDisplayName("added").setIsReadOnly(true).setShowOnAddForm(false));
        view_def.setColumnNamesForm(column_names_form.toArray(new String[column_names_form.size()]));
        return view_def;
    }
    if (name.equals(getCategoriesTable()))
        return m_categories.addHooks(new ViewDef(name).setColumn(new ColorColumn("color")));
    if (name.equals(getLocationsTable()))
        return m_locations.newViewDef(name, site).setDefaultOrderBy("lower(text)").setDialogModes(View.Mode.ADD_FORM, View.Mode.EDIT_FORM, View.Mode.READ_ONLY_FORM).setRecordName("Location").setColumn(new Column("text").setDisplayName("location").setIsRequired(true));
    if (name.equals(m_name + "_registrations"))
        return new ViewDef(name).setDialogModes(View.Mode.ADD_FORM, View.Mode.EDIT_FORM, View.Mode.READ_ONLY_FORM).setFrom(m_events_table + "_registrations").setRecordName("Registration").setColumnNamesTableAndForm(new String[] { "person", "note" }).setColumn(new LookupColumn("person", "people", "first,last").setDefaultToUserId().setFilter(site.getPeopleFilter()));
    if (name.equals(m_name + "_reminders")) {
        RecordOwnerAccessPolicy access_policy = (RecordOwnerAccessPolicy) new RecordOwnerAccessPolicy().add().delete().edit();
        return new ViewDef(name).setAccessPolicy(access_policy).setDialogModes(View.Mode.ADD_FORM, View.Mode.EDIT_FORM, View.Mode.READ_ONLY_FORM).setFrom(m_events_table + "_reminders").setRecordName("Reminder").setColumnNamesForm(new String[] { "before", "note", "email" }).setColumnNamesTable(new String[] { "email", "before" }).setColumn(new EmailColumn("email") {

            @Override
            public String getDefaultValue(Request request) {
                return request.getUser().getEmail();
            }

            @Override
            public String validate(String table, String value, int id, Request request) {
                if ("poster".equals(value))
                    return null;
                return super.validate(table, value, id, request);
            }
        }.setDisplayName("send to").setIsRequired(true)).setColumn(new ReminderWhenColumn("before", m_name, m_events_table, m_events_have_start_time, m_events_can_repeat, false).setDisplayName("when")).setColumn(access_policy.getColumn("person", site));
    }
    if (name.equals(m_name + "_reminders auto"))
        return new ViewDef(name).setBaseFilter(m_events_table + "_id IS NULL").setDialogModes(View.Mode.ADD_FORM, View.Mode.EDIT_FORM, View.Mode.READ_ONLY_FORM).setFrom(m_events_table + "_reminders").setRecordName("Reminder").setColumnNamesForm(new String[] { "before", "note", "email" }).setColumnNamesTable(new String[] { "email", "before" }).setColumn(new EmailColumn("email") {

            @Override
            public String validate(String table, String value, int id, Request request) {
                if ("poster".equals(value))
                    return null;
                return super.validate(table, value, id, request);
            }
        }.setDisplayName("send to").setIsRequired(true).setTitle("Enter the special value \"poster\" to send to the event poster")).setColumn(new ReminderWhenColumn("before", m_name, m_events_table, m_events_have_start_time, m_events_can_repeat, true).setDisplayName("when"));
    return null;
}
Also used : DBConnection(db.DBConnection) Or(db.access.Or) EmailColumn(db.column.EmailColumn) ArrayList(java.util.ArrayList) ViewDef(db.ViewDef) PersonColumn(app.PersonColumn) RecordOwnerAccessPolicy(db.access.RecordOwnerAccessPolicy) JDBCColumn(db.JDBCColumn) LookupColumn(db.column.LookupColumn) EmailColumn(db.column.EmailColumn) PersonColumn(app.PersonColumn) Column(db.column.Column) ColorColumn(db.column.ColorColumn) RoleAccessPolicy(db.access.RoleAccessPolicy) ColorColumn(db.column.ColorColumn) Request(app.Request) IOException(java.io.IOException) OneToMany(db.OneToMany) View(db.View) LookupColumn(db.column.LookupColumn) SelectRenderer(db.SelectRenderer) FormHook(db.FormHook)

Example 2 with DBConnection

use of db.DBConnection in project common by zenlunatics.

the class Site method nightly.

// --------------------------------------------------------------------------
public void nightly(LocalDateTime now) {
    if (m_base_directory.startsWith("/Users"))
        return;
    if (now.getDayOfMonth() == m_settings.getInt("nightly date"))
        return;
    // log("Site.nightly start " + now.toString());
    DBConnection db = new DBConnection(this);
    try {
        for (Module module : m_modules.values()) if (!(module instanceof SiteModule) || ((SiteModule) module).isActive())
            module.nightly(now, this, db);
        m_settings.set("nightly date", now.getDayOfMonth(), db);
        db.close();
        backup();
    } catch (RuntimeException e) {
        db.close();
        log(e);
    }
// log("Site.nightly end");
}
Also used : DBConnection(db.DBConnection)

Example 3 with DBConnection

use of db.DBConnection in project common by zenlunatics.

the class MailLists method readAndSend.

// --------------------------------------------------------------------------
@AdminTask
public void readAndSend() {
    DBConnection db = new DBConnection(m_site);
    Session session = Session.getInstance(new Properties(), null);
    new File(m_site.getBaseFilePath().append("inbox").toString()).listFiles(new FileFilter() {

        @Override
        public boolean accept(File file) {
            if (file.isDirectory() && file.getName().charAt(0) != '.')
                readAndSend(file, session, db);
            return false;
        }
    });
    db.close();
}
Also used : DBConnection(db.DBConnection) Properties(java.util.Properties) FileFilter(java.io.FileFilter) File(java.io.File) Session(javax.mail.Session) AdminTask(web.AdminTask)

Example 4 with DBConnection

use of db.DBConnection in project common by zenlunatics.

the class FiveMinuteThread method run.

// --------------------------------------------------------------------------
@Override
public void run() {
    // m_site.log("Starting FiveMinuteThread");
    while (true) {
        try {
            MailLists mail_lists = (MailLists) m_site.getModule("MailLists");
            if (mail_lists != null)
                mail_lists.checkForMail();
            LocalDateTime date_time = m_site.newLocalDateTime();
            DBConnection db = new DBConnection(m_site);
            for (Module module : m_site.getModules(EventProvider.class)) if (((EventProvider) module).supportsReminders())
                ((EventProvider) module).sendReminders(date_time, false, m_site, db);
            db.close();
        } catch (RuntimeException e) {
            m_site.log(e);
        }
        try {
            // 5 minutes
            Thread.sleep(5 * 60 * 1000);
        } catch (InterruptedException e) {
            System.out.println("FiveMinuteThread sleep exception");
            System.out.println(e.toString());
            m_site = null;
            return;
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) DBConnection(db.DBConnection) EventProvider(calendar.EventProvider) Module(app.Module)

Example 5 with DBConnection

use of db.DBConnection in project common by zenlunatics.

the class MailLists method checkForMail.

// --------------------------------------------------------------------------
@AdminTask
public void checkForMail() {
    if (m_site.getBaseFilePath().toString().startsWith("/Users"))
        return;
    if (m_read_and_send)
        readAndSend();
    else {
        if (m_lists != null && m_lists.size() == 0)
            return;
        DBConnection db = new DBConnection(m_site);
        if (m_lists == null)
            m_lists = db.readRows(new Select("name,username,host,password,id").from("mail_lists").where("active"));
        try {
            for (String[] list : m_lists) if (list[2] != null && list[3] != null)
                fetchAndSend(list[0], accountName(list[1], Integer.parseInt(list[4])), list[2], list[3], db);
        } catch (Exception e) {
            System.out.println("fetchAndSendAll");
            m_site.log(e);
        }
        db.close();
    }
}
Also used : DBConnection(db.DBConnection) Select(db.Select) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SQLException(java.sql.SQLException) AuthenticationFailedException(javax.mail.AuthenticationFailedException) MailConnectException(com.sun.mail.util.MailConnectException) IOException(java.io.IOException) AdminTask(web.AdminTask)

Aggregations

DBConnection (db.DBConnection)9 ViewDef (db.ViewDef)4 Request (app.Request)3 LookupColumn (db.column.LookupColumn)3 IOException (java.io.IOException)3 OneToMany (db.OneToMany)2 Select (db.Select)2 RecordOwnerAccessPolicy (db.access.RecordOwnerAccessPolicy)2 Column (db.column.Column)2 SQLException (java.sql.SQLException)2 AdminTask (web.AdminTask)2 Module (app.Module)1 PersonColumn (app.PersonColumn)1 EventProvider (calendar.EventProvider)1 MailConnectException (com.sun.mail.util.MailConnectException)1 FormHook (db.FormHook)1 JDBCColumn (db.JDBCColumn)1 JDBCTable (db.JDBCTable)1 Reorderable (db.Reorderable)1 SelectRenderer (db.SelectRenderer)1