Search in sources :

Example 51 with XSDIdentityConstraintDefinition

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

the class XSDNewXPathAction method doAction.

@Override
public IStatus doAction() {
    try {
        int index = 0;
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        if (selection.getFirstElement() instanceof XSDIdentityConstraintDefinition) {
            icd = (XSDIdentityConstraintDefinition) selection.getFirstElement();
        } else if (selection.getFirstElement() instanceof XSDXPathDefinition) {
            XSDXPathDefinition xpath = (XSDXPathDefinition) selection.getFirstElement();
            icd = (XSDIdentityConstraintDefinition) xpath.getContainer();
            if (xpath.getVariety().equals(XSDXPathVariety.FIELD_LITERAL))
                index = icd.getFields().indexOf(xpath) + 1;
            else
                index = 0;
        } else {
            MessageDialog.openError(this.page.getSite().getShell(), Messages._Error, Messages.XSDNewXPathAction_Huhhh + selection.getFirstElement().getClass().getName());
            return Status.CANCEL_STATUS;
        }
        // InputDialog id = new InputDialog(
        // page.getSite().getShell(),
        // "New XPath",
        // "Enter a new XPath to the field",
        // null,
        // new IInputValidator() {
        // public String isValid(String newText) {
        // if ((newText==null) || "".equals(newText)) return "The XPath cannot be empty";
        // return null;
        // };
        // }
        // );
        // $NON-NLS-1$
        List<String> childNames = Util.getChildElementNames("", (XSDElementDeclaration) icd.getContainer());
        // filter the non top level fields
        List<String> topChilds = new ArrayList<String>();
        for (String child : childNames) {
            if (child.indexOf('/') == -1) {
                topChilds.add(child);
            }
        }
        // forbid to add already exists field
        EList<XSDXPathDefinition> fields = icd.getFields();
        for (XSDXPathDefinition fd : fields) {
            if (topChilds.contains(fd.getValue()))
                topChilds.remove(fd.getValue());
        }
        SelectFieldDialog id = new SelectFieldDialog(page.getSite().getShell(), Messages.XSDNewXPathAction_SelectOnField, topChilds, null);
        id.create();
        id.setBlockOnOpen(true);
        int ret = id.open();
        if (ret == Dialog.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        String field = id.getField();
        if (field.length() == 0)
            return Status.CANCEL_STATUS;
        XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
        XSDXPathDefinition xpath = factory.createXSDXPathDefinition();
        xpath.setValue(field);
        xpath.setVariety(XSDXPathVariety.FIELD_LITERAL);
        icd.getFields().add(index, xpath);
        icd.updateElement();
        updateElementForAddedfield(icd, field);
        page.refresh();
        page.getTreeViewer().setSelection(new StructuredSelection(xpath), true);
        page.markDirty();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.ErrorMsg, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) SelectFieldDialog(com.amalto.workbench.dialogs.SelectFieldDialog) ArrayList(java.util.ArrayList) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) XSDXPathDefinition(org.eclipse.xsd.XSDXPathDefinition)

Example 52 with XSDIdentityConstraintDefinition

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

the class XSDDeleteParticleAction method doAction.

public IStatus doAction() {
    try {
        // xsdPartle is to support the multiple delete action on key press,
        // which each delete action on particle must be explicit passed a xsd particle to
        // delete
        XSDParticle particle = (XSDParticle) xsdPartle;
        if (particle == null) {
            ISelection selection = page.getTreeViewer().getSelection();
            particle = (XSDParticle) ((IStructuredSelection) selection).getFirstElement();
        }
        XSDTerm term = particle.getTerm();
        XSDElementDeclaration decl = null;
        if (term instanceof XSDElementDeclaration) {
            decl = (XSDElementDeclaration) particle.getTerm();
        }
        if (particle.getContainer() == null) {
            return Status.CANCEL_STATUS;
        }
        XSDIdentityConstraintDefinition identify = null;
        XSDXPathDefinition keyPath = null;
        List<Object> keyInfo = Util.getKeyInfo(decl);
        if (keyInfo != null && keyInfo.size() > 0) {
            identify = (XSDIdentityConstraintDefinition) keyInfo.get(0);
            keyPath = (XSDXPathDefinition) keyInfo.get(1);
            identify.getFields().remove(keyPath);
            if (identify.getFields().size() == 0) {
                XSDElementDeclaration top = (XSDElementDeclaration) Util.getParent(decl);
                top.getIdentityConstraintDefinitions().remove(identify);
            }
        }
        if (!(particle.getContainer() instanceof XSDModelGroup))
            throw new XtentisException(Messages.bind(Messages.XSDDeleteParticleAction_ExceptionInfo, particle.getContainer().getClass().getName()));
        XSDModelGroup group = (XSDModelGroup) particle.getContainer();
        group.getContents().remove(particle);
        // if (term instanceof XSDElementDeclaration) {
        // //remove type definition is no more used and type is not built in
        // XSDTypeDefinition typeDef = decl.getTypeDefinition();
        // if ( (typeDef.getName()!=null) && //anonymous type
        // (!typeDef.getSchema().getSchemaForSchemaNamespace().equals(typeDef.getTargetNamespace()))
        // ){
        // if (Util.findElementsUsingType(group.getSchema(),typeDef.getTargetNamespace(),
        // typeDef.getName()).size()==0)
        // group.getSchema().getContents().remove(typeDef);
        // }
        // }
        group.updateElement();
        xsdPartle = null;
        page.refresh();
        page.markDirty();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDDeleteParticleAction_ErrorMsg, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) ISelection(org.eclipse.jface.viewers.ISelection) XSDTerm(org.eclipse.xsd.XSDTerm) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) XSDXPathDefinition(org.eclipse.xsd.XSDXPathDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) XtentisException(com.amalto.workbench.utils.XtentisException) XtentisException(com.amalto.workbench.utils.XtentisException)

Example 53 with XSDIdentityConstraintDefinition

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

the class XSDEditElementAction 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.XSDEditElementAction_EditElement, Messages.XSDEditElementAction_EnterNameForElement, oldName, new EditXSDEleDecNameValidator(schema));
        id.setBlockOnOpen(true);
        int ret = id.open();
        if (ret == Window.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>());
        decl.setName(id.getValue());
        decl.updateElement();
        Util.updateReference(decl, objs, allForeignKeyRelatedInfos, oldName, id.getValue());
        // 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(id.getValue());
            toUpdate.updateElement();
        }
        page.refresh();
        page.markDirty();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDEditElementAction_ErrorEditElement, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : InputDialog(org.eclipse.jface.dialogs.InputDialog) ArrayList(java.util.ArrayList) EditXSDEleDecNameValidator(com.amalto.workbench.utils.inputvalidator.EditXSDEleDecNameValidator) 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)

