Search in sources :

Example 6 with DBConnection

use of db.DBConnection in project common by zenlunatics.

the class Surveys method newAnswersViewDef.

// ----------------------------------------------------------------------
private ViewDef newAnswersViewDef(String name, Site site) {
    ViewDef view_def = new ViewDef(name);
    String surveys_id = name.substring(14);
    DBConnection db = new DBConnection(site);
    boolean one_answer = db.lookupBoolean(new Select("one_answer").from("surveys").whereIdEquals(surveys_id));
    boolean show_names = db.lookupBoolean(new Select("show_names").from("surveys").whereIdEquals(surveys_id));
    view_def.addInsertHook(this);
    if (one_answer || show_names)
        view_def.setAccessPolicy(new RecordOwnerAccessPolicy().add().delete().edit());
    else
        view_def.setAccessPolicy(new AccessPolicy().add().edit());
    view_def.setFormButtonsLocation(Form.Location.BOTTOM);
    view_def.setDefaultOrderBy("id");
    view_def.setOnSuccess("$('survey_page').replace(context+'/Surveys?action=writeForm&id='+$('survey_page').get('survey_id'));");
    view_def.setRecordName("Answer");
    view_def.setRowWindowSize(0);
    view_def.setTimestampRecords(true);
    try {
        boolean answer_table_exists = db.tableExists(name);
        ResultSet questions = db.select("*", "survey_questions", "surveys_id=" + surveys_id, "id");
        if (!questions.isBeforeFirst()) {
            questions.getStatement().close();
            throw new RuntimeException("no questions defined");
        }
        if (!answer_table_exists)
            db.createManyTable("surveys", name, null, one_answer || show_names ? "people" : null, true);
        while (questions.next()) {
            String answer_type = questions.getString("answer_type");
            if (answer_type.equals("Divider") || answer_type.equals("HTML"))
                continue;
            String question_id = questions.getString("id");
            String type_data = questions.getString("type_data");
            view_def.setColumn(newAnswerColumn(question_id, questions.getString("question"), questions.getString("pre_text"), questions.getString("post_text"), answer_type, type_data, questions.getBoolean("required"), db));
            if (!answer_table_exists)
                addAnswerTableColumn(name, question_id, answer_type, type_data, db);
        }
        questions.getStatement().close();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    db.close();
    view_def.setColumn(new LookupColumn("_owner_", "people", "first,last").setDefaultToUserId());
    view_def.setColumn(new Column("surveys_id").setIsHidden(true));
    return view_def;
}
Also used : DBConnection(db.DBConnection) RecordOwnerAccessPolicy(db.access.RecordOwnerAccessPolicy) LookupColumn(db.column.LookupColumn) SQLException(java.sql.SQLException) LookupColumn(db.column.LookupColumn) BooleanColumn(db.column.BooleanColumn) Column(db.column.Column) TextAreaColumn(db.column.TextAreaColumn) Select(db.Select) ResultSet(java.sql.ResultSet) ViewDef(db.ViewDef) AccessPolicy(db.access.AccessPolicy) RecordOwnerAccessPolicy(db.access.RecordOwnerAccessPolicy)

Example 7 with DBConnection

use of db.DBConnection in project common by zenlunatics.

the class Admin method _newViewDef.

// --------------------------------------------------------------------------
@Override
public ViewDef _newViewDef(String name, Site site) {
    if (name.startsWith("admin__")) {
        String table = name.substring(7);
        ViewDef view_def = new ViewDef(name, table, null) {

            @Override
            public View newView(Request request) {
                if (!request.getUser().isAdministrator())
                    return null;
                return super.newView(request);
            }
        };
        view_def.setAllowQuickEdit(true);
        view_def.setRecordName(table);
        view_def.setShowTableColumnPicker(true, null);
        DBConnection db = new DBConnection(site);
        JDBCTable jdbc_table = db.getTable(table);
        if (jdbc_table.getColumn("_order_") != null)
            view_def.setReorderable(new Reorderable());
        for (String column_name : jdbc_table.getColumnNamesAll()) if (column_name.equals("_owner_"))
            view_def.setColumn(new LookupColumn(column_name, "people", "first,last").setAllowNoSelection(true).setShowFormLink(true));
        else if (column_name.endsWith("_id") && jdbc_table.getColumn(column_name).getType() == Types.INTEGER) {
            String one_table = column_name.substring(0, column_name.length() - 3);
            if (one_table.equals("people"))
                view_def.setColumn(new LookupColumn(column_name, one_table, "first,last").setAllowNoSelection(true).setShowFormLink(true));
            else if (db.getTable(one_table) != null) {
                String[] column_names = db.getTable(one_table).getColumnNames();
                if (column_names.length > 0)
                    view_def.setColumn(new LookupColumn(column_name, one_table, column_names[0]).setAllowNoSelection(true).setShowFormLink(true));
            }
        }
        for (String[] foreign_key : db.getForeignKeys(table)) if (foreign_key[1].equals(table + "_id"))
            view_def.addRelationshipDef(new OneToMany(foreign_key[0]));
        db.close();
        return view_def;
    }
    return null;
}
Also used : DBConnection(db.DBConnection) LookupColumn(db.column.LookupColumn) JDBCTable(db.JDBCTable) Request(app.Request) ViewDef(db.ViewDef) Reorderable(db.Reorderable) OneToMany(db.OneToMany)

Example 8 with DBConnection

use of db.DBConnection in project common by zenlunatics.

the class MethodForm method doPost.

// --------------------------------------------------------------------------
public static boolean doPost(Request request) {
    String class_name = request.getParameter("class");
    String method_name = request.getParameter("method");
    Object object = null;
    Class<?> object_class = null;
    if (class_name != null) {
        if (class_name.equals("Admin")) {
            object = request.site.getModule("Admin");
            object_class = object.getClass();
        } else if (class_name.equals("DBConnection")) {
            object = request.db;
            object_class = object.getClass();
        } else if (class_name.equals(request.site.getClass().getSimpleName())) {
            object = request.site;
            object_class = object.getClass();
        } else
            for (Class<?> c : request.site.getClasses()) if (c.getName().endsWith(class_name)) {
                object_class = c;
                break;
            }
    } else {
        object = request.site.getModule(request.getParameter("module"));
        object_class = object.getClass();
    }
    try {
        for (Method method : object_class.getMethods()) if (method_name.equals(method.getName()) && method.isAnnotationPresent(AdminTask.class)) {
            Class<?>[] parameter_types = method.getParameterTypes();
            Object[] args = new Object[parameter_types.length];
            for (int i = 0; i < parameter_types.length; i++) if (parameter_types[i] == Calendar.class) {
                Calendar calendar = Calendar.getInstance();
                DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
                calendar.setTime(df.parse(request.getParameter("arg" + i)));
                args[i] = calendar;
            } else if (parameter_types[i] == FileItem.class)
                args[i] = request.getFileItem("arg" + i);
            else if (parameter_types[i] == LocalDateTime.class)
                args[i] = request.site.newLocalDateTime();
            else if (parameter_types[i] == DBConnection.class)
                args[i] = request.db;
            else if (parameter_types[i] == HttpServletRequest.class)
                args[i] = request.request;
            else if (parameter_types[i] == Request.class)
                args[i] = request;
            else if (parameter_types[i] == Site.class)
                args[i] = request.site;
            else if (parameter_types[i] == boolean.class)
                args[i] = Boolean.valueOf(request.getParameter("arg" + i) != null);
            else if (parameter_types[i] == int.class)
                args[i] = Integer.valueOf(request.getParameter("arg" + i));
            else
                args[i] = request.getParameter("arg" + i);
            System.out.println("invoking " + object_class.getSimpleName() + "." + method.getName());
            Object return_value = null;
            try {
                return_value = method.invoke(object, args);
            } catch (Exception e) {
                request.abort(e);
            }
            request.writer.write(object_class.getSimpleName() + "." + method.getName() + " done");
            System.out.println(object_class.getSimpleName() + "." + method.getName() + " done");
            if (method.getReturnType() == boolean.class)
                return ((Boolean) return_value).booleanValue();
            return false;
        }
    } catch (IllegalArgumentException e) {
        request.abort(e);
    } catch (SecurityException e) {
        throw new RuntimeException(e);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    System.out.println("method " + class_name + "." + method_name + " not found or not public or not annotated with AdminTask");
    return false;
}
Also used : DBConnection(db.DBConnection) Calendar(java.util.Calendar) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(app.Request) Method(java.lang.reflect.Method) IOException(java.io.IOException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParseException(java.text.ParseException) FileItem(org.apache.commons.fileupload.FileItem) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException)

Example 9 with DBConnection

use of db.DBConnection in project common by zenlunatics.

the class Site method getViewDef.

// --------------------------------------------------------------------------
public ViewDef getViewDef(String name, DBConnection db) {
    ViewDef view_def = null;
    if (name != null)
        view_def = m_view_defs.get(name);
    if (view_def == null) {
        view_def = newViewDef(name);
        if (view_def != null) {
            DBConnection dbc = db != null ? db : new DBConnection(this);
            if (!view_def._load_(dbc) && "Record".equals(view_def.getRecordName()))
                view_def.setRecordName(name);
            if (db == null)
                dbc.close();
            m_view_defs.put(name, view_def);
        }
    }
    return view_def;
}
Also used : DBConnection(db.DBConnection) ViewDef(db.ViewDef)

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