上传文件至 'TML批量维护多肽属性'

main
libowen 9 months ago
parent 6878b03b11
commit c9a4aa62bb

@ -0,0 +1,162 @@
package com.tml.materialPeptide;
import com.dassault_systemes.platform.restServices.RestService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import matrix.db.JPO;
import matrix.util.MatrixException;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.List;
import java.util.logging.Logger;
@Path("/materialPeptide")
public class DSMaintenancePeptideAttrController extends RestService {
private static final Logger LOGGER = Logger.getLogger(DSMaintenancePeptideAttrController.class.getName());
@GET
public Response getInfo(@Context HttpServletRequest request,
@QueryParam("partCode") String partCode) {
return this.getPartPeptideAttr(request, partCode);
}
@PUT
@Path("/updatePartInfo")
@Consumes(MediaType.APPLICATION_JSON)
public Response updatePartInfo(@Context HttpServletRequest request,
PartAttrDTO partAttrDTO) {
return this.updatePartAttr(request,partAttrDTO);
}
@PUT
@Path("/updateInfo")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateInfo(@Context HttpServletRequest request,
PeptideUpdateRequest updateRequest) {
return this.updatePeptideAttrs(request, updateRequest);
}
private Response updatePartAttr(HttpServletRequest request, PartAttrDTO partAttrDTO) {
if(partAttrDTO == null || partAttrDTO.getPartId() == null) {
return Response.status(Response.Status.BAD_REQUEST).entity("Missing required properties").build();
}
matrix.db.Context context = this.getAuthenticatedContext(request, false);
String partId = partAttrDTO.getPartId();
StringBuilder updateInfoBuilder = new StringBuilder();
updateInfoBuilder.append("modify bus ").append(partId);
// 如果有规格型号属性,添加到更新字符串
if (partAttrDTO.getSpecification() != null) {
updateInfoBuilder.append(" ").append("specification").append(" ").append(partAttrDTO.getSpecification());
}
// 如果有品牌属性,添加到更新字符串
if (partAttrDTO.getBrand() != null) {
updateInfoBuilder.append(" ").append("brand").append(" ").append(partAttrDTO.getBrand());
}
String updateInfo = updateInfoBuilder.toString();
String[] updateInfoArray = {updateInfo};
LOGGER.info("调用JPO更新物料属性: " + updateInfo);
try {
String resultJson = JPO.invoke(context, "TMLMaintainMaterialPeptideAttr", null, "updatePartAttr", updateInfoArray, String.class);
return Response.status(Response.Status.OK).entity(resultJson).build();
} catch (MatrixException e) {
throw new RuntimeException(e);
}
}
private Response updatePeptideAttrs(HttpServletRequest request, PeptideUpdateRequest updateRequest) {
if(updateRequest == null || updateRequest.getProperties() == null) {
return Response.status(Response.Status.BAD_REQUEST).entity("Missing required properties").build();
}
matrix.db.Context context = this.getAuthenticatedContext(request, false);
List<PropertyDTO> properties = updateRequest.getProperties();
String itemId = updateRequest.getPartId() + " ";
String itemInfo = itemId;
for(PropertyDTO property : properties) {
itemInfo += property.getName() + " ";
itemInfo += property.getValue() + " ";
}
String updateInfo = "modify bus " + itemInfo;
String[] updateInfoArray = {updateInfo};
try {
String resultJson = JPO.invoke(context, "TMLMaintainMaterialPeptideAttr", null, "updatePeptideAttr", updateInfoArray, String.class);
return Response.status(Response.Status.OK).entity(resultJson).build();
} catch (MatrixException e) {
throw new RuntimeException(e);
}
}
private Response getPartPeptideAttr(HttpServletRequest request, String partCode) {
if (partCode == null || partCode.trim().isEmpty()) {
return createErrorResponse(400, "缺少 partCode 参数");
}
try {
matrix.db.Context context = this.getAuthenticatedContext(request, false);
String[] partInfo;
if (partCode.contains("|")) {
partInfo = partCode.split("\\|");
} else {
partInfo = new String[]{partCode};
}
return executeQuery(context, partInfo);
} catch (Exception e) {
return createErrorResponse(500, "认证失败: " + e.getMessage());
}
}
private Response executeQuery(matrix.db.Context context, String[] partInfo) {
try {
// 调用 JPO 方法,返回包装的JSON
String resultJson = JPO.invoke(context, "TMLMaintainMaterialPeptideAttr", null, "getPeptideAttrMap", partInfo, String.class);
// 直接返回JSON字符串
return Response.ok()
.entity(resultJson)
.type(MediaType.APPLICATION_JSON)
.build();
} catch (MatrixException e) {
return createErrorResponse(500, "JPO调用失败: " + e.getMessage());
} catch (Exception e) {
return createErrorResponse(500, "服务器错误: " + e.getMessage());
}
}
private Response createErrorResponse(int statusCode, String message) {
try {
JSONObject errorResponse = new JSONObject();
errorResponse.put("success", false);
errorResponse.put("code", statusCode);
errorResponse.put("message", message);
errorResponse.put("data", new JSONArray());
errorResponse.put("timestamp", System.currentTimeMillis());
return Response.status(statusCode)
.entity(errorResponse.toString())
.type(MediaType.APPLICATION_JSON)
.build();
} catch (Exception e) {
// 如果JSON创建失败,返回简单文本
return Response.status(500)
.entity("{\"error\":\"Internal server error\"}")
.type(MediaType.APPLICATION_JSON)
.build();
}
}
}

