Search in sources :

Example 11 with Request

use of app.Request in project common by zenlunatics.

the class Meetings method _newViewDef.

// ------------------------------------------------------------------------
@Override
public ViewDef _newViewDef(String name, Site site) {
    if (name.equals("meetings")) {
        ViewDef view_def = new ViewDef(name) {

            @Override
            public View newView(Request request) {
                View view = new View(this, request);
                if (request.getParameter("db_mode") == View.Mode.EDIT_FORM.toString())
                    if (request.db.lookupInt("_owner_", "meetings", Integer.parseInt(view.getKeyValue()), -1) != request.getUser().getId())
                        view.setColumn(new Column("title").setIsReadOnly(true));
                return view;
            }
        }.addInsertHook(this).addUpdateHook(this).setAccessPolicy(new RecordOwnerAccessPolicy() {

            @Override
            public boolean canUpdateRow(String from, int id, Request request) {
                return true;
            }

            @Override
            public boolean showEditButtonForRow(View view, Request request) {
                return true;
            }
        }.add().delete().edit()).setRecordName("Meeting").setColumnNamesForm(new String[] { "title", "days", "_owner_" }).setColumnNamesTable(new String[] { "title" }).setColumn(new Column("days").setInputRenderer(new MeetingInputRenderer()).setLabelRenderer(new AddButtonLabelRenderer("meeting.addDay()"))).setColumn(new Column("_owner_").setDefaultToUserId().setIsHidden(true)).setColumn(new Column("title").setIsRequired(true)).addRelationshipDef(new OneToManyLink("meeting_people", "people_id"));
        view_def.getViewTheme().setEditText("set times");
        return view_def;
    }
    if (name.equals("meeting_people"))
        return new ViewDef(name).setDialogModes(View.Mode.ADD_FORM).setRecordName("Person").setColumnNamesTable(new String[] { "color", "people_id" }).setColumn(new Column("color").setDisplayName("").setValueRenderer(new MeetingColorRenderer(), false)).setColumn(new Column("days").setInputRenderer(new MeetingInputRenderer()).setLabelRenderer(new AddButtonLabelRenderer("addDay()"))).setColumn(new LookupColumn("people_id", "people", "first,last").setDisplayName("Person"));
    return null;
}
Also used : OneToManyLink(db.OneToManyLink) Request(app.Request) ViewDef(db.ViewDef) View(db.View) RecordOwnerAccessPolicy(db.access.RecordOwnerAccessPolicy) LookupColumn(db.column.LookupColumn) LookupColumn(db.column.LookupColumn) Column(db.column.Column) AddButtonLabelRenderer(db.AddButtonLabelRenderer)

Example 12 with Request

use of app.Request 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 13 with Request

use of app.Request in project common by zenlunatics.

the class DB method doPost.

