Package com.psddev.dari.db

Examples of com.psddev.dari.db.State


            return false;
        }

        try {
            Draft draft = getOverlaidDraft(object);
            State state = State.getInstance(draft != null ? draft : object);

            state.as(Content.ObjectModification.class).setTrash(false);
            publish(state);
            redirectOnSave("");
            return true;

        } catch (Exception error) {
View Full Code Here


        if (!isFormPost() ||
                param(String.class, "action-save") == null) {
            return false;
        }

        State state = State.getInstance(object);

        try {
            updateUsingParameters(object);
            state.save();
            redirectOnSave("",
                    "_frame", param(boolean.class, "_frame") ? Boolean.TRUE : null,
                    "id", state.getId());
            return true;

        } catch (Exception error) {
            getErrors().add(error);
            return false;
View Full Code Here

    @Override
    protected void doService(ToolPageContext page) throws IOException, ServletException {
        ToolUser user = page.getUser();
        Collection<String> includeFields = Arrays.asList("returnToDashboardOnSave");
        Object object = Query.from(Object.class).where("_id = ?", page.param(UUID.class, "id")).first();
        State state = State.getInstance(object);
        ContentLock contentLock = null;

        if (object != null) {
            contentLock = ContentLock.Static.lock(object, null, user);
        }

        if (page.isFormPost()) {
            if (page.param(String.class, "action-edits") != null) {
                if (state != null) {
                    Date newPublishDate = page.param(Date.class, "publishDate");

                    if (newPublishDate != null) {
                        Content.ObjectModification contentData = state.as(Content.ObjectModification.class);
                        DateTimeZone timeZone = page.getUserDateTimeZone();
                        newPublishDate = new Date(DateTimeFormat.
                                forPattern("yyyy-MM-dd HH:mm:ss").
                                withZone(timeZone).
                                parseMillis(new DateTime(newPublishDate).toString("yyyy-MM-dd HH:mm:ss")));

                        contentData.setPublishUser(page.getUser());
                        contentData.setPublishDate(newPublishDate);
                        state.save();
                    }
                }

            } else if (page.param(String.class, "action-unlock") != null) {
                contentLock.delete();

                page.writeStart("script", "type", "text/javascript");
                    page.writeRaw("window.location.reload();");
                page.writeEnd();

            } else if (page.param(String.class, "action-settings") != null) {
                try {
                    page.include("/WEB-INF/objectPost.jsp", "object", user, "includeFields", includeFields);
                    user.save();

                } catch (Exception error) {
                    page.getErrors().add(error);
                }
            }
        }

        String returnUrl = page.param(String.class, "returnUrl");

        page.writeHeader();
            page.writeStart("style", "type", "text/css");
                page.writeCss(".cms-contentTools th",
                        "width", "25%;");
            page.writeEnd();

            page.writeStart("div", "class", "widget cms-contentTools");
                page.writeStart("h1", "class", "icon icon-wrench");
                    page.writeHtml("Tools");
                page.writeEnd();

                page.writeStart("div", "class", "tabbed");
                    page.writeStart("div",
                            "class", "fixedScrollable",
                            "data-tab", "For Editors");
                        if (object != null) {
                            Content.ObjectModification contentData = state.as(Content.ObjectModification.class);
                            Date publishDate = contentData.getPublishDate();
                            ToolUser publishUser = contentData.getPublishUser();
                            Date updateDate = contentData.getUpdateDate();
                            ToolUser updateUser = contentData.getUpdateUser();

                            page.writeStart("table", "class", "table-striped");
                                page.writeStart("tbody");
                                    if (publishDate != null || publishUser != null) {
                                        page.writeStart("tr");
                                            page.writeStart("th");
                                                page.writeHtml("Published");
                                            page.writeEnd();

                                            page.writeStart("td");
                                                if (publishDate != null) {
                                                    page.writeHtml(page.formatUserDateTime(publishDate));
                                                }
                                            page.writeEnd();

                                            page.writeStart("td");
                                                if (publishUser != null) {
                                                    page.writeObjectLabel(publishUser);
                                                }
                                            page.writeEnd();
                                        page.writeEnd();
                                    }

                                    if (updateDate != null || updateUser != null) {
                                        page.writeStart("tr");
                                            page.writeStart("th");
                                                page.writeHtml("Last Updated");
                                            page.writeEnd();

                                            page.writeStart("td");
                                                if (updateDate != null) {
                                                    page.writeHtml(page.formatUserDateTime(updateDate));
                                                }
                                            page.writeEnd();

                                            page.writeStart("td");
                                                if (updateUser != null) {
                                                    page.writeObjectLabel(updateUser);
                                                }
                                            page.writeEnd();
                                        page.writeEnd();
                                    }
                                page.writeEnd();
                            page.writeEnd();

                            page.writeStart("h2");
                                page.writeHtml("Advanced Edits");
                            page.writeEnd();

                            if (page.isFormPost() &&
                                    page.param(String.class, "action-edits") != null) {
                                if (page.getErrors().isEmpty()) {
                                    page.writeStart("div", "class", "message message-success");
                                        page.writeHtml("Advanced edits successfully saved.");
                                    page.writeEnd();

                                } else {
                                    page.include("/WEB-INF/errors.jsp");
                                }
                            }

                            page.writeStart("form",
                                    "method", "post",
                                    "action", page.url(""));

                                page.writeStart("div", "class", "inputContainer");
                                    page.writeStart("div", "class", "inputLabel");
                                        page.writeStart("label", "for", page.createId());
                                            page.writeHtml("New Publish Date");
                                        page.writeEnd();
                                    page.writeEnd();

                                    page.writeStart("div", "class", "inputSmall");
                                        page.writeElement("input",
                                                "type", "text",
                                                "class", "date",
                                                "name", "publishDate",
                                                "value", page.formatUserDateTime(publishDate));
                                    page.writeEnd();
                                page.writeEnd();

                                page.writeStart("div", "class", "actions");
                                    page.writeStart("button",
                                            "class", "icon icon-action-save",
                                            "name", "action-edits",
                                            "value", true);
                                        page.writeHtml("Save");
                                    page.writeEnd();
                                page.writeEnd();
                            page.writeEnd();

                            if (!user.equals(contentLock.getOwner())) {
                                page.writeStart("h2");
                                    page.writeHtml("Content Lock");
                                page.writeEnd();

                                page.writeStart("div", "class", "message message-warning");
                                    page.writeStart("p");
                                        page.writeHtml("Locked by ");
                                        page.writeObjectLabel(contentLock.getOwner());
                                        page.writeHtml(" since ");
                                        page.writeHtml(page.formatUserDateTime(contentLock.getCreateDate()));
                                        page.writeHtml(".");
                                    page.writeEnd();
                                page.writeEnd();

                                page.writeStart("form",
                                        "method", "post",
                                        "action", page.url(""));
                                    page.writeStart("div", "class", "actions");
                                        page.writeStart("button",
                                                "class", "icon icon-unlock",
                                                "name", "action-unlock",
                                                "value", true);
                                            page.writeHtml("Unlock");
                                        page.writeEnd();
                                    page.writeEnd();
                                page.writeEnd();
                            }
                        }

                        page.writeStart("h2");
                            page.writeHtml("Settings");
                        page.writeEnd();

                        if (page.isFormPost() &&
                                page.param(String.class, "action-settings") != null) {
                            if (page.getErrors().isEmpty()) {
                                page.writeStart("div", "class", "message message-success");
                                    page.writeHtml("Settings successfully saved.");
                                page.writeEnd();

                            } else {
                                page.include("/WEB-INF/errors.jsp");
                            }
                        }

                        page.writeStart("form",
                                "method", "post",
                                "action", page.url(""),
                                "style", "margin-bottom:0;");
                            page.include("/WEB-INF/objectForm.jsp", "object", user, "includeFields", includeFields);

                            page.writeStart("div", "class", "actions");
                                page.writeStart("button",
                                        "class", "icon icon-action-save",
                                        "name", "action-settings",
                                        "value", true);
                                    page.writeHtml("Save");
                                page.writeEnd();
                            page.writeEnd();
                        page.writeEnd();
                    page.writeEnd();

                    page.writeStart("div",
                            "class", "fixedScrollable",
                            "data-tab", "For Developers");
                        page.writeStart("ul");
                            if (object != null) {
                                page.writeStart("li");
                                    page.writeStart("a",
                                            "target", "_blank",
                                            "href", page.objectUrl("/contentRaw", object));
                                        page.writeHtml("View Raw Data");
                                    page.writeEnd();
                                page.writeEnd();
                            }

                            if (!ObjectUtils.isBlank(returnUrl)) {
                                page.writeStart("li");
                                    if (ObjectUtils.to(boolean.class, StringUtils.getQueryParameterValue(returnUrl, "deprecated"))) {
                                        page.writeStart("a",
                                                "target", "_top",
                                                "href", StringUtils.addQueryParameters(returnUrl,
                                                        "deprecated", null));
                                            page.writeHtml("Hide Deprecated Fields");
                                        page.writeEnd();

                                    } else {
                                        page.writeStart("a",
                                                "target", "_top",
                                                "href", StringUtils.addQueryParameters(returnUrl,
                                                        "deprecated", true));
                                            page.writeHtml("Show Deprecated Fields");
                                        page.writeEnd();
                                    }
                                page.writeEnd();
                            }
                        page.writeEnd();

                        if (object != null) {
                            ObjectType type = state.getType();

                            page.writeStart("table", "class", "table-striped");
                                page.writeStart("tbody");
                                    if (type != null) {
                                        Class<?> objectClass = type.getObjectClass();

                                        if (objectClass != null) {
                                            page.writeStart("tr");
                                                page.writeStart("th");
                                                    page.writeStart("label", "for", page.createId());
                                                        page.writeHtml("Class");
                                                    page.writeEnd();
                                                page.writeEnd();

                                                page.writeStart("td");
                                                    page.writeJavaClassLink(objectClass);
                                                page.writeEnd();
                                            page.writeEnd();
                                        }
                                    }

                                    page.writeStart("tr");
                                        page.writeStart("th");
                                            page.writeStart("label", "for", page.createId());
                                                page.writeHtml("ID");
                                            page.writeEnd();
                                        page.writeEnd();

                                        page.writeStart("td");
                                            page.writeElement("input",
                                                    "type", "text",
                                                    "id", page.getId(),
                                                    "class", "code",
                                                    "value", state.getId(),
                                                    "readonly", "readonly",
                                                    "style", "width:100%;",
                                                    "onclick", "this.select();");
                                        page.writeEnd();
                                    page.writeEnd();

                                    page.writeStart("tr");
                                        page.writeStart("th");
                                            page.writeStart("label", "for", page.createId());
                                                page.writeHtml("URL");
                                            page.writeEnd();
                                        page.writeEnd();

                                        page.writeStart("td");
                                            page.writeElement("input",
                                                    "type", "text",
                                                    "id", page.getId(),
                                                    "value", JspUtils.getAbsoluteUrl(page.getRequest(), page.cmsUrl("/content/edit.jsp", "id", state.getId())),
                                                    "readonly", "readonly",
                                                    "style", "width:100%;",
                                                    "onclick", "this.select();");
                                        page.writeEnd();
                                    page.writeEnd();
                                page.writeEnd();
                            page.writeEnd();
                        }

                        if (object != null) {
                            ObjectType type = state.getType();

                            if (type != null) {
                                if (!ObjectUtils.isBlank(type.as(Renderer.TypeModification.class).getEmbedPath())) {
                                    String permalink = state.as(Directory.ObjectModification.class).getPermalink();

                                    if (!ObjectUtils.isBlank(permalink)) {
                                        String siteUrl = Application.Static.getInstance(CmsTool.class).getDefaultSiteUrl();
                                        StringBuilder embedCode = new StringBuilder();
View Full Code Here

            return false;
        }

        setContentFormScheduleDate(object);

        State state = State.getInstance(object);
        Draft draft = getOverlaidDraft(object);
        Workflow.Data workflowData = state.as(Workflow.Data.class);
        String oldWorkflowState = workflowData.getCurrentState();

        try {
            state.beginWrites();

            Workflow workflow = Query.from(Workflow.class).where("contentTypes = ?", state.getType()).first();

            if (workflow != null) {
                WorkflowTransition transition = workflow.getTransitions().get(action);

                if (transition != null) {
                    WorkflowLog log = new WorkflowLog();

                    updateUsingParameters(object);
                    updateUsingAllWidgets(object);
                    state.as(Content.ObjectModification.class).setDraft(false);
                    log.getState().setId(param(UUID.class, "workflowLogId"));
                    updateUsingParameters(log);
                    workflowData.changeState(transition, getUser(), log);

                    if (draft == null) {
                        publish(object);

                    } else {
                        draft.as(Workflow.Data.class).changeState(transition, getUser(), log);
                        draft.setObject(object);
                        publish(draft);
                    }

                    state.commitWrites();
                }
            }

            redirectOnSave("", "id", state.getId());
            return true;

        } catch (Exception error) {
            if (draft != null) {
                draft.as(Workflow.Data.class).revertState(oldWorkflowState);
            }

            workflowData.revertState(oldWorkflowState);
            getErrors().add(error);
            return false;

        } finally {
            state.endWrites();
        }
    }
View Full Code Here

                                        Map<String, Object> objectMap = (Map<String, Object>) ObjectUtils.fromJson(objectString.trim());
                                        ObjectType type = ObjectType.getInstance(ObjectUtils.to(UUID.class, objectMap.remove("_typeId")));

                                        if (type != null) {
                                            Object object = type.createObject(ObjectUtils.to(UUID.class, objectMap.remove("_id")));
                                            State objectState = State.getInstance(object);

                                            objectState.setValues(objectMap);
                                            substitutions.put(objectState.getId(), object);
                                        }
                                    }
                                }
                            }
                        }

                        UUID mainObjectId = ObjectUtils.to(UUID.class, request.getParameter("_mainObjectId"));
                        Object preview = Query.
                                fromAll().
                                where("_id = ?", mainObjectId).
                                first();

                        if (preview == null) {
                            preview = Query.
                                    fromAll().
                                    where("_id = ?", previewId).
                                    first();
                        }

                        if (preview instanceof Draft) {
                            mainObject = ((Draft) preview).getObject();

                        } else if (preview instanceof History) {
                            mainObject = ((History) preview).getObject();

                        } else if (preview instanceof Preview) {
                            Preview previewPreview = (Preview) preview;
                            mainObject = previewPreview.getObject();
                            Site previewSite = previewPreview.getSite();

                            if (previewSite != null) {
                                site = previewSite;
                                setSite(request, site);
                            }

                            AuthenticationFilter.Static.setCurrentPreview(request, PageContextFilter.Static.getResponse(), previewPreview);

                        } else if (mainObjectId != null) {
                            mainObject = preview;

                        } else {
                            mainObject = substitutions.get(previewId);

                            if (mainObject == null) {
                                mainObject = preview;
                            }
                        }
                    }

                    if (mainObject != null) {
                        Directory.Data dirData = State.getInstance(mainObject).as(Directory.Data.class);

                        if (dirData.getRawPaths().isEmpty()) {
                            dirData.addPath(null, "/_preview-" + previewId, Directory.PathType.PERMALINK);
                        }

                        Site previewSite = Query.
                                from(Site.class).
                                where("_id = ?", request.getParameter(PREVIEW_SITE_ID_PARAMETER)).
                                first();

                        if (previewSite != null) {
                            setSite(request, previewSite);
                        }
                    }

                } else {
                    mainObject = Directory.Static.findByPath(site, path);

                    if (mainObject == null) {
                        mainObject = Directory.Static.findByPath(site, path + "/index");

                        // Index pages should have a trailing slash.
                        if (mainObject != null) {

                            // Except when told not to.
                            if (Query.from(CmsTool.class).first().isRemoveTrailingSlashes()) {
                                if (path.length() > 1 && path.endsWith("/")) {
                                    fixPath(request, servletPath.substring(0, servletPath.length() - 1));
                                }

                            } else if (!path.endsWith("/")) {
                                fixPath(request, servletPath + "/");
                            }
                        }

                    // Normal pages shouldn't have a trailing slash.
                    } else if (path.endsWith("/")) {
                        fixPath(request, servletPath.substring(0, servletPath.length() - 1));
                    }
                }

                // Case-insensitive path look-up.
                for (int i = 0, length = path.length(); i < length; ++ i) {
                    if (Character.isUpperCase(path.charAt(i))) {
                        String pathLc = path.toLowerCase(Locale.ENGLISH);
                        if (Directory.Static.findObject(site, pathLc) != null) {
                            fixPath(request, pathLc);
                        }
                        break;
                    }
                }

                // Special fallback names. For example, given /path/to/file,
                // the following are checked:
                //
                // - /path/to/file/*
                // - /path/to/file/**
                // - /path/to/*
                // - /path/to/**
                // - /path/**
                // - /**
                String checkPath;
                int endMarker;

                if (path.endsWith("/")) {
                    checkPath = path;
                    endMarker = 0;

                } else {
                    checkPath = path + "/";
                    endMarker = 1;
                }

                for (int i = 0; mainObject == null; ++ i) {
                    int slashAt = checkPath.lastIndexOf("/");

                    if (slashAt < 0) {
                        break;
                    } else {
                        checkPath = checkPath.substring(0, slashAt);
                    }

                    if (i <= endMarker) {
                        mainObject = Directory.Static.findObject(site, checkPath + "/*");
                    }

                    if (mainObject == null) {
                        mainObject = Directory.Static.findObject(site, checkPath + "/**");
                    }

                    if (mainObject instanceof Directory) {
                        mainObject = null;
                    }

                    if (mainObject != null) {
                        final String pathInfo = path.substring(checkPath.length());

                        if (Query.from(CmsTool.class).first().isRemoveTrailingSlashes()) {
                            if ("/".equals(pathInfo)) {
                                fixPath(request, servletPath.substring(0, servletPath.length() - 1));
                            }

                        } else if (pathInfo.length() < 1) {
                            fixPath(request, servletPath + "/");
                        }

                        request.setAttribute(NEW_REQUEST_ATTRIBUTE, new HttpServletRequestWrapper(request) {
                            @Override
                            public String getPathInfo() {
                                return pathInfo;
                            }
                        });
                    }
                }

                if (!Static.isPreview(request) && mainObject != null) {
                    Preview preview = AuthenticationFilter.Static.getCurrentPreview(request);

                    if (preview != null) {
                        State mainState = State.getInstance(mainObject);

                        if (mainState.getId().equals(preview.getObjectId())) {
                            request.setAttribute(PREVIEW_ATTRIBUTE, Boolean.TRUE);
                            request.setAttribute(PERSISTENT_PREVIEW_ATTRIBUTE, Boolean.TRUE);
                            mainState.putAll(preview.getObjectValues());
                        }
                    }
                }

                setMainObject(request, mainObject);
