Search in sources :

Example 36 with XSDXPathDefinition

use of org.eclipse.xsd.XSDXPathDefinition 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 37 with XSDXPathDefinition

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

the class XSDChangeToComplexTypeAction method createUniqueKey.

private void createUniqueKey(XSDFactory factory, XSDElementDeclaration declaration, XSDComplexTypeDefinition complexType) {
    List<String> fields = getPKFields(complexType);
    if (!fields.isEmpty()) {
        XSDIdentityConstraintDefinition uniqueKey = factory.createXSDIdentityConstraintDefinition();
        uniqueKey.setIdentityConstraintCategory(XSDIdentityConstraintCategory.UNIQUE_LITERAL);
        uniqueKey.setName(declaration.getName());
        XSDXPathDefinition selector = factory.createXSDXPathDefinition();
        selector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
        // $NON-NLS-1$
        selector.setValue(".");
        uniqueKey.setSelector(selector);
        for (String fieldName : fields) {
            XSDXPathDefinition field = factory.createXSDXPathDefinition();
            field.setVariety(XSDXPathVariety.FIELD_LITERAL);
            field.setValue(fieldName);
            uniqueKey.getFields().add(field);
        }
        declaration.getIdentityConstraintDefinitions().add(uniqueKey);
    }
}
Also used : XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) XSDXPathDefinition(org.eclipse.xsd.XSDXPathDefinition)

Aggregations

XSDXPathDefinition (org.eclipse.xsd.XSDXPathDefinition)37 XSDIdentityConstraintDefinition (org.eclipse.xsd.XSDIdentityConstraintDefinition)32 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)21 XSDComplexTypeDefinition (org.eclipse.xsd.XSDComplexTypeDefinition)16 ArrayList (java.util.ArrayList)15 XSDParticle (org.eclipse.xsd.XSDParticle)15 XSDModelGroup (org.eclipse.xsd.XSDModelGroup)14 XSDFactory (org.eclipse.xsd.XSDFactory)12 XSDSimpleTypeDefinition (org.eclipse.xsd.XSDSimpleTypeDefinition)12 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)10 XSDAnnotation (org.eclipse.xsd.XSDAnnotation)10 Iterator (java.util.Iterator)8 XSDTerm (org.eclipse.xsd.XSDTerm)8 Element (org.w3c.dom.Element)7 EList (org.eclipse.emf.common.util.EList)6 XSDAttributeUse (org.eclipse.xsd.XSDAttributeUse)6 XSDSchema (org.eclipse.xsd.XSDSchema)6 XSDTypeDefinition (org.eclipse.xsd.XSDTypeDefinition)6 TreeObject (com.amalto.workbench.models.TreeObject)5 XSDAttributeGroupDefinition (org.eclipse.xsd.XSDAttributeGroupDefinition)5