// --------------------------------------------------------------------------
@Override
protected void doPost(HttpServletRequest http_request, HttpServletResponse http_response) throws ServletException, IOException {
    http_request.setCharacterEncoding("UTF-8");
    http_response.setCharacterEncoding("UTF-8");
    Request request = new Request(http_request, http_response);
    http_response.setContentType("text/plain");
    String db_cmd = request.getParameter("db_cmd");
    if ("set_session_attribute".equals(db_cmd)) {
        Enumeration<String> parameter_names = request.getParameterNames();
        while (parameter_names.hasMoreElements()) {
            String parameter_name = parameter_names.nextElement();
            if (!parameter_name.equals("db_cmd")) {
                request.setSessionAttribute(parameter_name, request.getParameter(parameter_name));
                request.release();
                return;
            }
        }
    } else if ("set_setting".equals(db_cmd)) {
        Enumeration<String> parameter_names = request.getParameterNames();
        while (parameter_names.hasMoreElements()) {
            String parameter_name = parameter_names.nextElement();
            if (!parameter_name.startsWith("db_")) {
                request.site.getSettings().set(parameter_name, request.getParameter(parameter_name), request.db);
                request.writer.write("fade_alert('saved',$('" + Validation.getValidIdentifier("db_submit_id", request) + "'))");
                request.release();
                return;
            }
        }
    } else if ("set_theme".equals(db_cmd)) {
        Person user = request.getUser();
        request.db.update("people", "theme='" + request.getParameter("theme") + "'", user.getId());
        request.setSessionAttribute("user", null);
        request.release();
        return;
    }
    String task = request.getParameter("db_task");
    String view_def_name = request.getParameter("db_view_def");
    ViewDef view_def = request.site.getViewDef(view_def_name, request.db);
    if (task != null) {
        view_def.m_tasks.get(task).perform(request);
        request.release();
        return;
    }
    try {
        if (db_cmd.equals("and_filter")) {
            ViewState view_state = (ViewState) request.getState(ViewState.class, view_def_name);
            int index = view_state.filter.lastIndexOf(" AND " + request.getParameter("db_column") + " ");
            if (index != -1)
                // TODO: assumes is last expression
                view_state.filter = view_state.filter.substring(0, index);
            setFilterFromForm(view_def, true, request);
        } else if (db_cmd.equals("clear_filter"))
            ViewState.setFilter(view_def_name, null, request);
        else if (db_cmd.equals("delete")) {
            // exists if we're unlinking a OneToManyLink
            String one = request.getParameter("db_one_from");
            if (one != null)
                request.db.update(view_def.getFrom(), one + "_id=NULL", Validation.getValidInteger("db_key_value", -1, request));
            else
                view_def.delete(Validation.getValidIntegerString("db_key_value", request), request);
            String on_delete = view_def.getOnDelete(request);
            if (on_delete != null)
                request.writer.write(on_delete).write(';');
            String form = request.getParameter("form");
            if (form != null)
                request.writer.write(view_def.getFormCloseOnClick(View.Mode.EDIT_FORM));
        } else if (db_cmd.equals("import_views"))
            Admin.importViews(request.getParameter("site_url"), request);
        else if (db_cmd.equals("insert"))
            insert(view_def, request);
        else if (db_cmd.equals("link")) {
            request.db.update(view_def.getFrom(), request.getParameter("db_one_from") + "_id=" + ((ViewState) request.getOrCreateState(ViewState.class, request.getParameter("db_one_name"))).key_value, request.getInt("many_id", -1));
            request.writer.write(view_def.getOnSuccess(View.Mode.EDIT_FORM, request));
        } else if (db_cmd.equals("quicksearch")) {
            String value = request.getParameter("value");
            List<Feature> features = view_def.getFeatures();
            if (features != null)
                for (Feature feature : features) if (feature instanceof Quicksearch)
                    ViewState.setQuicksearch(view_def.getName(), value.length() > 0 ? value : null, ((Quicksearch) feature).getColumns(), request);
        } else if (db_cmd.equals("read"))
            writeFile(view_def, request);
        else if (db_cmd.equals("reorder"))
            view_def.getReorderable(request).reorder(request);
        else if (db_cmd.equals("set")) {
            String value;
            if ((value = request.getParameter("password")) != null) {
                int id = request.getInt("id", 0);
                Person user = request.getUser();
                if (id == user.getId() || request.userIsAdmin() || request.db.rowExists("user_roles", "people_id=" + user.getId() + " AND role='people'")) {
                    request.db.update("people", "password=md5('" + DBConnection.escape(value) + "')", id);
                    request.writer.write("dialog_alert('','password changed')");
                }
            }
        } else if (db_cmd.equals("set_filter_from_form"))
            setFilterFromForm(view_def, false, request);
        else if (db_cmd.equals("update"))
            update(view_def, request);
        else if (db_cmd.equals("update_file")) {
            String column = request.getParameter("db_column");
            int id = Validation.getValidInteger("db_key_value", 0, request);
            NameValuePairs name_value_pairs = new NameValuePairs();
            name_value_pairs.set(column, request.db.lookupString(new Select(column).from(view_def_name).whereIdEquals(id)));
            name_value_pairs.set(column + "_text", request.getParameter(column + "_text"));
            FileColumn file_column = (FileColumn) view_def.getColumn(column);
            String dir_column = file_column.getDirColumn();
            if (dir_column != null)
                name_value_pairs.set(dir_column, request.getParameter(dir_column));
            file_column.beforeUpdate(id, name_value_pairs, null, request);
        } else if (db_cmd.equals("toggle_column"))
            ViewState.toggleColumn(view_def_name, request.getParameter("db_column"), request);
        request.release();
    } catch (Exception e) {
        String s = e.toString();
        if (s.indexOf("ERROR: duplicate key value") != -1) {
            e.printStackTrace(System.out);
            request.abortAlert(s.substring(s.indexOf("Detail:") + 12));
        } else
            request.abortAlert(e);
    }
}
Also used : Enumeration(java.util.Enumeration) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(app.Request) FileColumn(db.column.FileColumn) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Person(app.Person)

Example 14 with Request

use of app.Request 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 15 with Request

use of app.Request in project common by zenlunatics.

the class Remote method doGet.

// --------------------------------------------------------------------
@Override
protected void doGet(HttpServletRequest http_request, HttpServletResponse http_response) throws ServletException, IOException {
    Request request = new Request(http_request, http_response);
    String action = request.getParameter("action");
    if (request.site.allowRemote())
        if (action.equals("export"))
            request.db.writeTable(request.getParameter("from"), request.getParameter("columns"), null, http_response.getWriter());
        else if (action.equals("get_schema"))
            Schema.writeXML(request.db, request.writer);
    request.release();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(app.Request)

Aggregations

Request (app.Request)15 ViewDef (db.ViewDef)8 LookupColumn (db.column.LookupColumn)7 JDBCColumn (db.JDBCColumn)6 Column (db.column.Column)6 IOException (java.io.IOException)6 View (db.View)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 NameValuePairs (db.NameValuePairs)4 OneToMany (db.OneToMany)4 RecordOwnerAccessPolicy (db.access.RecordOwnerAccessPolicy)4 DBConnection (db.DBConnection)3 JDBCTable (db.JDBCTable)3 Select (db.Select)3 ArrayList (java.util.ArrayList)3 Page (app.Page)2 FormHook (db.FormHook)2 LinkValueRenderer (db.LinkValueRenderer)2 ManyToMany (db.ManyToMany)2 Reorderable (db.Reorderable)2