@ -0,0 +1,17 @@
package com.tml.materialPeptide;
public class PartAttrDTO {
private String partId;
private String specification; // 规格型号
private String brand; // 品牌
// getters and setters
public String getPartId() { return partId; }
public void setPartId(String partId) { this.partId = partId; }
public String getSpecification() { return specification; }
public void setSpecification(String specification) { this.specification = specification; }
public String getBrand() { return brand; }
public void setBrand(String brand) { this.brand = brand; }
}

@ -0,0 +1,32 @@
package com.tml.materialPeptide;
import java.util.List;
public class PeptideUpdateRequest {
private String partId;
private List<PropertyDTO> properties;
public PeptideUpdateRequest(String partId, List<PropertyDTO> properties) {
this.partId = partId;
this.properties = properties;
}
public PeptideUpdateRequest() {
}
public String getPartId() {
return partId;
}
public List<PropertyDTO> getProperties() {
return properties;
}
public void setPartId(String partId) {
this.partId = partId;
}
public void setProperties(List<PropertyDTO> properties) {
this.properties = properties;
}
}

@ -0,0 +1,299 @@
import com.matrixone.apps.domain.util.FrameworkException;
import com.matrixone.apps.domain.util.MqlUtil;
import matrix.db.Context;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.*;
import java.util.logging.Logger;
public class TMLMaintainMaterialPeptideAttr_mxJPO {
private static final Logger LOGGER = Logger.getLogger(TMLMaintainMaterialPeptideAttr_mxJPO.class.getName());
public String updatePartAttr(Context context, String[] args) throws FrameworkException {
String updateInfo = args[0];
LOGGER.info("updatePeptideAttr: " + updateInfo);
MqlUtil.mqlCommand(context, updateInfo);
return "success";
}
public String updatePeptideAttr(Context context, String[] args) throws FrameworkException {
String updateInfo = args[0];
LOGGER.info("updatePeptideAttr: " + updateInfo);
MqlUtil.mqlCommand(context, updateInfo);
return "success";
}
public String getPeptideAttrMap(Context context, String[] args) throws FrameworkException {
JSONArray peptideAttrArray = new JSONArray();
if(args != null && args.length > 0) {
for (String itemValue : args) {
if (itemValue == null || itemValue.trim().isEmpty()) {
continue;
}
JSONObject peptideAttrObj = new JSONObject();
try {
// 1. 获取item ID
String getItemInfo = "temp query bus VPMReference * * where \"attribute[PLMEntity.V_description].value == '" +
escapeMqlString(itemValue.trim()) + "'\" select id dump |";
String itemInfo = MqlUtil.mqlCommand(context, getItemInfo);
if (itemInfo == null || itemInfo.trim().isEmpty()) {
LOGGER.warning("未找到项目: " + itemValue);
peptideAttrObj.put("itemValue", itemValue);
peptideAttrObj.put("error", "未找到项目");
peptideAttrArray.put(peptideAttrObj);
continue;
}
String[] itemInfoParts = itemInfo.split("\\|");
if (itemInfoParts.length <= 3) {
LOGGER.warning("项目信息格式不正确: " + itemInfo);
peptideAttrObj.put("itemValue", itemValue);
peptideAttrObj.put("error", "项目信息格式不正确");
peptideAttrArray.put(peptideAttrObj);
continue;
}
String itemId = itemInfoParts[3].trim();
// 2. 获取肽属性信息
String getMaterialPeptideAttrInfo = "print bus " + itemId +
" select attribute[PLMEntity.V_Name].value " +
"attribute[TMLVPMReference.TMLSpecification].value attribute[TMLVPMReference.TMLBrand].value " +
"to[Classified Item].attribute to[Classified Item].attribute.value dump |";
String materialPeptideAttrInfo = MqlUtil.mqlCommand(context, getMaterialPeptideAttrInfo);
// 添加基本信息
peptideAttrObj.put("itemValue", itemValue);
peptideAttrObj.put("itemId", itemId);
// 安全检查:确保返回结果不为空
if (materialPeptideAttrInfo != null && !materialPeptideAttrInfo.trim().isEmpty()) {
try {
// 使用 -1 保留所有空字符串元素
String[] materialPeptideAttrSplit = materialPeptideAttrInfo.split("\\|", -1);
// 1. 先提取基本信息(前3个元素)
if (materialPeptideAttrSplit.length >= 1) {
peptideAttrObj.put("materialName", materialPeptideAttrSplit[0].trim());
}
if (materialPeptideAttrSplit.length >= 2) {
peptideAttrObj.put("brand", materialPeptideAttrSplit[1].trim());
}
if (materialPeptideAttrSplit.length >= 3) {
peptideAttrObj.put("specification", materialPeptideAttrSplit[2].trim());
}
List<Map<String, String>> peptideProperties = new ArrayList<>();
// 2. 处理剩余的属性键值对(从第4个元素开始)
// 确保有足够的元素形成键值对
int startIndex = 3; // 从第4个元素开始(索引3)
if (materialPeptideAttrSplit.length > startIndex) {
// 计算可用于形成键值对的元素数量
int availableElements = materialPeptideAttrSplit.length - startIndex;
// 如果剩余元素是奇数,添加一个空字符串使其成为偶数
if (availableElements % 2 != 0) {
materialPeptideAttrSplit = Arrays.copyOf(
materialPeptideAttrSplit,
materialPeptideAttrSplit.length + 1
);
materialPeptideAttrSplit[materialPeptideAttrSplit.length - 1] = "";
availableElements += 1;
}
// 处理键值对
int totalPairs = availableElements / 2;
for (int j = 0; j < totalPairs; j++) {
int keyIndex = startIndex + j;
int valueIndex = startIndex + totalPairs + j;
String key = materialPeptideAttrSplit[keyIndex].trim();
String value = materialPeptideAttrSplit[valueIndex].trim();
// 只有当键不为空时才添加到对象
if (!key.isEmpty()) {
Map<String, String> property = new HashMap<>();
property.put("name", key);
property.put("value", value);
peptideProperties.add(property);
}
}
}
// 3. 将多肽属性列表添加到结果对象
peptideAttrObj.put("peptideProperties", peptideProperties);
} catch (Exception e) {
// 异常处理
System.err.println("Error processing material peptide attributes for itemId: " + itemId);
e.printStackTrace();
// 返回默认结构
peptideAttrObj.put("PLMEntity.V_Name", "");
peptideAttrObj.put("PLMEntity.V_Description", "");
peptideAttrObj.put("PLMEntity.V_Classification", "");
peptideAttrObj.put("TMLVPMReference.TMLBrand", "");
peptideAttrObj.put("peptideProperties", new ArrayList<>());
}
} else {
// MQL命令返回为空的情况
peptideAttrObj.put("PLMEntity.V_Name", "");
peptideAttrObj.put("PLMEntity.V_Description", "");
peptideAttrObj.put("PLMEntity.V_Classification", "");
peptideAttrObj.put("TMLVPMReference.TMLBrand", "");
peptideAttrObj.put("peptideProperties", new ArrayList<>());
}
} catch (Exception e) {
LOGGER.warning("处理项目 " + itemValue + " 时出错: " + e.getMessage());
try {
peptideAttrObj.put("itemValue", itemValue);
peptideAttrObj.put("error", e.getMessage());
} catch (Exception ex) {
// 忽略 JSON 异常
}
}
peptideAttrArray.put(peptideAttrObj);
}
}
// 直接返回 JSONArray 的字符串表示
System.out.println(peptideAttrArray);
return peptideAttrArray.toString();
}
public String getPeptideAttrMapWithWrapper(Context context, String[] args) throws FrameworkException {
JSONObject result = new JSONObject();
try {
JSONArray data = new JSONArray(getPeptideAttrMap(context, args));
result.put("success", true);
result.put("code", 200);
result.put("message", "查询成功");
result.put("data", data);
result.put("total", data.length());
result.put("timestamp", System.currentTimeMillis());
} catch (Exception e) {
result.put("success", false);
result.put("code", 500);
result.put("message", "查询失败: " + e.getMessage());
result.put("data", new JSONArray());
result.put("timestamp", System.currentTimeMillis());
}
return result.toString();
}
/**
* 另一种实现:直接返回标准JSON格式
*/
public String getPeptideAttrMapStandard(Context context, String[] args) throws FrameworkException {
JSONObject response = new JSONObject();
JSONArray dataArray = new JSONArray();
try {
if(args != null && args.length > 0) {
for (String itemValue : args) {
JSONObject itemData = processSingleItem(context, itemValue);
dataArray.put(itemData);
}
}
response.put("success", true);
response.put("code", 200);
response.put("message", "success");
response.put("data", dataArray);
response.put("total", dataArray.length());
response.put("timestamp", System.currentTimeMillis());
} catch (Exception e) {
response.put("success", false);
response.put("code", 500);
response.put("message", "Error: " + e.getMessage());
response.put("data", new JSONArray());
response.put("timestamp", System.currentTimeMillis());
}
return response.toString();
}
/**
* 处理单个项目
*/
private JSONObject processSingleItem(Context context, String itemValue) throws Exception {
JSONObject itemObj = new JSONObject();
// 获取item ID
String getItemInfo = "temp query bus VPMReference * * where \"attribute[PLMEntity.V_description].value == '" +
escapeMqlString(itemValue.trim()) + "'\" select id dump |";
String itemInfo = MqlUtil.mqlCommand(context, getItemInfo);
if (itemInfo == null || itemInfo.trim().isEmpty()) {
itemObj.put("error", "Item not found");
return itemObj;
}
String[] itemInfoParts = itemInfo.split("\\|");
if (itemInfoParts.length <= 3) {
itemObj.put("error", "Invalid item info format");
return itemObj;
}
String itemId = itemInfoParts[3].trim();
itemObj.put("itemId", itemId);
itemObj.put("itemValue", itemValue);
// 获取肽属性信息
String getMaterialPeptideAttrInfo = "print bus " + itemId +
" select to[Classified Item].attribute to[Classified Item].attribute.value dump |";
String materialPeptideAttrInfo = MqlUtil.mqlCommand(context, getMaterialPeptideAttrInfo);
if (materialPeptideAttrInfo != null && !materialPeptideAttrInfo.trim().isEmpty()) {
JSONObject attributes = parseAttributes(materialPeptideAttrInfo);
itemObj.put("attributes", attributes);
} else {
itemObj.put("attributes", new JSONObject());
}
return itemObj;
}
/**
* 解析属性字符串为 JSONObject
*/
private JSONObject parseAttributes(String attrInfo) throws Exception {
JSONObject attributes = new JSONObject();
String[] parts = attrInfo.split("\\|", -1);
if (parts.length % 2 != 0) {
parts = Arrays.copyOf(parts, parts.length + 1);
parts[parts.length - 1] = "";
}
int totalPairs = parts.length / 2;
for (int i = 0; i < totalPairs; i++) {
String key = parts[i].trim();
String value = parts[totalPairs + i].trim();
if (!key.isEmpty()) {
attributes.put(key, value);
}
}
return attributes;
}
private String escapeMqlString(String input) {
if (input == null) return "";
return input.replace("'", "''");
}
}

@ -0,0 +1,6 @@
需求说明
给库的管理多肽属性进行管理,可以进行增删改查
部署说明
为JPO:TMLMaintainMaterialPeptideAttr_mxJPO
Vue:前端显示
materialPeptide : 接口
Loading…
Cancel
Save