View Full Code Here

        /**
         * Returns a label, or the given {@code defaultLabel} if one can't be
         * found, for the given {@code object}.
         */
        public static String getObjectLabelOrDefault(Object object, String defaultLabel) {
            State state = State.getInstance(object);

            if (state != null) {
                String label = state.getLabel();

                if (ObjectUtils.to(UUID.class, label) == null) {
                    return notTooShort(label);
                }
            }
View Full Code Here

            return notTooShort(defaultLabel);
        }

        /** Returns a label for the given {@code object}. */
        public static String getObjectLabel(Object object) {
            State state = State.getInstance(object);
            String label = null;

            if (state != null) {
                label = state.getLabel();
            }

            if (ObjectUtils.isBlank(label)) {
                label = "Not Available";
            }
View Full Code Here

        /**
         * Returns a label, or the given {@code defaultLabel} if one can't be
         * found, for the type of the given {@code object}.
         */
        public static String getTypeLabelOrDefault(Object object, String defaultLabel) {
            State state = State.getInstance(object);

            if (state != null) {
                ObjectType type = state.getType();

                if (type != null) {
                    return getObjectLabel(type);
                }
            }
View Full Code Here

     * Updates this stage using the given {@code object}.
     *
     * @param object Can't be {@code null}.
     */
    public void update(Object object) {
        State state = State.getInstance(object);
        ObjectType type = state.getType();
        SharedUpdatable sharedUpdatable = type != null ?
                type.as(TypeData.class).createSharedUpdatable() :
                null;

        if (sharedUpdatable != null) {
View Full Code Here

        if (object == null) {
            return;
        }

        // Pretend to update the object.
        State state = State.getInstance(object);

        if (state.isNew() ||
                object instanceof Draft ||
                state.as(Content.ObjectModification.class).isDraft() ||
                state.as(Workflow.Data.class).getCurrentState() != null) {
            page.setContentFormScheduleDate(object);
        }

        try {
            state.beginWrites();
            page.updateUsingParameters(object);
            page.updateUsingAllWidgets(object);
            page.publish(object);

        } catch (IOException error) {
            throw error;

        } catch (ServletException error) {
            throw error;

        } catch (RuntimeException error) {
            throw error;

        } catch (Exception error) {
            ErrorUtils.rethrow(error);

        } finally {
            state.endWrites();
        }

        // Expensive operations that should only trigger occasionally.
        boolean idle = page.param(boolean.class, "idle");

        if (idle) {
            boolean saveUser = false;

            // Automatically save newly created drafts when the user is idle.
            Content.ObjectModification contentData = state.as(Content.ObjectModification.class);
            ToolUser user = page.getUser();

            if (idle &&
                    (state.isNew() || contentData.isDraft()) &&
                    !page.getCmsTool().isDisableAutomaticallySavingDrafts()) {
                contentData.setDraft(true);
                contentData.setUpdateDate(new Date());
                contentData.setUpdateUser(user);
                state.save();

                Set<UUID> automaticallySavedDraftIds = user.getAutomaticallySavedDraftIds();
                UUID id = state.getId();

                if (!automaticallySavedDraftIds.contains(id)) {
                    saveUser = true;

                    automaticallySavedDraftIds.add(id);
                }
            }

            // Preview for looking glass.
            Preview preview = new Preview();
            UUID currentPreviewId = user.getCurrentPreviewId();

            if (currentPreviewId == null) {
                saveUser = true;
                currentPreviewId = preview.getId();

                user.setCurrentPreviewId(currentPreviewId);
            }

            Map<String, Object> values = state.getSimpleValues();

            preview.getState().setId(currentPreviewId);
            preview.setCreateDate(new Date());
            preview.setObjectType(state.getType());
            preview.setObjectId(state.getId());
            preview.setObjectValues(values);
            preview.setSite(page.getSite());
            preview.save();
            AuthenticationFilter.Static.setCurrentPreview(page.getRequest(), page.getResponse(), preview);
            user.saveAction(page.getRequest(), object);

            if (saveUser) {
                user.save();
            }
        }

        Map<String, Object> jsonResponse = new CompactMap<String, Object>();

        // HTML display for the URL widget.
        @SuppressWarnings("unchecked")
        Set<Directory.Path> newPaths = (Set<Directory.Path>) state.getExtras().get("cms.newPaths");

        if (!ObjectUtils.isBlank(newPaths)) {
            StringWriter string = new StringWriter();
            @SuppressWarnings("resource")
            HtmlWriter html = new HtmlWriter(string);
View Full Code Here

TOP

Related Classes of com.psddev.dari.db.State

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.