Search in sources :

Example 1 with VersionableSaveDialog

use of com.centurylink.mdw.plugin.designer.dialogs.VersionableSaveDialog in project mdw-designer by CenturyLinkCloud.

the class TaskTemplateEditor method saveDialog.

private int saveDialog(boolean closeButton) {
    VersionableSaveDialog saveDlg = new VersionableSaveDialog(getSite().getShell(), taskTemplate, closeButton);
    int result = saveDlg.open();
    if (result == VersionableSaveDialog.CANCEL)
        return result;
    try {
        if (result == VersionableSaveDialog.CLOSE_WITHOUT_SAVE) {
            // refresh from file
            String content = new String(PluginUtil.readFile(taskTemplate.getFile()));
            TaskVO taskVO;
            if (content.trim().startsWith("{")) {
                taskVO = new TaskVO(new JSONObject(content));
            } else {
                TaskTemplateDocument docx = TaskTemplateDocument.Factory.parse(content);
                taskVO = new TaskVO(docx.getTaskTemplate());
            }
            taskVO.setName(taskTemplate.getName());
            taskVO.setTaskId(taskTemplate.getId());
            taskVO.setPackageName(taskTemplate.getPackage().getName());
            taskTemplate.setTaskVO(taskVO);
            taskTemplate.setDirty(false);
            return result;
        } else // save the template
        {
            // clean out units attrs
            taskTemplate.removeAttribute("TaskSLA_UNITS");
            taskTemplate.removeAttribute("ALERT_INTERVAL_UNITS");
            Increment versionIncrement = saveDlg.getVersionIncrement();
            String pkgMeta = taskTemplate.getPackage().getMetaContent();
            if (pkgMeta != null && pkgMeta.trim().startsWith("{")) {
                taskTemplate.setContent(taskTemplate.getTaskVO().getJson().toString(2));
            } else {
                TaskTemplateDocument doc = TaskTemplateDocument.Factory.newInstance();
                doc.setTaskTemplate(taskTemplate.getTaskVO().toTemplate());
                XmlOptions xmlOptions = new XmlOptions().setSaveAggressiveNamespaces();
                xmlOptions.setSavePrettyPrint().setSavePrettyPrintIndent(2);
                taskTemplate.setContent(doc.xmlText(xmlOptions));
            }
            taskTemplate.ensureFileWritable();
            InputStream is = new ByteArrayInputStream(taskTemplate.getContent().getBytes());
            taskTemplate.getAssetFile().setContents(is, true, true, new NullProgressMonitor());
            if (versionIncrement != Increment.Overwrite) {
                taskTemplate.setVersion(versionIncrement == Increment.Major ? taskTemplate.getNextMajorVersion() : taskTemplate.getNextMinorVersion());
                taskTemplate.setRevisionComment(saveDlg.getVersionComment());
                taskTemplate.getProject().getDesignerProxy().saveWorkflowAssetWithProgress(taskTemplate, false);
                taskTemplate.fireElementChangeEvent(ChangeType.VERSION_CHANGE, taskTemplate.getVersion());
            }
            dirtyStateChanged(false);
            if (!taskTemplate.getProject().isRemote())
                taskTemplate.getProject().getDesignerProxy().getCacheRefresh().fireRefresh(false);
            return result;
        }
    } catch (Exception ex) {
        PluginMessages.uiError(getSite().getShell(), ex, "Save Task Template", taskTemplate.getProject());
        return VersionableSaveDialog.CANCEL;
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TaskTemplateDocument(com.centurylink.mdw.task.TaskTemplateDocument) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) VersionableSaveDialog(com.centurylink.mdw.plugin.designer.dialogs.VersionableSaveDialog) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Increment(com.centurylink.mdw.plugin.designer.model.Versionable.Increment) XmlOptions(org.apache.xmlbeans.XmlOptions) TaskVO(com.centurylink.mdw.model.value.task.TaskVO) JSONException(org.json.JSONException) PartInitException(org.eclipse.ui.PartInitException)

Example 2 with VersionableSaveDialog

use of com.centurylink.mdw.plugin.designer.dialogs.VersionableSaveDialog in project mdw-designer by CenturyLinkCloud.

the class WorkflowAsset method resourceChanged.

/**
 * Change listener so we'll know when the resource is changed in the
 * workspace.
 */
public void resourceChanged(IResourceChangeEvent event) {
    if (isForceExternalEditor())
        return;
    if (event.getType() == IResourceChangeEvent.POST_CHANGE) {
        final IFile file = getAssetFile();
        IResourceDelta rootDelta = event.getDelta();
        IResourceDelta assetDelta = rootDelta.findMember(file.getFullPath());
        if (assetDelta != null && assetDelta.getKind() == IResourceDelta.CHANGED && (assetDelta.getFlags() & IResourceDelta.CONTENT) != 0) {
            // the file has been changed
            final Display display = Display.getCurrent();
            if (display != null) {
                display.syncExec(new Runnable() {

                    public void run() {
                        if (isRawEdit()) {
                            if (getProject().isReadOnly())
                                MessageDialog.openWarning(display.getActiveShell(), "Not Editable", "Your changes to " + getFile().getName() + " will be overwritten the next time project '" + getProject().getLabel() + "' is refreshed.");
                        } else {
                            if (!isUserAuthorized(UserRoleVO.ASSET_DESIGN)) {
                                MessageDialog.openWarning(display.getActiveShell(), "Can't Update " + getTitle(), "You're not authorized to update '" + getLabel() + "'\nin workflow project '" + getProject().getName() + "'.");
                                return;
                            } else if (!isLockedToUser()) {
                                MessageDialog.openWarning(display.getActiveShell(), "Can't Update " + getTitle(), "Resource '" + getLabel() + "' is not locked by you, so updates are not allowed.");
                                return;
                            }
                        }
                        if (isBinary())
                            encodeAndSetContent(PluginUtil.readFile(file));
                        else
                            setContent(new String(PluginUtil.readFile(file)));
                        Increment versionIncrement = Increment.Overwrite;
                        int previousVersion = getVersion();
                        String versionComment = null;
                        if (getProject().checkRequiredVersion(5, 0)) {
                            VersionableSaveDialog saveDialog = new VersionableSaveDialog(display.getActiveShell(), WorkflowAsset.this);
                            int res = saveDialog.open();
                            if (res == VersionableSaveDialog.CANCEL) {
                                if (isRawEdit()) {
                                    String message = "Version for '" + WorkflowAsset.this.getName() + "' remains " + WorkflowAsset.this.getVersionLabel();
                                    MessageDialog.openInformation(display.getActiveShell(), WorkflowAsset.this.getTitle() + " Overwrite", message);
                                    return;
                                } else {
                                    String message = "Database save for '" + WorkflowAsset.this.getName() + "' was canceled.\nTemp file changes were not persisted.";
                                    MessageDialog.openWarning(display.getActiveShell(), WorkflowAsset.this.getTitle() + " Not Saved", message);
                                    return;
                                }
                            }
                            versionIncrement = saveDialog.getVersionIncrement();
                            if (versionIncrement != Increment.Overwrite) {
                                setVersion(versionIncrement == Increment.Major ? getNextMajorVersion() : getNextMinorVersion());
                                versionComment = saveDialog.getVersionComment();
                            }
                        }
                        if (isRawEdit()) {
                            if (versionIncrement == Increment.Overwrite) {
                                // just fire cache refresh
                                if (!getProject().isRemote())
                                    getProject().getDesignerProxy().getCacheRefresh().fireRefresh(RuleSetVO.JAVA.equals(getLanguage()));
                            } else {
                                setRevisionComment(versionComment);
                                getProject().getDesignerProxy().saveWorkflowAssetWithProgress(WorkflowAsset.this, false);
                                fireElementChangeEvent(ChangeType.VERSION_CHANGE, getVersion());
                            }
                        } else {
                            try {
                                IPreferenceStore prefsStore = MdwPlugin.getDefault().getPreferenceStore();
                                boolean keepLocked = prefsStore.getBoolean(PreferenceConstants.PREFS_KEEP_RESOURCES_LOCKED_WHEN_SAVING);
                                if (versionIncrement != Increment.Overwrite) {
                                    RuleSetVO prevVO = new RuleSetVO(WorkflowAsset.this.getRuleSetVO());
                                    prevVO.setId(getId());
                                    prevVO.setVersion(previousVersion);
                                    setComment(versionComment);
                                    if (!isInDefaultPackage()) {
                                        getPackage().removeAsset(WorkflowAsset.this);
                                        getProject().getUnpackagedWorkflowAssets().add(new WorkflowAsset(prevVO, getProject().getDefaultPackage()));
                                    }
                                    RunnerResult result = getProject().getDesignerProxy().createNewWorkflowAsset(WorkflowAsset.this, keepLocked);
                                    if (result.getStatus() == RunnerStatus.SUCCESS) {
                                        getRuleSetVO().setPrevVersion(prevVO);
                                        fireElementChangeEvent(ChangeType.VERSION_CHANGE, getVersion());
                                    } else if (result.getStatus() == RunnerStatus.DISALLOW) {
                                        // deregister since save never
                                        // happened
                                        WorkflowAssetFactory.deRegisterAsset(WorkflowAsset.this);
                                    }
                                } else {
                                    getProject().getDesignerProxy().saveWorkflowAssetWithProgress(WorkflowAsset.this, keepLocked);
                                    fireElementChangeEvent(ChangeType.SETTINGS_CHANGE, null);
                                }
                                if (!keepLocked)
                                    fireElementChangeEvent(ChangeType.PROPERTIES_CHANGE, null);
                            } catch (Exception ex) {
                                PluginMessages.uiError(ex, "Save Definition Doc", getProject());
                            }
                        }
                    }
                });
            }
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) VersionableSaveDialog(com.centurylink.mdw.plugin.designer.dialogs.VersionableSaveDialog) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) MDWException(com.centurylink.mdw.common.exception.MDWException) IResourceDelta(org.eclipse.core.resources.IResourceDelta) Display(org.eclipse.swt.widgets.Display) RuleSetVO(com.centurylink.mdw.model.value.attribute.RuleSetVO) RunnerResult(com.centurylink.mdw.plugin.designer.DesignerRunner.RunnerResult)

Aggregations

VersionableSaveDialog (com.centurylink.mdw.plugin.designer.dialogs.VersionableSaveDialog)2 PartInitException (org.eclipse.ui.PartInitException)2 MDWException (com.centurylink.mdw.common.exception.MDWException)1 RuleSetVO (com.centurylink.mdw.model.value.attribute.RuleSetVO)1 TaskVO (com.centurylink.mdw.model.value.task.TaskVO)1 RunnerResult (com.centurylink.mdw.plugin.designer.DesignerRunner.RunnerResult)1 Increment (com.centurylink.mdw.plugin.designer.model.Versionable.Increment)1 TaskTemplateDocument (com.centurylink.mdw.task.TaskTemplateDocument)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 XmlOptions (org.apache.xmlbeans.XmlOptions)1 IFile (org.eclipse.core.resources.IFile)1 IResourceDelta (org.eclipse.core.resources.IResourceDelta)1 CoreException (org.eclipse.core.runtime.CoreException)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)1 Display (org.eclipse.swt.widgets.Display)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1