use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.
the class ModelUtil method getSchemaInfo.
/**
* Parse SchemaInfo message TreeNode and return SchemaInfo Object
*
* @param classinfo TreeNode
* @return SchemaInfo
*/
public static SchemaInfo getSchemaInfo(TreeNode classinfo, DatabaseInfo dbInfo) {
SchemaInfo schema = new SchemaInfo();
SocketTask.setFieldValue(classinfo, schema);
/*Process the type for CMS 9.0, If the CMS fixed the bug. It can be remove.*/
if (CompatibleUtil.isSupportEnumVersion(dbInfo)) {
List<String> enumColumnList = new ArrayList<String>();
for (DBAttribute attribute : schema.getAttributes()) {
String type = attribute.getType();
type = processFiledTypeForCMS9(type);
if (DataType.DATATYPE_ENUM.equalsIgnoreCase(type)) {
enumColumnList.add(attribute.getName());
}
attribute.setType(type);
}
if (enumColumnList.size() > 0) {
Connection connection = null;
Statement stmt = null;
ResultSet rs = null;
try {
connection = JDBCConnectionManager.getConnection(dbInfo, true);
String escapedTableName = QuerySyntax.escapeKeyword(schema.getClassname());
StringBuilder sb = new StringBuilder();
sb.append("SHOW COLUMNS FROM ").append(escapedTableName).append(" WHERE FIELD IN (");
stmt = connection.createStatement();
for (int i = 0; i < enumColumnList.size(); i++) {
sb.append("'").append(enumColumnList.get(i)).append("'");
if (i + 1 < enumColumnList.size()) {
sb.append(",");
}
}
sb.append(");");
rs = stmt.executeQuery(sb.toString());
while (rs.next()) {
String name = rs.getString("Field");
String type = rs.getString("Type");
DBAttribute attr = schema.getDBAttributeByName(name, false);
attr.setEnumeration(StringUtil.getEnumeration(type));
}
} catch (Exception e) {
LOGGER.error("", e);
} finally {
QueryUtil.freeQuery(connection, stmt, rs);
}
}
}
return schema;
}
use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.
the class CubridWorkbenchContrItem method openEditorOrView.
/**
* Open and reopen the editor or view part of this CUBRID node
*
* @param cubridNode the ICubridNode object
*/
public void openEditorOrView(ICubridNode cubridNode) {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window == null) {
return;
}
if (cubridNode instanceof ISchemaNode) {
ISchemaNode schemaNode = (ISchemaNode) cubridNode;
if (schemaNode.getDatabase() != null && !schemaNode.getDatabase().isLogined()) {
return;
}
}
//close the editor part that has been open
String editorId = cubridNode.getEditorId();
String viewId = cubridNode.getViewId();
IWorkbenchPart workbenchPart = null;
if (editorId != null && editorId.trim().length() > 0) {
IEditorPart editorPart = LayoutUtil.getEditorPart(cubridNode, editorId);
if (editorPart != null) {
window.getActivePage().closeEditor(editorPart, false);
}
} else if (viewId != null && viewId.trim().length() > 0) {
IViewPart viewPart = LayoutUtil.getViewPart(cubridNode, viewId);
if (viewPart != null) {
// monitor view part do not need to close and then open
if (MONITOR_VIEWID_LST.contains(viewId)) {
workbenchPart = viewPart;
window.getActivePage().bringToTop(viewPart);
} else {
window.getActivePage().hideView(viewPart);
}
}
}
String nodeType = cubridNode.getType();
ISelectionAction logViewAction = null;
if (NodeType.contains(nodeType, new String[] { CubridNodeType.BROKER_SQL_LOG, CubridNodeType.LOGS_BROKER_ACCESS_LOG, CubridNodeType.LOGS_BROKER_ERROR_LOG, CubridNodeType.LOGS_BROKER_ADMIN_LOG, CubridNodeType.LOGS_SERVER_DATABASE_LOG })) {
logViewAction = (ISelectionAction) ActionManager.getInstance().getAction(LogViewAction.ID);
((LogViewAction) logViewAction).setCubridNode(cubridNode);
} else if (NodeType.contains(nodeType, new String[] { CubridNodeType.LOGS_MANAGER_ACCESS_LOG, CubridNodeType.LOGS_MANAGER_ERROR_LOG })) {
logViewAction = (ISelectionAction) ActionManager.getInstance().getAction(ManagerLogViewAction.ID);
((ManagerLogViewAction) logViewAction).setCubridNode(cubridNode);
}
if (logViewAction != null && logViewAction.isSupported(cubridNode)) {
logViewAction.run();
return;
}
if (!StringUtil.isEmpty(editorId)) {
try {
if (cubridNode instanceof ISchemaNode) {
CubridDatabase database = ((ISchemaNode) cubridNode).getDatabase();
// Judge database is started and open DatabaseDashboardEditor
if (StringUtil.isEqual(editorId, DatabaseDashboardEditor.ID)) {
if (!DbRunningType.CS.equals(database.getRunningType())) {
return;
}
}
// if open the table schema editor,firstly load the schema
if (StringUtil.isEqual(editorId, SchemaInfoEditorPart.ID)) {
SchemaInfo newSchema = database.getDatabaseInfo().getSchemaInfo(cubridNode.getName());
if (newSchema == null) {
CommonUITool.openErrorBox(database.getDatabaseInfo().getErrorMessage());
return;
}
}
}
workbenchPart = window.getActivePage().openEditor(cubridNode, editorId, true, IWorkbenchPage.MATCH_ID & IWorkbenchPage.MATCH_INPUT);
} catch (PartInitException e) {
LOGGER.error("", e);
}
} else if (viewId != null && viewId.trim().length() > 0) {
try {
if (MONITOR_VIEWID_LST.contains(viewId)) {
if (workbenchPart == null) {
String secondId = LayoutUtil.getViewSecondId(cubridNode);
workbenchPart = window.getActivePage().showView(viewId, secondId, IWorkbenchPage.VIEW_CREATE | IWorkbenchPage.VIEW_ACTIVATE | IWorkbenchPage.VIEW_VISIBLE);
window.getActivePage().bringToTop(workbenchPart);
}
} else {
workbenchPart = window.getActivePage().showView(viewId);
}
} catch (Exception e) {
LOGGER.error("", e);
}
}
if (workbenchPart != null) {
LayoutManager.getInstance().getTitleLineContrItem().changeTitleForViewOrEditPart(cubridNode, workbenchPart);
LayoutManager.getInstance().getStatusLineContrItem().changeStuatusLineForViewOrEditPart(cubridNode, workbenchPart);
}
}
use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.
the class TableEditorPart method deleteColumn.
private void deleteColumn() {
if (!CommonUITool.openConfirmBox(Messages.msgDeleteColumnConfirm)) {
return;
}
TableItem[] tblItems = columnsTable.getSelection();
if (tblItems.length > 0) {
DBAttribute attr = (DBAttribute) tblItems[0].getData();
List<DBAttribute> items = newSchemaInfo.getAttributes();
if (!items.contains(attr)) {
return;
}
}
TableItem[] selection = columnsTable.getSelection();
int selectionIndex = columnsTable.getSelectionIndex();
if (selection != null && selection.length >= 1) {
List<String> attrNames = new ArrayList<String>();
for (int m = 0; m < selection.length; m++) {
attrNames.add(m, selection[m].getText(1));
}
List<SchemaInfo> allSupers = SuperClassUtil.getSuperClasses(database.getDatabaseInfo(), newSchemaInfo);
Constraint pk = newSchemaInfo.getPK(allSupers);
List<String> pkAttributes = pk == null ? new ArrayList<String>() : pk.getAttributes();
boolean hasPk = false;
for (String pkAttribute : pkAttributes) {
if (attrNames.contains(pkAttribute)) {
hasPk = true;
break;
}
}
if (hasPk) {
if (attrNames.containsAll(pkAttributes)) {
newSchemaInfo.removeConstraintByName(pk.getName(), Constraint.ConstraintType.PRIMARYKEY.getText());
} else {
CommonUITool.openErrorBox(Messages.errColumnNotDropForPk);
return;
}
}
for (PartitionInfo partitionInfo : newSchemaInfo.getPartitionList()) {
String partitionExpr = partitionInfo.getPartitionExpr();
if (StringUtil.isNotEmpty(partitionExpr)) {
if (partitionExpr.startsWith("[") && partitionExpr.endsWith("]")) {
partitionExpr = partitionExpr.substring(1, partitionExpr.length() - 1);
if (attrNames.contains(partitionExpr)) {
CommonUITool.openErrorBox(Messages.errDropForPartitonColumn);
return;
}
} else {
if (attrNames.contains(partitionExpr)) {
CommonUITool.openErrorBox(Messages.errDropForPartitonColumn);
return;
}
}
}
}
for (TableItem selec : selection) {
DBAttribute oldAttribute = (DBAttribute) selec.getData();
if (oldAttribute == null) {
continue;
}
if (!oldAttribute.getInherit().equals(newSchemaInfo.getClassname())) {
CommonUITool.openErrorBox(Messages.errColumnNotDrop);
return;
}
if (oldAttribute.isClassAttribute()) {
newSchemaInfo.getClassAttributes().remove(oldAttribute);
} else {
newSchemaInfo.getAttributes().remove(oldAttribute);
// newSchemaInfo.removeUniqueByAttrName(selec.getText(1));
// For bug TOOLS-2390 After delete a column of a table,some
// related index doesn't been deleted.
newSchemaInfo.removeConstraintByAttrName(oldAttribute.getName());
indexTableView.setInput(newSchemaInfo);
fkTableView.setInput(newSchemaInfo);
}
SuperClassUtil.fireSuperClassChanged(database.getDatabaseInfo(), oldSchemaInfo, newSchemaInfo, newSchemaInfo.getSuperClasses());
String oldAttrName = oldAttribute.getName();
addDropAttrLog(oldAttrName, oldAttribute.isClassAttribute());
}
attrLabelProvider.setSchema(newSchemaInfo);
loadColumnData();
int itemCount = columnsTable.getItemCount();
columnsTable.select(selectionIndex < itemCount ? selectionIndex : selectionIndex - 1);
columnsTable.setFocus();
}
}
use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.
the class TableEditorPart method init.
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
super.init(site, input);
TableEditorInput tableInfoEditorInput = (TableEditorInput) input;
this.database = tableInfoEditorInput.getDatabase();
this.editedTableNode = tableInfoEditorInput.getEditedTableNode();
this.isNewTableFlag = tableInfoEditorInput.isNewTableFlag();
this.dbUserList = tableInfoEditorInput.getDbUserList();
this.showDefaultType = tableInfoEditorInput.getType();
this.collationList = tableInfoEditorInput.getCollationList();
if (collationList != null) {
Collation emptyCollation = new Collation();
emptyCollation.setCharset("");
emptyCollation.setName("");
collationList.add(0, emptyCollation);
}
this.oldSchemaInfo = tableInfoEditorInput.getSchemaInfo();
this.supportCharset = CompatibleUtil.isSupportCreateDBByCharset(database.getDatabaseInfo());
if (isNewTableFlag) {
newSchemaInfo = new SchemaInfo();
//$NON-NLS-1$
newSchemaInfo.setClassname("");
newSchemaInfo.setOwner(database.getUserName());
newSchemaInfo.setDbname(database.getName());
newSchemaInfo.setType(Messages.userSchema);
newSchemaInfo.setVirtual(Messages.schemaTypeClass);
if (database.getDatabaseInfo() != null) {
newSchemaInfo.setCollation(database.getDatabaseInfo().getCollation());
}
} else {
newSchemaInfo = null;
if (tableInfoEditorInput.getSchemaInfo() != null) {
newSchemaInfo = tableInfoEditorInput.getSchemaInfo().clone();
originalConstraints.addAll(newSchemaInfo.getConstraints());
}
}
if (supportCharset) {
columnProperites = new String[] { IAttributeColumn.COL_EMPTY, IAttributeColumn.COL_FLAG, IAttributeColumn.COL_NAME, IAttributeColumn.COL_DATATYPE, IAttributeColumn.COL_DEFAULT, IAttributeColumn.COL_AUTO_INCREMENT, IAttributeColumn.COL_NOT_NULL, IAttributeColumn.COL_PK, IAttributeColumn.COL_UK, IAttributeColumn.COL_SHARED, IAttributeColumn.COL_COLLATION, IAttributeColumn.COL_MEMO };
} else {
columnProperites = new String[] { IAttributeColumn.COL_EMPTY, IAttributeColumn.COL_FLAG, IAttributeColumn.COL_NAME, IAttributeColumn.COL_DATATYPE, IAttributeColumn.COL_DEFAULT, IAttributeColumn.COL_AUTO_INCREMENT, IAttributeColumn.COL_NOT_NULL, IAttributeColumn.COL_PK, IAttributeColumn.COL_UK, IAttributeColumn.COL_SHARED, IAttributeColumn.COL_MEMO };
}
Connection conn = null;
try {
conn = JDBCConnectionManager.getConnection(database.getDatabaseInfo(), false);
IDatabaseSpec dbSpec = database.getDatabaseInfo();
isSupportTableComment = SchemaCommentHandler.isInstalledMetaTable(dbSpec, conn);
database.getDatabaseInfo().setSupportTableComment(isSupportTableComment);
if (isSupportTableComment && !isNewTableFlag && newSchemaInfo != null) {
Map<String, SchemaComment> map = SchemaCommentHandler.loadDescription(dbSpec, conn, newSchemaInfo.getClassname());
for (DBAttribute attr : newSchemaInfo.getAttributes()) {
SchemaComment schemaComment = SchemaCommentHandler.find(map, newSchemaInfo.getClassname(), attr.getName());
if (schemaComment != null) {
attr.setDescription(schemaComment.getDescription());
}
}
// get description for index
for (Constraint cons : newSchemaInfo.getConstraints()) {
if (CompatibleUtil.isCommentSupports(dbSpec)) {
String indexName = cons.getName();
SchemaComment indexComment = SchemaCommentHandler.loadObjectDescription(dbSpec, conn, indexName, CommentType.INDEX);
if (indexComment != null) {
cons.setDescription(indexComment.getDescription());
}
}
}
SchemaComment schemaComment = SchemaCommentHandler.find(map, newSchemaInfo.getClassname(), null);
if (schemaComment != null) {
newSchemaInfo.setDescription(schemaComment.getDescription());
}
}
} catch (SQLException e) {
LOGGER.error("", e);
} catch (Exception e) {
LOGGER.error("", e);
} finally {
QueryUtil.freeQuery(conn);
}
schemaChangeMgr = new SchemaChangeManager(database.getDatabaseInfo(), isNewTableFlag);
schemaDDL = new SchemaDDL(schemaChangeMgr, database.getDatabaseInfo());
if (database != null) {
isSupportChange = CompatibleUtil.isSupportChangeColumn(database.getDatabaseInfo());
}
setSite(site);
setInput(input);
setPartName(input.getName());
setTitleToolTip(input.getName());
setTitleImage(CommonUIPlugin.getImage("icons/navigator/schema_table.png"));
}
use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.
the class ExportSchemaTask method execute.
public void execute() {
// FIXME logic code move to core module
entityMap.clear();
relationshipMap.clear();
entityAttrMap.clear();
ERwin4 root = new ERwin4();
Model model = new Model();
// DEFUALT database type "ODBC" "194"
model.setTargetServer("194");
model.setDbmsVersion("3");
model.setModelProps(new Model.ModelProps());
model.getModelProps().setDbmsVersion(new DBMSVersion());
model.getModelProps().getDbmsVersion().setValue("3");
model.getModelProps().setTargetServer(new TargetServer());
model.getModelProps().getTargetServer().setValue("194");
model.setId(getUUID());
root.setModel(model);
RelationshipGroups relationShipGroups = new RelationshipGroups();
model.setRelationshipGroups(relationShipGroups);
model.setDefaultValueGroups(new DefaultValueGroups());
EntityGroups groups = new EntityGroups();
model.setEntityGroups(groups);
for (Entry<String, SchemaInfo> entry : allSchemaInfos.entrySet()) {
String physicalTableName = entry.getKey();
String logicalTableName = physicalTableName;
if (tablePLMap != null) {
String logical = tablePLMap.get(physicalTableName);
logicalTableName = StringUtil.isEmpty(logical) ? physicalTableName : logical;
}
SchemaInfo schemaInfo = entry.getValue();
if (schemaInfo.isSystemClass()) {
continue;
}
Entity entity = new Entity();
groups.getEntity().add(entity);
entity.setId(getUUID());
entity.setName(logicalTableName);
entityMap.put(physicalTableName, entity);
entityAttrMap.put(physicalTableName, new HashMap<String, Attribute>());
EntityPropsList entityPropsList = new EntityPropsList();
entity.setEntityProps(entityPropsList);
entityPropsList.setPhysicalName(new EntityPropsList.PhysicalName());
entityPropsList.getPhysicalName().setValue(physicalTableName);
Type type = new Type();
if (schemaInfo.getVirtual().equals(ClassType.NORMAL.getText())) {
type.setValue(ERXmlModelConstant.ENTITYTYPE_TABLE_STR);
} else {
type.setValue(ERXmlModelConstant.ENTITYTYPE_VIEW_STR);
}
entityPropsList.setType(type);
Name nameEntityProps = new Name();
nameEntityProps.setValue(logicalTableName);
entityPropsList.setName(nameEntityProps);
List<DBAttribute> attributes = schemaInfo.getAttributes();
AttributeGroups attributeGroups = new AttributeGroups();
entity.setAttributeGroups(attributeGroups);
Map<String, Attribute> schemaAttrMap = new HashMap<String, Attribute>();
int attrOrder = 1;
for (DBAttribute dbAttr : attributes) {
Attribute attribute = new Attribute();
attributeGroups.getAttribute().add(attribute);
attribute.setId(getUUID());
String logicalAttrName = getLogicalAttrName(physicalTableName, dbAttr.getName());
if (StringUtil.isEmpty(logicalAttrName)) {
logicalAttrName = dbAttr.getName();
}
attribute.setName(logicalAttrName);
schemaAttrMap.put(dbAttr.getName(), attribute);
AttributePropsList attributePropsList = new AttributePropsList();
attribute.setAttributeProps(attributePropsList);
attributePropsList.setPhysicalName(new PhysicalName());
attributePropsList.getPhysicalName().setValue(dbAttr.getName());
com.cubrid.common.ui.cubrid.database.erwin.xmlmodel.AttributePropsList.Type attrType = new com.cubrid.common.ui.cubrid.database.erwin.xmlmodel.AttributePropsList.Type();
attributePropsList.setType(attrType);
if (dbAttr.isNotNull() && dbAttr.isUnique()) {
attrType.setValue("" + ERXmlModelConstant.ATTRIBUTE_TYPE_PK);
} else {
attrType.setValue("100");
}
Datatype dataType = new Datatype();
String typeValue = dbAttr.getType();
if (!StringUtil.isEmpty(dbAttr.getEnumeration())) {
typeValue += dbAttr.getEnumeration();
}
dataType.setValue(typeValue);
attributePropsList.setDataType(dataType);
com.cubrid.common.ui.cubrid.database.erwin.xmlmodel.AttributePropsList.Name propName = new com.cubrid.common.ui.cubrid.database.erwin.xmlmodel.AttributePropsList.Name();
propName.setValue(logicalAttrName);
attributePropsList.setName(propName);
LogicalDataType logicalDataType = new LogicalDataType();
String logicalAttrType = getLogicalAttrType(physicalTableName, dbAttr.getName());
if (StringUtil.isEmpty(logicalAttrType)) {
logicalAttrType = typeValue;
}
logicalDataType.setValue(logicalAttrType);
attributePropsList.setLogicalDataType(logicalDataType);
if (dbAttr.isNotNull()) {
NullOption nullOption = new NullOption();
nullOption.setValue("" + 1);
attributePropsList.setNullOption(nullOption);
}
Order order = new Order();
attributePropsList.setOrder(order);
order.setValue("" + attrOrder++);
if (dbAttr.getDefault() != null) {
String defaultNameStr = handleSpace(dbAttr.getDefault());
String defaultName = "default_" + defaultNameStr;
Default defaultValue = null;
attributePropsList.setDefaultValue(new AttributePropsList.DefaultValue());
if (defaultNodeMap.containsKey(defaultName)) {
defaultValue = defaultNodeMap.get(defaultName);
attributePropsList.getDefaultValue().setValue(defaultValue.getId());
continue;
}
String defaultId = getUUID();
DefaultValueGroups defaultValueGroups = model.getDefaultValueGroups();
attributePropsList.getDefaultValue().setValue(defaultId);
defaultValue = new Default();
defaultValue.setId(defaultId);
defaultValue.setName("default_" + defaultNameStr);
defaultValueGroups.getDefault().add(defaultValue);
defaultNodeMap.put(defaultValue.getName(), defaultValue);
DefaultPropsList defaultPropsList = new DefaultPropsList();
defaultValue.setDefaultProps(defaultPropsList);
defaultPropsList.setName(new DefaultPropsList.Name());
defaultPropsList.getName().setValue(defaultValue.getName());
defaultPropsList.setServerValue(new ServerValue());
defaultPropsList.getServerValue().setValue(dbAttr.getDefault());
defaultPropsList.setLogicalDefaultValue(new LogicalDefaultValue());
defaultPropsList.getLogicalDefaultValue().setValue(dbAttr.getDefault());
}
}
entityAttrMap.put(physicalTableName, schemaAttrMap);
}
List<String> relIds = new ArrayList<String>();
for (Entry<String, SchemaInfo> entry : allSchemaInfos.entrySet()) {
Entity entity = entityMap.get(entry.getKey());
if (entity == null)
continue;
SchemaInfo schemaInfo = entry.getValue();
Map<String, Attribute> schemaAttrMap = entityAttrMap.get(schemaInfo.getClassname());
KeyGroupGroups keyGroups = new KeyGroupGroups();
entity.setKeyGroupGroups(keyGroups);
// counter for index
int fk = 1;
int idx = 1;
int uidx = 1;
List<Constraint> constraints = schemaInfo.getConstraints();
for (Constraint constraint : constraints) {
if (!isValid(constraint)) {
continue;
}
KeyGroup keyGroup = new KeyGroup();
keyGroups.getKeyGroup().add(keyGroup);
keyGroup.setId(getUUID());
keyGroup.setName(constraint.getName());
KeyGroupPropsList keyGroupPropsList = new KeyGroupPropsList();
keyGroup.setKeyGroupProps(keyGroupPropsList);
KeyGroupMemberGroups keyGroupMemberGroups = new KeyGroupMemberGroups();
keyGroup.setKeyGroupMemberGroups(keyGroupMemberGroups);
KeyGroupType keyGrouptype = new KeyGroupType();
keyGroupPropsList.setKeyGroupType(keyGrouptype);
if (constraint.getType().equals(ConstraintType.PRIMARYKEY.getText())) {
keyGrouptype.setValue("PK");
List<String> keyAttr = constraint.getAttributes();
int order = 1;
for (String keyName : keyAttr) {
KeyGroupMember keyGroupMember = new KeyGroupMember();
keyGroupMemberGroups.getKeyGroupMember().add(keyGroupMember);
keyGroupMember.setId(getUUID());
keyGroupMember.setName("" + order);
KeyGroupMemberPropsList propList = new KeyGroupMemberPropsList();
keyGroupMember.setKeyGroupMemberProps(propList);
KeyGroupPosition position = new KeyGroupPosition();
propList.setKeyGroupPosition(position);
position.setValue("" + order++);
KeyGroupMemberColumn column = new KeyGroupMemberColumn();
Attribute attrTemp = schemaAttrMap.get(keyName);
column.setValue(attrTemp.getId());
propList.setKeyGroupMemberColumn(column);
}
} else if (constraint.getType().equals(ConstraintType.FOREIGNKEY.getText())) {
keyGrouptype.setValue("IF" + fk++);
String relId = getUUID();
keyGroupPropsList.setKeyGroupRelationPointer(new KeyGroupRelationPointer());
keyGroupPropsList.getKeyGroupRelationPointer().setValue(relId);
relIds.add(relId);
List<String> keyAttr = constraint.getAttributes();
String inherit = constraint.getReferencedTable();
Entity pEntity = entityMap.get(inherit);
if (pEntity == null) {
continue;
}
Map<String, Attribute> tempAttrMap = entityAttrMap.get(schemaInfo.getClassname());
Map<String, Attribute> parentAttrMap = entityAttrMap.get(inherit);
Relationship relationShip = new Relationship();
relationShip.setId(relId);
relationshipMap.put(relId, relationShip);
relationShip.setName("R/" + fk);
model.getRelationshipGroups().getRelationship().add(relationShip);
RelationshipPropsList relationShipPropsList = new RelationshipPropsList();
relationShip.setRelationshipProps(relationShipPropsList);
relationShipPropsList.setName(new RelationshipPropsList.Name());
relationShipPropsList.getName().setValue(relationShip.getName());
// type == 7 : non-identify fk
relationShipPropsList.setType(new RelationshipPropsList.Type());
relationShipPropsList.getType().setValue("2");
relationShipPropsList.setRelationshipNoNulls(new RelationshipNoNulls());
relationShipPropsList.getRelationshipNoNulls().setValue("101");
relationShipPropsList.setRelationshipSequence(new RelationshipSequence());
relationShipPropsList.getRelationshipSequence().setValue("1");
relationShipPropsList.setRelationshipParentInsertRule(new RelationshipParentInsertRule());
relationShipPropsList.getRelationshipParentInsertRule().setValue("0");
relationShipPropsList.setRelationshipParentUpdateRule(new RelationshipParentUpdateRule());
relationShipPropsList.setRelationshipParentDeleteRule(new RelationshipParentDeleteRule());
relationShipPropsList.setRelationshipChildInsertRule(new RelationshipChildInsertRule());
relationShipPropsList.getRelationshipChildInsertRule().setValue("0");
relationShipPropsList.setRelationshipChildUpdateRule(new RelationshipChildUpdateRule());
relationShipPropsList.getRelationshipChildUpdateRule().setValue("0");
relationShipPropsList.setRelationshipChildDeleteRule(new RelationshipChildDeleteRule());
relationShipPropsList.getRelationshipChildDeleteRule().setValue("0");
List<String> rules = constraint.getRules();
for (String rule : rules) {
if (rule.indexOf("ON UPDATE") != -1) {
int tmp = rule.replace("ON UPDATE ", "").hashCode();
RelationshipParentUpdateRule updateRule = relationShipPropsList.getRelationshipParentUpdateRule();
if (tmp == "CASCADE".hashCode()) {
updateRule.setValue("10001");
} else if (tmp == "NO ACTION".hashCode()) {
updateRule.setValue("9998");
} else if (tmp == "RESTRICT".hashCode()) {
updateRule.setValue("10000");
}
} else if (rule.indexOf("ON DELETE") != -1) {
int tmp = rule.replace("ON DELETE ", "").hashCode();
RelationshipParentDeleteRule deleteRule = relationShipPropsList.getRelationshipParentDeleteRule();
if (tmp == "CASCADE".hashCode()) {
deleteRule.setValue("10005");
} else if (tmp == "NO ACTION".hashCode()) {
deleteRule.setValue("9999");
} else if (tmp == "RESTRICT".hashCode()) {
deleteRule.setValue("10004");
}
}
}
relationShipPropsList.setRelationshipParentEntity(new RelationshipParentEntity());
relationShipPropsList.getRelationshipParentEntity().setValue(pEntity.getId());
relationShipPropsList.setRelationshipChildEntity(new RelationshipChildEntity());
relationShipPropsList.getRelationshipChildEntity().setValue(entityMap.get(schemaInfo.getClassname()).getId());
LinkedList<String> pkAttr = new LinkedList<String>();
SchemaInfo parentTable = allSchemaInfos.get(inherit);
for (Constraint con : parentTable.getConstraints()) {
if (con.getType().equals(ConstraintType.PRIMARYKEY.getText())) {
pkAttr.addAll(con.getAttributes());
break;
}
}
int order = 1;
for (String keyName : keyAttr) {
KeyGroupMember keyGroupMember = new KeyGroupMember();
keyGroupMemberGroups.getKeyGroupMember().add(keyGroupMember);
keyGroupMember.setId(getUUID());
keyGroupMember.setName("" + order);
KeyGroupMemberPropsList propList = new KeyGroupMemberPropsList();
keyGroupMember.setKeyGroupMemberProps(propList);
KeyGroupPosition position = new KeyGroupPosition();
propList.setKeyGroupPosition(position);
position.setValue("" + order++);
KeyGroupMemberColumn column = new KeyGroupMemberColumn();
String parentPkAttr = pkAttr.remove();
Attribute attrTemp = tempAttrMap.get(keyName);
column.setValue(attrTemp.getId());
propList.setKeyGroupMemberColumn(column);
// Attribute parent part
Attribute schemaAttr = schemaAttrMap.get(keyName);
AttributePropsList schemaPropList = schemaAttr.getAttributeProps();
Attribute parentPkAttrNode = parentAttrMap.get(parentPkAttr);
ParentAttribute pAttribute = new ParentAttribute();
pAttribute.setValue(parentPkAttrNode.getId());
schemaPropList.setParentAttribute(pAttribute);
schemaAttr.getAttributeProps().setParentRelationship(new ParentRelationship());
schemaAttr.getAttributeProps().getParentRelationship().setValue(relId);
}
} else if (constraint.getType().equals(ConstraintType.INDEX.getText()) || constraint.getType().equals(ConstraintType.REVERSEINDEX.getText())) {
keyGrouptype.setValue("IE" + idx++);
List<String> keyAttr = constraint.getAttributes();
int order = 1;
for (String keyName : keyAttr) {
KeyGroupMember keyGroupMember = new KeyGroupMember();
keyGroupMemberGroups.getKeyGroupMember().add(keyGroupMember);
keyGroupMember.setId(getUUID());
keyGroupMember.setName("" + order);
KeyGroupMemberPropsList propList = new KeyGroupMemberPropsList();
keyGroupMember.setKeyGroupMemberProps(propList);
for (String rule : constraint.getRules()) {
if (rule.toLowerCase().startsWith(keyName.toLowerCase())) {
if (rule.toLowerCase().indexOf(" desc") != -1) {
propList.setKeyGroupSortOrder(new KeyGroupSortOrder());
propList.getKeyGroupSortOrder().setValue("DESC");
}
}
}
KeyGroupPosition position = new KeyGroupPosition();
propList.setKeyGroupPosition(position);
position.setValue("" + order++);
KeyGroupMemberColumn column = new KeyGroupMemberColumn();
Attribute attrTemp = schemaAttrMap.get(keyName);
column.setValue(attrTemp.getId());
propList.setKeyGroupMemberColumn(column);
}
} else if (constraint.getType().equals(ConstraintType.UNIQUE.getText()) || constraint.getType().equals(ConstraintType.REVERSEUNIQUE.getText())) {
keyGrouptype.setValue("AK" + uidx++);
List<String> keyAttr = constraint.getAttributes();
int order = 1;
for (String keyName : keyAttr) {
KeyGroupMember keyGroupMember = new KeyGroupMember();
keyGroupMemberGroups.getKeyGroupMember().add(keyGroupMember);
keyGroupMember.setId(getUUID());
keyGroupMember.setName("" + order);
KeyGroupMemberPropsList propList = new KeyGroupMemberPropsList();
keyGroupMember.setKeyGroupMemberProps(propList);
KeyGroupPosition position = new KeyGroupPosition();
propList.setKeyGroupPosition(position);
position.setValue("" + order++);
KeyGroupMemberColumn column = new KeyGroupMemberColumn();
Attribute attrTemp = schemaAttrMap.get(keyName);
column.setValue(attrTemp.getId());
propList.setKeyGroupMemberColumn(column);
KeyGroupSortOrder sortOrder = new KeyGroupSortOrder();
propList.setKeyGroupSortOrder(sortOrder);
for (String rule : constraint.getRules()) {
if (rule.toLowerCase().startsWith(keyName.toLowerCase())) {
String orderStr = rule.replace(keyName.toLowerCase(), "").replaceAll(" ", "");
sortOrder.setValue(orderStr);
}
}
}
}
}
}
setDrawData(model);
FileOutputStream fout = null;
try {
File f = new File(filename);
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
CommonUITool.openErrorBox(Messages.bind(Messages.errCreateFile, filename));
return;
}
}
fout = new FileOutputStream(f);
marshaller.marshal(root, fout);
fout.close();
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
isSuccess = false;
} finally {
try {
if (fout != null) {
fout.flush();
fout.close();
}
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
}
isSuccess = true;
}
Aggregations