修改Project中的表名和字段名
开发了大半,遇到这样一个需求:
1.自己创建的表,要加上公司的简称做为前缀;
2.自己创建的表的字段不需要添加公司的简称做为前缀;
3.在系统原有的表上添加的字段要添加公司的简称做为前缀。
创建的表太多了,一个个修改是重体力活,于是写了个Job来完成,在这里记录一下:
static void ModifyTableAndFieldNameOfSomeProject(Args _args)
{
#TreeNodeSysNodeType
#define.prefix('PF')
#define.ProjectName('ProjectName')
TreeNode projectListNode = SysTreeNode::getPrivateProject();
ProjectGroupNode pNode;
//TreeNode projectSuperNode,projectGroupNode;
TreeNodeIterator projectIt;//,projectGroupIt;
ProjectNode projectNode = projectListNode.AOTfindChild(#ProjectName);
//Only has current Layers?
Boolean onlyCurrentLayer(TreeNode _treeNode)
{
int layers = _treeNode.applObjectLayerMask();
UtilEntryLevel level;
UtilEntryLevel currentLevel;
int i,j=0;
;
for(i=0;i<enumcnt(UtilEntryLevel);i++)
{
if (layers & (1 << i))
{
currentLevel = i;
j++;
}
}
if(j == 1 && currentLevel == infolog.currentAOLayer())
return true;
return false;
}
//Modify Classes and Tables
void modify(TreeNode _treeNode)
{
str property;
TreeNode childTreeNode;
;
switch(_treeNode.sysNodeType())
{
case #NT_DBTABLE:
{
//Modify TableName
if(onlyCurrentLayer(_treeNode))
{
if(substr(tableId2Name(_treeNode.applObjectId()),1,2)!=#prefix)
{
_treeNode.AOTsetProperty("Name",#Prefix+tableId2Name(_treeNode.applObjectId()));
}
}
//Modify field Name
childTreeNode = _treeNode.AOTfirstChild().AOTfirstChild();
while(childTreeNode)
{
if(!onlyCurrentLayer(_treeNode) && onlyCurrentLayer(childTreeNode))
{
if(substr(fieldId2Name(_treeNode.applObjectId(),childTreeNode.applObjectId()),1,2)!=#prefix)
{
childTreeNode.AOTsetProperty("Name",#Prefix+fieldId2Name(_treeNode.applObjectId(),childTreeNode.applObjectId()));
childTreeNode.AOTcompile(1);
childTreeNode.AOTsave();
}
}
childTreeNode = childTreeNode.AOTnextSibling();
}
//Save
_treeNode.AOTcompile(1);
_treeNode.AOTsave();
//Synchronize Table
appl.dbSynchronize(SysDictTable::newTreeNode(_treeNode).id());
break;
}
default:
break;
}
}
//Get the classes and tables to be modify
void getElementsToBeModified(TreeNode _treeNode)
{
TreeNodeIterator projectGroupIt;
TreeNode projectSuperNode,projectGroupNode;
if(!_treeNode)
return;
//Project Group
if(_treeNode.sysNodeType() == #NT_PROJECT_GROUP)
{
projectGroupIt = _treeNode.AOTiterator();
projectGroupNode = projectGroupIt.next();
while(projectGroupNode != null)
{
getElementsToBeModified(projectGroupNode);
projectGroupNode = projectGroupIt.next();
}
}
info(_treeNode.AOTname());
modify(_treeNode);
}
;
projectNode = projectNode.getRunNode();
projectIt = projectNode.AOTiterator();
pNode = projectIt.next();
while(pNode)
{
//info(pNode.AOTname());
getElementsToBeModified(pNode);
pNode = projectIt.next();
}
}