Search in sources :

Example 26 with XSDElementDeclaration

use of org.eclipse.xsd.XSDElementDeclaration in project tmdm-studio-se by Talend.

the class XSDDeleteElementAction method doAction.

public IStatus doAction() {
    try {
        // xsdElem is to support the multiple delete action on key press,
        // which each delete action on element must be explicit passed a xsdElem to
        // delete
        XSDElementDeclaration decl = xsdElem;
        if (decl == null) {
            ISelection selection = page.getTreeViewer().getSelection();
            decl = (XSDElementDeclaration) ((IStructuredSelection) selection).getFirstElement();
        }
        ArrayList<Object> objList = new ArrayList<Object>();
        IStructuredContentProvider provider = (IStructuredContentProvider) page.getTreeViewer().getContentProvider();
        Object[] all = Util.getAllObject(page.getSite(), objList, provider);
        Util.deleteReference(decl, all);
        // remove declaration
        schema.getContents().remove(decl);
        schema.update();
        xsdElem = null;
        page.refresh();
        page.markDirtyWithoutCommit();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDDeleteElementAction_ErrorMsg, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) ISelection(org.eclipse.jface.viewers.ISelection) ArrayList(java.util.ArrayList) IStructuredContentProvider(org.eclipse.jface.viewers.IStructuredContentProvider) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 27 with XSDElementDeclaration

use of org.eclipse.xsd.XSDElementDeclaration in project tmdm-studio-se by Talend.

the class XSDDeleteIdentityConstraintAction method doAction.

public IStatus doAction() {
    try {
        // xsdIdenty is to support the multiple delete action on key press,
        // which each delete action on identity must be explicit passed a xsd key to
        // delete
        XSDIdentityConstraintDefinition constraint = xsdIdenty;
        XSDElementDeclaration decl = null;
        if (constraint != null) {
            decl = (XSDElementDeclaration) constraint.getContainer();
            if (decl == null)
                return Status.CANCEL_STATUS;
        }
        if (decl == null) {
            IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
            constraint = (XSDIdentityConstraintDefinition) selection.getFirstElement();
            decl = (XSDElementDeclaration) constraint.getContainer();
        }
        /*
             * REMOVE so that simple elements can be made if (
             * (constraint.getIdentityConstraintCategory().equals(XSDIdentityConstraintCategory.UNIQUE_LITERAL)) &&
             * (decl.getContainer().equals(decl.getSchema())) ) { MessageDialog.openError(
             * this.page.getSite().getShell(), "Error", "Entities must have an unique key" ); return; }
             */
        decl.getIdentityConstraintDefinitions().remove(constraint);
        decl.updateElement();
        xsdIdenty = null;
        page.refresh();
        page.markDirty();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDDeleteXX_ErrorMsg, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 28 with XSDElementDeclaration

use of org.eclipse.xsd.XSDElementDeclaration in project tmdm-studio-se by Talend.

the class XSDEditConceptAction method doAction.

@Override
public IStatus doAction() {
    try {
        ISelection selection = page.getTreeViewer().getSelection();
        XSDElementDeclaration decl = (XSDElementDeclaration) ((IStructuredSelection) selection).getFirstElement();
        ArrayList<Object> objList = new ArrayList<Object>();
        IStructuredContentProvider provider = (IStructuredContentProvider) page.getTreeViewer().getContentProvider();
        String oldName = decl.getName();
        InputDialog id = new InputDialog(page.getSite().getShell(), Messages.XSDEditConceptAction_Text, Messages.XSDEditConceptAction_DialogTip, oldName, new IInputValidator() {

            public String isValid(String newText) {
                if ((newText == null) || "".equals(newText)) {
                    return Messages.XSDEditConceptAction_NameCannotBeEmpty;
                }
                if (// $NON-NLS-1$
                Pattern.compile("^\\s+\\w+\\s*").matcher(newText).matches() || newText.trim().replaceAll("\\s", "").length() != newText.trim().length()) {
                    return Messages.XSDEditConceptAction_NameCannotContainEmpty;
                }
                if (!XSDUtil.isValidatedXSDName(newText)) {
                    return Messages.InvalidName_Message;
                }
                EList list = schema.getElementDeclarations();
                for (Iterator iter = list.iterator(); iter.hasNext(); ) {
                    XSDElementDeclaration d = (XSDElementDeclaration) iter.next();
                    if (d.getName().equalsIgnoreCase(newText.trim())) {
                        return Messages.XSDEditConceptAction_EntityAlreadyExist;
                    }
                }
                return null;
            }
        });
        id.setBlockOnOpen(true);
        int ret = id.open();
        if (ret == Dialog.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        Object[] objs = Util.getAllObject(page.getSite(), objList, provider);
        Object[] allForeignKeyRelatedInfos = Util.getAllForeignKeyRelatedInfos(page.getSite(), new ArrayList<Object>(), provider, new HashSet<Object>());
        String newName = id.getValue().trim();
        decl.setName(newName);
        decl.updateElement();
        Util.updateReference(decl, objs, allForeignKeyRelatedInfos, oldName, newName);
        EntitySyncProcessor.syncNameForAnnotation(decl, oldName, newName);
        if (mapinfoExAdapter != null) {
            mapinfoExAdapter.renameEntityMapinfo(oldName, newName);
        }
        if (elementExAdapter != null) {
            elementExAdapter.renameEntityName(decl.getSchema(), oldName, newName);
        }
        // change unique key with new name of concept
        EList list = decl.getIdentityConstraintDefinitions();
        XSDIdentityConstraintDefinition toUpdate = null;
        for (Iterator iter = list.iterator(); iter.hasNext(); ) {
            XSDIdentityConstraintDefinition icd = (XSDIdentityConstraintDefinition) iter.next();
            if (icd.getName().equals(oldName)) {
                toUpdate = icd;
                break;
            }
        }
        if (toUpdate != null) {
            toUpdate.setName(newName);
            toUpdate.updateElement();
        }
        page.refresh();
        page.markDirty();
    // page.refreshPage();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDEditConceptAction_ErrorMsg, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : InputDialog(org.eclipse.jface.dialogs.InputDialog) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) ISelection(org.eclipse.jface.viewers.ISelection) IStructuredContentProvider(org.eclipse.jface.viewers.IStructuredContentProvider) Iterator(java.util.Iterator) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) IInputValidator(org.eclipse.jface.dialogs.IInputValidator)

Example 29 with XSDElementDeclaration

use of org.eclipse.xsd.XSDElementDeclaration in project tmdm-studio-se by Talend.

the class XSDEditParticleAction method doAction.

@Override
public IStatus doAction() {
    try {
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        String originalXpath = getOriginalXpath();
        // $NON-NLS-1$
        String entity = originalXpath.substring(0, originalXpath.indexOf("/"));
        selParticle = (XSDParticle) selection.getFirstElement();
        if (!(selParticle.getTerm() instanceof XSDElementDeclaration)) {
            return Status.CANCEL_STATUS;
        }
        XSDElementDeclaration decl = (XSDElementDeclaration) selParticle.getContent();
        XSDElementDeclaration ref = null;
        if (decl.isElementDeclarationReference()) {
            // it is a ref
            ref = decl.getResolvedElementDeclaration();
        }
        EList eDecls = decl.getSchema().getElementDeclarations();
        ArrayList<String> elementDeclarations = new ArrayList<String>();
        for (Iterator iter = eDecls.iterator(); iter.hasNext(); ) {
            XSDElementDeclaration d = (XSDElementDeclaration) iter.next();
            if (d.getTargetNamespace() != null && d.getTargetNamespace().equals(IConstants.DEFAULT_NAME_SPACE)) {
                continue;
            }
            if (!d.getQName().equals(entity)) {
                elementDeclarations.add(// $NON-NLS-1$ //$NON-NLS-2$
                d.getQName() + (d.getTargetNamespace() != null ? " : " + d.getTargetNamespace() : ""));
            }
        }
        // $NON-NLS-1$
        elementDeclarations.add("");
        XSDIdentityConstraintDefinition identify = null;
        XSDXPathDefinition keyPath = null;
        List<Object> keyInfo = Util.getKeyInfo(decl);
        boolean isPK = false;
        if (keyInfo != null && keyInfo.size() > 0) {
            identify = (XSDIdentityConstraintDefinition) keyInfo.get(0);
            keyPath = (XSDXPathDefinition) keyInfo.get(1);
            isPK = true;
        }
        initEleName = decl.getName();
        dialog = new BusinessElementInputDialog(this, page.getSite().getShell(), Messages.XSDEditParticleAction_InputDialogTitle, decl.getName(), ref == null ? null : ref.getQName(), elementDeclarations, selParticle.getMinOccurs(), selParticle.getMaxOccurs(), false, isPK);
        dialog.setBlockOnOpen(true);
        int ret = dialog.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        if (keyPath != null) {
            identify.getFields().remove(keyPath);
        }
        // find reference
        XSDElementDeclaration newRef = null;
        if (!"".equals(refName.trim())) {
            // $NON-NLS-1$
            newRef = Util.findReference(refName, schema);
            if (newRef == null) {
                MessageDialog.openError(this.page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDEditParticleAction_ErrorMsg, refName));
                return Status.CANCEL_STATUS;
            }
        }
        // ref
        // update validation rule
        Set<String> paths = new HashSet<String>();
        Util.collectElementPaths((IStructuredContentProvider) page.getElementsViewer().getContentProvider(), page.getSite(), selParticle, paths, null);
        // 
        // $NON-NLS-1$
        decl.setName("".equals(this.elementName) ? null : this.elementName);
        if (keyPath != null) {
            XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
            XSDXPathDefinition field = factory.createXSDXPathDefinition();
            field.setVariety(keyPath.getVariety());
            field.setValue(elementName);
            identify.getFields().add(field);
        }
        if (newRef != null) {
            decl.setResolvedElementDeclaration(newRef);
            decl.setTypeDefinition(null);
            Element elem = decl.getElement();
            if (elem.getAttributes().getNamedItem("type") != null) {
                // $NON-NLS-1$
                elem.getAttributes().removeNamedItem("type");
            }
            decl.updateElement();
        } else if (ref != null) {
            // fliu
            // no more element declarations --> we create a new Declaration with String simpleType definition
            // instead
            // FIXME: dereferecning and element is buggy
            XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
            XSDElementDeclaration newD = factory.createXSDElementDeclaration();
            newD.setName(this.elementName);
            newD.updateElement();
            XSDSimpleTypeDefinition stringType = ((SchemaTreeContentProvider) page.getTreeViewer().getContentProvider()).getXsdSchema().getSchemaForSchema().resolveSimpleTypeDefinition(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001, // $NON-NLS-1$
            "string");
            newD.setTypeDefinition(stringType);
            if (selParticle.getContainer() instanceof XSDModelGroup) {
                XSDModelGroup group = ((XSDModelGroup) selParticle.getContainer());
                ((XSDModelGroup) selParticle.getContainer()).getContents().remove(selParticle);
                selParticle = factory.createXSDParticle();
                selParticle.setContent(newD);
                group.getContents().add(selParticle);
            }
        }
        if (Util.changeElementTypeToSequence(decl, maxOccurs) == Status.CANCEL_STATUS) {
            return Status.CANCEL_STATUS;
        }
        selParticle.setMinOccurs(this.minOccurs);
        if (maxOccurs > -1) {
            selParticle.setMaxOccurs(this.maxOccurs);
        } else {
            if (selParticle.getElement().getAttributeNode("maxOccurs") != null) {
                // $NON-NLS-1$//$NON-NLS-2$
                selParticle.getElement().getAttributeNode("maxOccurs").setNodeValue("unbounded");
            } else {
                // $NON-NLS-1$//$NON-NLS-2$
                selParticle.getElement().setAttribute("maxOccurs", "unbounded");
            }
        }
        selParticle.updateElement();
        updateReference(originalXpath);
        if (elementExAdapter != null) {
            elementExAdapter.renameElement(decl.getSchema(), paths, decl.getName());
        }
        if (mapinfoExAdapter != null) {
            mapinfoExAdapter.renameElementMapinfo(paths, decl.getName());
        }
        page.refresh();
        page.markDirty();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDEditParticleAction_ErrorMsg1, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) Element(org.w3c.dom.Element) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) EList(org.eclipse.emf.common.util.EList) BusinessElementInputDialog(com.amalto.workbench.dialogs.BusinessElementInputDialog) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) Iterator(java.util.Iterator) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) XSDXPathDefinition(org.eclipse.xsd.XSDXPathDefinition) HashSet(java.util.HashSet)

Example 30 with XSDElementDeclaration

use of org.eclipse.xsd.XSDElementDeclaration in project tmdm-studio-se by Talend.

the class XSDAbstractNewXPathAction method updateElementForAddedfield.

protected void updateElementForAddedfield(XSDIdentityConstraintDefinition icd, String fieldName) {
    if (icd == null || fieldName == null)
        return;
    XSDElementDeclaration entity = (XSDElementDeclaration) icd.getContainer();
    XSDComplexTypeDefinition ctype = (XSDComplexTypeDefinition) entity.getTypeDefinition();
    if (ctype.getContent() instanceof XSDParticle) {
        XSDParticleImpl particle = (XSDParticleImpl) ctype.getContent();
        if (particle.getTerm() instanceof XSDModelGroup) {
            XSDModelGroup group = (XSDModelGroup) particle.getTerm();
            EList<XSDParticle> particles = group.getParticles();
            for (XSDParticle part : particles) {
                if (part.getTerm() instanceof XSDElementDeclaration) {
                    XSDElementDeclaration el = (XSDElementDeclaration) part.getTerm();
                    if (el.getTypeDefinition() instanceof XSDSimpleTypeDefinition) {
                        if (fieldName.equals(el.getName())) {
                            part.setMinOccurs(1);
                            part.setMaxOccurs(1);
                            break;
                        }
                    }
                }
            }
        }
    }
    entity.updateElement();
}
Also used : XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) XSDParticleImpl(org.eclipse.xsd.impl.XSDParticleImpl) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle)

Aggregations

XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)222 XSDParticle (org.eclipse.xsd.XSDParticle)103 XSDComplexTypeDefinition (org.eclipse.xsd.XSDComplexTypeDefinition)93 XSDModelGroup (org.eclipse.xsd.XSDModelGroup)87 ArrayList (java.util.ArrayList)60 XSDTypeDefinition (org.eclipse.xsd.XSDTypeDefinition)57 XSDSchema (org.eclipse.xsd.XSDSchema)48 XSDSimpleTypeDefinition (org.eclipse.xsd.XSDSimpleTypeDefinition)47 Test (org.junit.Test)44 XSDIdentityConstraintDefinition (org.eclipse.xsd.XSDIdentityConstraintDefinition)38 XSDFactory (org.eclipse.xsd.XSDFactory)37 XSDTerm (org.eclipse.xsd.XSDTerm)32 XSDConcreteComponent (org.eclipse.xsd.XSDConcreteComponent)30 EList (org.eclipse.emf.common.util.EList)29 XSDAnnotation (org.eclipse.xsd.XSDAnnotation)28 Iterator (java.util.Iterator)27 XSDAttributeDeclaration (org.eclipse.xsd.XSDAttributeDeclaration)27 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)24 XSDXPathDefinition (org.eclipse.xsd.XSDXPathDefinition)21 Element (org.w3c.dom.Element)21