Example 54 with XSDIdentityConstraintDefinition

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

the class XSDNewIdentityConstraintAction method doAction.

@Override
public IStatus doAction() {
    try {
        int index = -1;
        // EList list = schema.getIdentityConstraintDefinitions();
        // schema.getElement();
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        List<String> childNames = new ArrayList<String>();
        if (selection.getFirstElement() instanceof XSDElementDeclaration) {
            decl = (XSDElementDeclaration) selection.getFirstElement();
        // childNames = Util.getChildElementNames(decl.getElement());
        } else if (selection.getFirstElement() instanceof XSDIdentityConstraintDefinition) {
            XSDIdentityConstraintDefinition selIcd = (XSDIdentityConstraintDefinition) selection.getFirstElement();
            decl = (XSDElementDeclaration) (selIcd.getContainer());
            // get position of the selected Identity Constraint in the container (Element Declaration)
            int i = 0;
            for (Iterator iter = decl.getIdentityConstraintDefinitions().iterator(); iter.hasNext(); ) {
                XSDIdentityConstraintDefinition ic = (XSDIdentityConstraintDefinition) iter.next();
                if (ic.equals(selIcd)) {
                    index = i;
                    break;
                }
                i++;
            }
        } else if (selection.getFirstElement() instanceof XSDParticle) {
            XSDParticle selParticle = (XSDParticle) selection.getFirstElement();
            if (!(selParticle.getTerm() instanceof XSDElementDeclaration))
                return Status.CANCEL_STATUS;
            decl = (XSDElementDeclaration) selParticle.getTerm();
        // childNames.add(decl.getName());
        } else {
            MessageDialog.openError(this.page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDNewIdentityConstraintAction_ErrorMsg, selection.getFirstElement().getClass().getName()));
            return Status.CANCEL_STATUS;
        }
        // $NON-NLS-1$
        childNames = Util.getChildElementNames("", decl);
        // filter the non top level fields
        List<String> topChilds = new ArrayList<String>();
        for (String child : childNames) {
            if (child.indexOf('/') == -1) {
                topChilds.add(child);
            }
        }
        dialog = new IdentityConstraintInputDialog(decl, page.getSite().getShell(), Messages.XSDNewIdentityConstraintAction_AddANewKey, topChilds, decl.getName());
        dialog.setBlockOnOpen(true);
        int ret = dialog.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        keyName = dialog.getKeyName();
        fieldName = dialog.getFieldName();
        type = dialog.getType();
        XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
        XSDIdentityConstraintDefinition icd = factory.createXSDIdentityConstraintDefinition();
        icd.setName(keyName);
        icd.setIdentityConstraintCategory(type);
        XSDXPathDefinition selector = factory.createXSDXPathDefinition();
        selector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
        // $NON-NLS-1$
        selector.setValue(".");
        icd.setSelector(selector);
        XSDXPathDefinition field = factory.createXSDXPathDefinition();
        field.setVariety(XSDXPathVariety.FIELD_LITERAL);
        // $NON-NLS-1$
        field.setValue(".");
        // if complex content set name of first field
        if (fieldName == null || fieldName.trim().length() == 0) {
            if (decl.getTypeDefinition() instanceof XSDComplexTypeDefinition) {
                XSDComplexTypeContent ctc = ((XSDComplexTypeDefinition) decl.getTypeDefinition()).getContent();
                if (ctc instanceof XSDParticle) {
                    if (((XSDParticle) ctc).getTerm() instanceof XSDModelGroup) {
                        XSDModelGroup mg = (XSDModelGroup) ((XSDParticle) ctc).getTerm();
                        if (mg.getContents().size() > 0)
                            if (mg.getContents().get(0).getTerm() instanceof XSDElementDeclaration)
                                field.setValue(((XSDElementDeclaration) (mg.getContents().get(0).getTerm())).getName());
                    }
                }
            }
        } else {
            field.setValue(fieldName);
        }
        icd.getFields().add(field);
        decl.getIdentityConstraintDefinitions().add(index + 1, icd);
        decl.updateElement();
        updateElementForAddedfield(icd, fieldName);
        page.refresh();
        page.getTreeViewer().setSelection(new StructuredSelection(icd), true);
        page.markDirty();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDNewIdentityConstraintAction_ErrorMsg2, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDComplexTypeContent(org.eclipse.xsd.XSDComplexTypeContent) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) ArrayList(java.util.ArrayList) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IdentityConstraintInputDialog(com.amalto.workbench.dialogs.IdentityConstraintInputDialog) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) Iterator(java.util.Iterator) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) XSDXPathDefinition(org.eclipse.xsd.XSDXPathDefinition) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle)

