一.使用SearchCondition
查询语句中用容器中的containerReference.key.id名称来代替数据库中的字段idA3containerReference
/**
* 获取项目下所有的活动 StandardProjectReportService.java
* @param project
* @return
*/
public static List<PlanActivity> getActivityByProject(Project2 project) throws Exception{
List<PlanActivity> activityList = new ArrayList<PlanActivity>();
QueryResult queryResult = null;
if (project == null)
return null;
int[] index = { 0 };
QuerySpec querySpec = new QuerySpec(PlanActivity.class);//SELECT A0.* FROM com.ptc.projectmanagement.plan.PlanActivity A0
long projectId = PersistenceHelper.getObjectIdentifier(project).getId();
WhereExpression whereExpression = new SearchCondition(
PlanActivity.class, "containerReference.key.id",/* 数据库字段是 idA3containerReference */
SearchCondition.EQUAL, projectId);//WHERE (A0.idA3containerReference = projectId
querySpec.appendWhere(whereExpression, index);
queryResult = PersistenceHelper.manager.find((StatementSpec) querySpec);
PlanActivity projectActivity = null;
while(queryResult.hasMoreElements()){
projectActivity = (PlanActivity) queryResult.nextElement();
activityList.add(projectActivity);
}
return activityList;
}
表名PlanActivity的类名是 com.ptc.projectmanagement.plan.PlanActivity
在服务器中的Wndchill shell中输入如下命令:inforeport com.ptc.projectmanagement.plan.PlanActivity
inforeport wt.projmgmt.admin.Project2
会在D:\ptc\Windchill_10.1\Windchill\temp 目录中自动生成文件admin.Project2.ou,打开即可
二.使用TableColumn
直接使用数据库中 字段
public static List<Project2> queryProjects(HashMap<String, String> queryParams)
throws Exception {
String department = queryParams.get("department"); //0
String user = queryParams.get("user"); //1
System.out.println("user=====ddd==========>" + user);
String pjName = queryParams.get("pjName"); //2项目名称
String projComp = queryParams.get("projComp"); //3 项目进度查询
String create_date = queryParams.get("create_date");//4
String end_date = queryParams.get("end_date"); //5
List<Project2> list = new ArrayList<Project2>();
QueryResult queryResult = null;
QuerySpec querySpec = null;
try {
querySpec = new QuerySpec();
int projectClassIndex = querySpec.appendClassList(Project2.class,true);//wt.projmgmt.admin.Project2 A0
int userClassIndex = querySpec.appendClassList(WTUser.class, true);//wt.org.WTUser A1
int epplanClassIndex = querySpec.appendClassList(Plan.class, true);//com.ptc.projectmanagement.plan.Plan A2
//System.out.println(">>>>querySpec22222----------"+querySpec);
//SELECT A0.*,A1.*,A2.* FROM wt.projmgmt.admin.Project2 A0 ,wt.org.WTUser A1 ,com.ptc.projectmanagement.plan.Plan A2
querySpec.setAdvancedQueryEnabled(true);
String[] aliases = new String[3];
aliases[0] = querySpec.getFromClause().getAliasAt(projectClassIndex);//A0
aliases[1] = querySpec.getFromClause().getAliasAt(epplanClassIndex);//A2
aliases[2] = querySpec.getFromClause().getAliasAt(userClassIndex);//A1
TableColumn projectIdColumn = new TableColumn(aliases[0], "idA2A2");//A0.idA2A2
TableColumn projectNameColumn = new TableColumn(aliases[0], "namecontainerInfo");//A0.namecontainerInfo
TableColumn projectUserIDColumn = new TableColumn(aliases[0],"idA3B2containerInfo"); //A0.idA3B2containerInfo
TableColumn projectStateColumn = new TableColumn(aliases[0],"statecontainerTeamManagedInf");//A0.statecontainerTeamManagedInf
TableColumn foreignKeyColumn = new TableColumn(aliases[1], "idA3containerReference");//A2.idA3containerReference
TableColumn userIDColumn = new TableColumn(aliases[2], "idA2A2");//A1.idA2A2
TableColumn percentCompleteColumn = new TableColumn(aliases[1],"percentWorkComplete"); // 项目完成进度
TableColumn actualStartTimeColumn = new TableColumn(aliases[1],"startDate"); // 实际开始时间 //epplan表
TableColumn finishDateColumn = new TableColumn(aliases[1],"finishDate"); // 实际完成时间
CompositeWhereExpression andExpression = new CompositeWhereExpression(LogicalOperator.AND);//where
andExpression.append(new SearchCondition(projectIdColumn, "=",foreignKeyColumn));//A0.idA2A2 = A2.idA3containerReference
andExpression.append(new SearchCondition(projectUserIDColumn, "=",userIDColumn));//A0.idA3B2containerInfo = A1.idA2A2
andExpression.append(new SearchCondition(projectStateColumn, SearchCondition.NOT_EQUAL,ConstantExpression.newExpression("SUSPENDED")));//A0.statecontainerTeamManagedInf <> N‘SUSPENDED‘
// WHERE ((A0.idA2A2 = A2.idA3containerReference) AND (A0.idA3B2containerInfo = A1.idA2A2) AND (A0.statecontainerTeamManagedInf <> N‘SUSPENDED‘)
//AND (A0.namecontainerInfo LIKE N‘D1409 %‘))
//项目名称模糊查询
if(pjName!= null && !pjName.equals("")){
andExpression.append(new SearchCondition(projectNameColumn,
SearchCondition.LIKE, ConstantExpression.newExpression(pjName+"%")));//A0.namecontainerInfo LIKE N‘D1409 %‘
}
// 项目进度查询
if (projComp != null && !projComp.equals("")) {
andExpression.append(new SearchCondition(percentCompleteColumn,
">=", ConstantExpression.newExpression(Integer.parseInt(projComp))));
}
DateFormat dateFormat = new SimpleDateFormat(DATE_STYLE,SessionHelper.getLocale());
// String formatText = WTMessage.getLocalizedMessage();
// SimpleDateFormat exportTableTimeFormat = new SimpleDateFormat(formatText, SessionHelper.getLocale());
dateFormat.setTimeZone(TimeZoneHelper.getLocalTimeZone());
Date date = null;
Date date2 = null;
try {
if(create_date!=null && !create_date.equalsIgnoreCase("")){
date = dateFormat.parse(create_date); //date为项目开始时间转化成了Date格式
}
if(end_date!=null && !end_date.equalsIgnoreCase("")){
date2 = dateFormat.parse(end_date); //date2为项目结束时间
}
} catch (ParseException e) {
e.printStackTrace();
}
if (date != null) {
andExpression.append(new SearchCondition(actualStartTimeColumn,
SearchCondition.GREATER_THAN_OR_EQUAL,ConstantExpression.newExpression(date)));//>=
}
if (date2 != null) {
andExpression.append(new SearchCondition(finishDateColumn,
SearchCondition.LESS_THAN_OR_EQUAL, ConstantExpression.newExpression(date2)));//<=
}
querySpec.appendWhere(andExpression, null);
queryResult = PersistenceHelper.manager.find((StatementSpec) querySpec);
System.out.println(">>>>querySpec5555----------"+querySpec);
while (queryResult.hasMoreElements()) {
Object obj= queryResult.nextElement();
Project2 project2 = (Project2) ((Object[])obj)[0];
WTPrincipal principal=project2.getOwner();
WTPrincipalReference wf = WTPrincipalReference.newWTPrincipalReference(principal);
String ownerName = wf.getFullName();
//按人员过滤,通过条件:1.人员为project owner,
////HM=============================================HM==============
//System.out.println("Pro=============>" + ProjectUtil.findJoinUserByProject2(project2, user.trim()));
if(ProjectUtil.findJoinUserByProject2(project2, user.trim())== true|| ownerName.equals(user.trim())){
list.add(project2);
}
}
} catch (QueryException e) {
e.printStackTrace();
}
return list;
}