public int removeItemFromCluster(Integer tenantId, String clusterName, Integer itemId, Integer itemTypeId) throws ClusterException { Preconditions.checkNotNull(tenantId); Preconditions.checkNotNull(clusterName); Preconditions.checkNotNull(itemId); Preconditions.checkNotNull(itemTypeId); Integer itemTypeCluster = itemTypeDAO.getIdOfType(tenantId, ClusterService.ITEMTYPE_CLUSTER); Integer clusterId = idMappingDAO.lookup(clusterName); ClusterVO cluster = new ClusterVO(tenantId, clusterId, itemTypeCluster, clusterName, null); ItemVO<Integer, Integer> item = new ItemVO<Integer, Integer>(tenantId, itemId, itemTypeId); return removeItemFromCluster(cluster, item); }
public List<ClusterVO> getSiblings(Integer tenantId, ClusterVO cluster) { DelegateTree<ClusterVO, ItemAssocVO<Integer,Integer>> tree = clusters.get(tenantId); if (tree.isRoot(cluster)) return null; //root has no siblings ClusterVO parent = tree.getParent(cluster); Collection<ClusterVO> sibl = tree.getChildren(parent); List<ClusterVO> siblings = new ArrayList<ClusterVO>(); for (ClusterVO clusterVO : sibl) { if (!clusterVO.equals(cluster)) siblings.add(clusterVO); } return siblings; }
public List<ItemVO<Integer, Integer>> getItemsOfCluster(ClusterVO cluster, Integer itemType) { Preconditions.checkNotNull(cluster); Preconditions.checkNotNull(cluster.getItem()); Preconditions.checkNotNull(cluster.getItem().getTenant()); Preconditions.checkNotNull(cluster.getName()); Integer tenantId = cluster.getItem().getTenant(); Integer assocTypeBelongsTo = assocTypeDAO.getIdOfType(tenantId, ClusterService.ASSOCTYPE_BELONGSTO); IAConstraintVO<Integer, Integer> constraint = new IAConstraintVO<Integer, Integer>( null, null, null, null, tenantId, true, null); List<AssociatedItemVO<Integer, Integer>> itemAssocs = itemAssocService.getItemsFrom(itemType, assocTypeBelongsTo, cluster.getItem(), constraint); return Lists.transform(itemAssocs, new Function<AssociatedItemVO<Integer, Integer>, ItemVO<Integer, Integer>>() { public ItemVO<Integer, Integer> apply( AssociatedItemVO<Integer, Integer> input) { return input.getItem(); } }); }
public void renameCluster(Integer tenantId, String clusterName, String newName) throws ClusterException { ClusterVO cluster = loadCluster(tenantId, idMappingDAO.lookup(clusterName), itemTypeDAO.getIdOfType(tenantId, ClusterService.ITEMTYPE_CLUSTER)); DelegateTree<ClusterVO, ItemAssocVO<Integer,Integer>> tree = clusters.get(tenantId); if (tree.isRoot(cluster)) throw new ClusterException("ROOT cluster cannot be renamed!"); ClusterVO parent = tree.getParent(cluster); ClusterVO newCluster = new ClusterVO(tenantId, idMappingDAO.lookup(newName), itemTypeDAO.getIdOfType(tenantId, ClusterService.ITEMTYPE_CLUSTER), newName, cluster.getDescription()); // add the new cluster addCluster(newCluster, parent); // move all children to the new cluster for (ClusterVO child : tree.getChildren(cluster)) { moveCluster(tenantId, child.getName(), newName); } if (logger.isDebugEnabled()) { logger.debug("removed all children: " + tree.isLeaf(cluster)); } // move all items to the new cluster for (ItemVO<Integer, Integer> item : getItemsOfCluster(cluster)) { addItemToCluster(newCluster, item); } // delete the old cluster removeCluster(tenantId, clusterName); }
@SuppressWarnings("unchecked") @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final ClusterVO other = (ClusterVO) obj; // two clusters are equal if their items are equal! return this.getItem().equals(other.getItem()); }
public void addCluster(Integer tenantId, String clusterName, String clusterDescription, String parent) throws ClusterException { ClusterVO parentCluster = loadCluster(tenantId, idMappingDAO.lookup(parent), itemTypeDAO.getIdOfType(tenantId, ClusterService.ITEMTYPE_CLUSTER)); if (parentCluster == null) throw new ClusterException("Cannot add cluster at this position! No parent cluster selected!"); logger.info(parentCluster.getName()); ClusterVO childCluster = new ClusterVO(tenantId, idMappingDAO.lookup(clusterName), itemTypeDAO.getIdOfType(tenantId, ClusterService.ITEMTYPE_CLUSTER), clusterName, clusterDescription); addCluster(childCluster, parentCluster); }
private ClusterVO loadCluster(Integer tenantId, Integer itemId, Integer itemTypeId) { try { Unmarshaller um = context.createUnmarshaller(); // logger.info("Trying to load Profile: tenant-" + tenantId + " item-" + itemId + " type-" + itemTypeId); // only return active profiles! String xml = profileDAO.getProfile(tenantId, itemId, itemTypeId, true); if (xml == null) { // logger.info("No profile String found!"); return null; } ClusterVO ret = (ClusterVO) um.unmarshal(new StringReader(xml)); ret.setItem(new ItemVO<Integer, Integer>(tenantId, itemId, itemTypeId)); return ret; } catch (Exception e) { logger.error("An Error occurred marshaling the document" + e); return null; } }
public void updateClusterDescription(Integer tenantId, String clusterName, String newDescription) throws ClusterException { ClusterVO cluster = loadCluster(tenantId, idMappingDAO.lookup(clusterName), itemTypeDAO.getIdOfType(tenantId, ClusterService.ITEMTYPE_CLUSTER)); DelegateTree<ClusterVO, ItemAssocVO<Integer,Integer>> tree = clusters.get(tenantId); if (tree.isRoot(cluster)) throw new ClusterException("ROOT cluster description cannot be changed!"); cluster.setDescription(newDescription); storeCluster(cluster); }
logger.info("Found " + curNumberOfResults + " items for cluster " + sibling.getName());
private int storeCluster(ClusterVO cluster) { String xml; try { Marshaller m = context.createMarshaller(); StringWriter sw = new StringWriter(); m.marshal(cluster, sw); xml = sw.toString(); } catch (Exception e) { System.out.println("An Error occurred marshaling the document" + e); return -1; } int i = profileDAO.storeProfile(cluster.getItem().getTenant(), cluster.getItem().getItem(), cluster.getItem().getType(), xml); profileDAO.activateProfile(cluster.getItem().getTenant(), cluster.getItem().getItem(), cluster.getItem().getType()); return i; }
public List<ItemVO<Integer, Integer>> getItemsOfCluster(ClusterVO cluster, String strategy, Boolean useFallback, int numberOfResults, Integer itemType) { Preconditions.checkNotNull(cluster); Preconditions.checkNotNull(cluster.getItem()); Preconditions.checkNotNull(cluster.getItem().getTenant()); Preconditions.checkNotNull(cluster.getName()); Integer tenantId = cluster.getItem().getTenant(); ClusterStrategy clusterStrategy = strategy == null ? null : strategies.get(strategy); if (clusterStrategy == null) clusterStrategy = strategies.get(ClusterService.DEFAULT_STRATEGY); List<AssociatedItemVO<Integer, Integer>> itemAssocs; if (!useFallback) { itemAssocs = clusterStrategy.getItemsFromCluster(tenantId, cluster, numberOfResults, itemType); } else { itemAssocs = fallBack(tenantId, cluster, clusterStrategy, numberOfResults, itemType); } return Lists.transform(itemAssocs, new Function<AssociatedItemVO<Integer, Integer>, ItemVO<Integer, Integer>>() { public ItemVO<Integer, Integer> apply( AssociatedItemVO<Integer, Integer> input) { return input.getItem(); } }); }
public void addCluster(ClusterVO cluster, ClusterVO parent) throws ClusterException { clusters.get(parent.getItem().getTenant()); if (tree.containsVertex(cluster)) throw new ClusterException("Cannot add cluster! Cluster already exists!"); if (tree.containsVertex(parent)) { ItemAssocVO<Integer,Integer> edge = new ItemAssocVO<Integer,Integer>( parent.getItem().getTenant(), // tenantId parent.getItem(), //itemFrom assocTypeDAO.getIdOfType(parent.getItem().getTenant(), ClusterService.ASSOCTYPE_CLUSTER), cluster.getItem(), //itemTo sourceTypeDAO.getIdOfType(parent.getItem().getTenant(), viewTypeDAO.getIdOfType(parent.getItem().getTenant(), ViewTypeDAO.VIEWTYPE_ADMIN), true); //timeStamp itemAssocService.insertOrUpdateItemAssoc(edge);
public List<ItemVO<Integer, Integer>> getItemsOfCluster(Integer tenantId, String clusterName, Integer itemType) { Preconditions.checkNotNull(tenantId); Preconditions.checkNotNull(clusterName); Integer itemTypeCluster = itemTypeDAO.getIdOfType(tenantId, ClusterService.ITEMTYPE_CLUSTER); Integer clusterId = idMappingDAO.lookup(clusterName); ClusterVO cluster = new ClusterVO(tenantId, clusterId, itemTypeCluster, clusterName, null); return getItemsOfCluster(cluster, itemType); }
public int removeItemFromCluster(ClusterVO cluster, ItemVO<Integer, Integer> item) throws ClusterException { Preconditions.checkNotNull(cluster); Preconditions.checkNotNull(cluster.getItem()); Preconditions.checkNotNull(cluster.getItem().getTenant()); Preconditions.checkNotNull(cluster.getName()); Preconditions.checkNotNull(item); Preconditions.checkNotNull(item.getItem()); Preconditions.checkNotNull(item.getType()); Preconditions.checkArgument(cluster.getItem().getTenant().equals(item.getTenant()), "cluster's tenantId (%s) must be the same as item's tenant id (%s)", cluster.getItem().getTenant(), item.getTenant()); Integer tenantId = cluster.getItem().getTenant(); Integer assocTypeBelongsTo = assocTypeDAO.getIdOfType(tenantId, ClusterService.ASSOCTYPE_BELONGSTO); ItemAssocVO<Integer,Integer> itemAssoc = new ItemAssocVO<Integer,Integer>(tenantId, item, assocTypeBelongsTo, null, cluster.getItem(), null, null, null, null, null); int rowsModified = itemAssocService.removeItemAssocQBE(itemAssoc); if (rowsModified == 0) throw new ClusterException( "The item was not removed from the cluster, maybe the item was not in the cluster."); return rowsModified; }
private void removeClustersFromDB( DelegateTree<ClusterVO, ItemAssocVO<Integer,Integer>> tree, ClusterVO vertex) { List<ItemAssocVO<Integer,Integer>> edges = itemAssocService.getItemAssocsForItem(vertex.getItem().getTenant(), vertex.getItem(), null); //remove all edges in db for (ItemAssocVO<Integer,Integer> edge : edges) { itemAssocService.removeItemAssoc(edge.getId()); } profileDAO.deactivateProfile(vertex.getItem().getTenant(), vertex.getItem().getItem(), vertex.getItem().getType()); // remove all items from cluster for (ItemVO<Integer, Integer> item : getItemsOfCluster(vertex)) { try { removeItemFromCluster(vertex, item); } catch (ClusterException ce) { logger.error(ce.getMessage()); } } for (ClusterVO child : tree.getChildren(vertex)) { removeClustersFromDB(tree, child); } }
public int addItemToCluster(Integer tenantId, String clusterName, Integer itemId, Integer itemTypeId) throws ClusterException { Preconditions.checkNotNull(tenantId); Preconditions.checkNotNull(clusterName); Preconditions.checkNotNull(itemId); Preconditions.checkNotNull(itemTypeId); Integer itemTypeCluster = itemTypeDAO.getIdOfType(tenantId, ClusterService.ITEMTYPE_CLUSTER); Integer clusterId = idMappingDAO.lookup(clusterName); ClusterVO cluster = new ClusterVO(tenantId, clusterId, itemTypeCluster, clusterName, null); ItemVO<Integer, Integer> item = new ItemVO<Integer, Integer>(tenantId, itemId, itemTypeId); return addItemToCluster(cluster, item); }
public int addItemToCluster(ClusterVO cluster, ItemVO<Integer, Integer> item) throws ClusterException { Preconditions.checkNotNull(cluster); Preconditions.checkNotNull(cluster.getItem()); Preconditions.checkNotNull(cluster.getItem().getTenant()); Preconditions.checkNotNull(cluster.getName()); Preconditions.checkNotNull(item); Preconditions.checkNotNull(item.getItem()); Preconditions.checkNotNull(item.getType()); Preconditions.checkArgument(cluster.getItem().getTenant().equals(item.getTenant()), "cluster's tenantId (%s) must be the same as item's tenant id (%s)", cluster.getItem().getTenant(), item.getTenant()); Integer tenantId = cluster.getItem().getTenant(); Integer assocTypeBelongsTo = assocTypeDAO.getIdOfType(tenantId, ClusterService.ASSOCTYPE_BELONGSTO); Integer sourceTypeManuallyCreated = sourceTypeDAO.getIdOfType(tenantId, SourceTypeDAO.SOURCETYPE_MANUALLY_CREATED); Integer viewTypeAdmin = viewTypeDAO.getIdOfType(tenantId, ViewTypeDAO.VIEWTYPE_ADMIN); ItemAssocVO<Integer,Integer> itemAssoc = new ItemAssocVO<Integer,Integer>(tenantId, item, assocTypeBelongsTo, 1.0, cluster.getItem(), sourceTypeManuallyCreated, null, viewTypeAdmin, true, new Date()); try { return itemAssocService.insertOrUpdateItemAssoc(itemAssoc); } catch (DuplicateKeyException e) { throw new ClusterException( "The item was not added to the cluster because the item already was in the cluster."); } }
@ShortCacheable public List<AssociatedItemVO<Integer, Integer>> getItemsFromCluster(Integer tenantId, ClusterVO cluster, Integer numberOfResults, Integer itemType) { IAConstraintVO<Integer, Integer> constraint = new IAConstraintVO<Integer, Integer>( numberOfResults, null, null, null, tenantId, true, false, ItemAssocDAO.DEFAULT_CHANGE_DATE_COLUMN_NAME); return itemAssocService.getItemsFrom(itemType, assocTypeDAO.getIdOfType(tenantId, ClusterService.ASSOCTYPE_BELONGSTO), cluster.getItem(), constraint); }
public ClusterVO getParent(Integer tenantId, String clusterName) { // to avoid a db access, we create a new cluster instead of using load. Only tenant, id, and type are necessary to identify a cluster ClusterVO cluster = new ClusterVO(tenantId, idMappingDAO.lookup(clusterName), itemTypeDAO.getIdOfType(tenantId, ClusterService.ITEMTYPE_CLUSTER), "", ""); return getParent(tenantId, cluster); }
@ShortCacheable private List<AssociatedItemVO<Integer, Integer>> getAllItems(Integer tenantId, ClusterVO cluster, Integer numberOfResults, Integer itemType) { IAConstraintVO<Integer, Integer> constraint = new IAConstraintVO<Integer, Integer>( numberOfResults, null, null, null, tenantId, true, null); //TODO: modify SQL statement: SELECT RANDOM .... LIMIT 100; return itemAssocService.getItemsFrom(itemType, assocTypeDAO.getIdOfType(tenantId, ClusterService.ASSOCTYPE_BELONGSTO), cluster.getItem(), constraint); }