Example 55 with XSDIdentityConstraintDefinition

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

the class XSDChangeToComplexTypeAction method removeExistUniqueKey.

private void removeExistUniqueKey(XSDElementDeclaration declaration) {
    List<XSDIdentityConstraintDefinition> keys = new ArrayList<XSDIdentityConstraintDefinition>();
    EList<XSDIdentityConstraintDefinition> list = declaration.getIdentityConstraintDefinitions();
    for (XSDIdentityConstraintDefinition icd : list) {
        if (icd.getIdentityConstraintCategory().equals(XSDIdentityConstraintCategory.UNIQUE_LITERAL)) {
            keys.add(icd);
        }
    }
    declaration.getIdentityConstraintDefinitions().removeAll(keys);
}
Also used : ArrayList(java.util.ArrayList) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition)

Aggregations

XSDIdentityConstraintDefinition (org.eclipse.xsd.XSDIdentityConstraintDefinition)56 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)38 XSDXPathDefinition (org.eclipse.xsd.XSDXPathDefinition)32 XSDComplexTypeDefinition (org.eclipse.xsd.XSDComplexTypeDefinition)21 ArrayList (java.util.ArrayList)20 XSDParticle (org.eclipse.xsd.XSDParticle)19 XSDModelGroup (org.eclipse.xsd.XSDModelGroup)18 XSDSimpleTypeDefinition (org.eclipse.xsd.XSDSimpleTypeDefinition)18 Iterator (java.util.Iterator)15 XSDTypeDefinition (org.eclipse.xsd.XSDTypeDefinition)14 XSDAnnotation (org.eclipse.xsd.XSDAnnotation)13 XSDAttributeGroupDefinition (org.eclipse.xsd.XSDAttributeGroupDefinition)13 XSDFactory (org.eclipse.xsd.XSDFactory)13 XSDSchema (org.eclipse.xsd.XSDSchema)13 EList (org.eclipse.emf.common.util.EList)12 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)11 XSDAttributeDeclaration (org.eclipse.xsd.XSDAttributeDeclaration)10 XSDTerm (org.eclipse.xsd.XSDTerm)10 Element (org.w3c.dom.Element)10 TreeObject (com.amalto.workbench.models.TreeObject)9