/** * 根据模型id返回所有关联关系 * * @param modelId 资源模型id * @return 所有关联关系的列表 */ @Override public List<ModelRelation> getModelRelationsByModelId(String modelId) { List<ModelRelation> list = new ArrayList<ModelRelation>(); List<ModelRelation> all = getAllModelRelations(); for (ModelRelation mr : all) { if (mr.getModelId().equals(modelId) || mr.getModelId2().equals(modelId)) { list.add(mr); } } return list; }
/** * 获取某个资源有可能会有关联关系的资源集合.(包含本身) 即取出指定资源所属模型的所有关联模型下的所有资源. * * @param resId 指定资源ID * @return */ public List<ResourceObject> getRelationsWithModelByResId(int resId, int userId) { List<ResourceObject> list = new ArrayList<ResourceObject>(); IResourceService is = ServiceManager.getResourceService(); ResourceObject r = is.getResourceById(resId); Map<String, String> m = new HashMap<String, String>(); for (ModelRelation mr : getModelRelationsByModelId(r.getModelId())) { if (!mr.getModelId().equals(r.getModelId())) m.put(mr.getModelId(), "1"); if (!mr.getModelId2().equals(r.getModelId())) m.put(mr.getModelId2(), "1"); } for (String modelId : m.keySet()) { list.addAll(is.getResourcesByModelId(modelId, userId)); } list.add(r); return list; }
/** * 生成模型关联关系拓扑图数据 * * @return 拓扑图数据 */ private String getModelRelationXml(List<ModelRelation> list) { Document doc = null; try { doc = XmlUtil.parseString("<Graph/>"); } catch (DocumentException e) { // 此处不可能发生 e.printStackTrace(); return ""; } IModelService ms = ServiceManager.getModelService(); Map<String, Model> map = new HashMap<String, Model>(); for (int i = 0; i < list.size(); i++) { ModelRelation mr = list.get(i); Model m1 = ms.getModelById(mr.getModelId()); Model m2 = ms.getModelById(mr.getModelId2()); map.put(m1.getId(), m1); map.put(m2.getId(), m2); } Model[] models = map.values().toArray(new Model[] {}); for (int i = 0; i < models.length; i++) { Model m = models[i]; Element el = doc.getRootElement().addElement("Node"); el.addAttribute("id", m.getId()); el.addAttribute("name", m.getName()); el.addAttribute("nodeIcon", "" + m.getLargeIcon()); el.addAttribute("nodeSize", "32"); } for (int i = 0; i < list.size(); i++) { ModelRelation mr = list.get(i); Element el = doc.getRootElement().addElement("Edge"); el.addAttribute("fromID", mr.getModelId()); el.addAttribute("toID", mr.getModelId2()); el.addAttribute("edgeLabel", getRelationDefineById(mr.getRelationId()).getName()); } return XmlUtil.getXmlString(doc); }