Search in sources :

Example 1 with IdentityConstraintInputDialog

use of com.amalto.workbench.dialogs.IdentityConstraintInputDialog 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)

Aggregations

IdentityConstraintInputDialog (com.amalto.workbench.dialogs.IdentityConstraintInputDialog)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)1 XSDComplexTypeContent (org.eclipse.xsd.XSDComplexTypeContent)1 XSDComplexTypeDefinition (org.eclipse.xsd.XSDComplexTypeDefinition)1 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)1 XSDFactory (org.eclipse.xsd.XSDFactory)1 XSDIdentityConstraintDefinition (org.eclipse.xsd.XSDIdentityConstraintDefinition)1 XSDModelGroup (org.eclipse.xsd.XSDModelGroup)1 XSDParticle (org.eclipse.xsd.XSDParticle)1 XSDXPathDefinition (org.eclipse.xsd.XSDXPathDefinition)1