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;
}
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);
}
}
Aggregations