use of com.amalto.workbench.dialogs.BusinessElementInputDialog 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;
}
use of com.amalto.workbench.dialogs.BusinessElementInputDialog in project tmdm-studio-se by Talend.
the class XSDNewParticleFromTypeAction method doAction.
@Override
public IStatus doAction() {
try {
IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
if (selection.getFirstElement() instanceof XSDComplexTypeDefinition) {
ctd = (XSDComplexTypeDefinition) selection.getFirstElement();
if (!(ctd.getContent() instanceof XSDParticle)) {
return Status.CANCEL_STATUS;
}
if (!(((XSDParticle) ctd.getContent()).getTerm() instanceof XSDModelGroup)) {
return Status.CANCEL_STATUS;
}
;
group = (XSDModelGroup) ((XSDParticle) ctd.getContent()).getTerm();
} else if (selection.getFirstElement() instanceof XSDParticle) {
group = (XSDModelGroup) ((XSDParticle) selection.getFirstElement()).getTerm();
} else if (selection.getFirstElement() instanceof XSDModelGroup) {
group = (XSDModelGroup) selection.getFirstElement();
} else {
log.info(Messages.bind(Messages._UnkownSection, selection.getFirstElement().getClass().getName(), selection.getFirstElement().toString()));
return Status.CANCEL_STATUS;
}
EList<XSDElementDeclaration> eDecls = schema.getElementDeclarations();
List<String> elementDeclarations = new LinkedList<String>();
for (XSDElementDeclaration xsdElementDeclaration : eDecls) {
XSDElementDeclaration d = xsdElementDeclaration;
if (d.getTargetNamespace() != null && d.getTargetNamespace().equals(IConstants.DEFAULT_NAME_SPACE)) {
continue;
}
// $NON-NLS-1$//$NON-NLS-2$
elementDeclarations.add(d.getQName() + (d.getTargetNamespace() != null ? " : " + d.getTargetNamespace() : ""));
}
// $NON-NLS-1$
elementDeclarations.add("");
dialog = new BusinessElementInputDialog(this, page.getSite().getShell(), Messages._AddANewBusinessElement, "", "", elementDeclarations, 0, 1, true, // $NON-NLS-1$//$NON-NLS-2$;
false);
dialog.setBlockOnOpen(true);
int ret = dialog.open();
if (ret == Dialog.CANCEL) {
return Status.CANCEL_STATUS;
}
XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
XSDElementDeclaration decl = factory.createXSDElementDeclaration();
decl.setName(this.elementName);
// simpleTypeName));
if (!refName.equals("")) {
// $NON-NLS-1$
XSDElementDeclaration ref = Util.findReference(refName, schema);
if (ref != null) {
decl.setResolvedElementDeclaration(ref);
}
} else {
decl.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), simpleTypeName));
}
XSDParticle particle = factory.createXSDParticle();
particle.setContent(decl);
particle.setMinOccurs(this.minOccurs);
particle.setMaxOccurs(this.maxOccurs);
group.getContents().add(group.getContents().size(), particle);
group.updateElement();
if (Util.changeElementTypeToSequence(decl, maxOccurs) == Status.CANCEL_STATUS) {
return Status.CANCEL_STATUS;
}
if (dialog.isInherit()) {
XSDTerm totm = particle.getTerm();
XSDElementDeclaration concept = null;
Object obj = Util.getParent(particle);
if (obj instanceof XSDElementDeclaration) {
concept = (XSDElementDeclaration) obj;
} else {
concept = (XSDElementDeclaration) particle.getContent();
}
XSDAnnotation fromannotation = null;
if (concept != null) {
fromannotation = concept.getAnnotation();
}
if (fromannotation != null) {
XSDAnnotationsStructure struc = new XSDAnnotationsStructure(totm);
if (((XSDElementDeclaration) totm).getType() != null) {
addAnnotion(struc, fromannotation);
}
}
}
page.refresh();
page.getTreeViewer().setSelection(new StructuredSelection(particle), true);
page.markDirty();
} catch (Exception e) {
log.error(e.getMessage(), e);
MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages._ErrorCreatBusinessElement, e.getLocalizedMessage()));
return Status.CANCEL_STATUS;
}
return Status.OK_STATUS;
}
use of com.amalto.workbench.dialogs.BusinessElementInputDialog in project tmdm-studio-se by Talend.
the class XSDNewParticleFromParticleAction method doAction.
public IStatus doAction() {
try {
IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
selParticle = (XSDParticle) selection.getFirstElement();
if (!(selParticle.getContainer() instanceof XSDModelGroup))
return Status.CANCEL_STATUS;
;
XSDModelGroup group = (XSDModelGroup) selParticle.getContainer();
// get position of the selected particle in the container
int index = 0;
int i = 0;
for (Iterator<XSDParticle> iter = group.getContents().iterator(); iter.hasNext(); ) {
XSDParticle p = (XSDParticle) iter.next();
if (p.equals(selParticle)) {
index = i;
break;
}
i++;
}
EList<XSDElementDeclaration> eDecls = schema.getElementDeclarations();
List<String> elementDeclarations = new ArrayList<String>();
for (Iterator<XSDElementDeclaration> iter = eDecls.iterator(); iter.hasNext(); ) {
XSDElementDeclaration d = (XSDElementDeclaration) iter.next();
if (d.getTargetNamespace() != null && d.getTargetNamespace().equals(IConstants.DEFAULT_NAME_SPACE))
continue;
// $NON-NLS-1$//$NON-NLS-2$
elementDeclarations.add(d.getQName() + (d.getTargetNamespace() != null ? " : " + d.getTargetNamespace() : ""));
}
// $NON-NLS-1$
elementDeclarations.add("");
dialog = new BusinessElementInputDialog(this, page.getSite().getShell(), Messages._AddANewBusinessElement, null, null, elementDeclarations, 0, 1, true, false);
dialog.setBlockOnOpen(true);
int ret = dialog.open();
if (ret == Dialog.CANCEL) {
return Status.CANCEL_STATUS;
}
XSDElementDeclaration elem = (XSDElementDeclaration) selParticle.getContent();
if (Util.changeElementTypeToSequence(elem, maxOccurs) == Status.CANCEL_STATUS) {
return Status.CANCEL_STATUS;
}
XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
XSDElementDeclaration decl = factory.createXSDElementDeclaration();
decl.setName(this.elementName);
if (!refName.equals("")) {
// $NON-NLS-1$
XSDElementDeclaration ref = Util.findReference(refName, schema);
if (ref != null) {
decl.setResolvedElementDeclaration(ref);
}
} else {
decl.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), simpleTypeName));
}
XSDParticle particle = factory.createXSDParticle();
particle.setContent(decl);
particle.setMinOccurs(this.minOccurs);
if (maxOccurs > -1) {
particle.setMaxOccurs(this.maxOccurs);
} else {
particle.setMaxOccurs(this.maxOccurs);
group.getContents().add(group.getContents().size(), particle);
group.updateElement();
if (// $NON-NLS-1$
particle.getElement().getAttributeNode("maxOccurs") != null)
// $NON-NLS-1$//$NON-NLS-2$
particle.getElement().getAttributeNode("maxOccurs").setNodeValue("unbounded");
else {
// $NON-NLS-1$//$NON-NLS-2$
particle.getElement().setAttribute("maxOccurs", "unbounded");
}
}
if (maxOccurs > -1) {
group.getContents().add(group.getContents().size(), particle);
group.updateElement();
}
if (dialog.isInherit()) {
XSDTerm totm = particle.getTerm();
XSDElementDeclaration concept = null;
if (Util.getParent(selParticle) instanceof XSDElementDeclaration)
concept = (XSDElementDeclaration) Util.getParent(selParticle);
else if (Util.getParent(selParticle) instanceof XSDComplexTypeDefinition) {
if (selParticle instanceof XSDParticle)
concept = (XSDElementDeclaration) ((XSDParticle) selParticle).getContent();
else if (selParticle instanceof XSDElementDeclaration)
concept = (XSDElementDeclaration) selParticle;
}
XSDAnnotation fromannotation = null;
if (concept != null)
fromannotation = concept.getAnnotation();
if (fromannotation != null) {
XSDAnnotationsStructure struc = new XSDAnnotationsStructure(totm);
if (((XSDElementDeclaration) totm).getType() != null)
addAnnotion(struc, fromannotation);
}
}
page.refresh();
page.getTreeViewer().setSelection(new StructuredSelection(particle), true);
page.markDirty();
} catch (Exception e) {
log.error(e.getMessage(), e);
MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages._ErrorCreatBusinessElement, e.getLocalizedMessage()));
return Status.CANCEL_STATUS;
}
return Status.OK_STATUS;
}
Aggregations