com.capitalone.dashboard.service.DashboardServiceImpl Maven / Gradle / Ivy
package com.capitalone.dashboard.service;
import com.capitalone.dashboard.misc.HygieiaException;
import com.capitalone.dashboard.model.Collector;
import com.capitalone.dashboard.model.CollectorItem;
import com.capitalone.dashboard.model.CollectorType;
import com.capitalone.dashboard.model.Component;
import com.capitalone.dashboard.model.Dashboard;
import com.capitalone.dashboard.model.DashboardType;
import com.capitalone.dashboard.model.Widget;
import com.capitalone.dashboard.repository.CollectorItemRepository;
import com.capitalone.dashboard.repository.CollectorRepository;
import com.capitalone.dashboard.repository.ComponentRepository;
import com.capitalone.dashboard.repository.DashboardRepository;
import com.capitalone.dashboard.repository.PipelineRepository;
import com.capitalone.dashboard.repository.ServiceRepository;
import com.capitalone.dashboard.util.UnsafeDeleteException;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Service
public class DashboardServiceImpl implements DashboardService {
private final DashboardRepository dashboardRepository;
private final ComponentRepository componentRepository;
private final CollectorRepository collectorRepository;
private final CollectorItemRepository collectorItemRepository;
@SuppressWarnings("unused")
private final PipelineRepository pipelineRepository; //NOPMD
private final ServiceRepository serviceRepository;
@Autowired
public DashboardServiceImpl(DashboardRepository dashboardRepository,
ComponentRepository componentRepository,
CollectorRepository collectorRepository,
CollectorItemRepository collectorItemRepository,
ServiceRepository serviceRepository,
PipelineRepository pipelineRepository) {
this.dashboardRepository = dashboardRepository;
this.componentRepository = componentRepository;
this.collectorRepository = collectorRepository;
this.collectorItemRepository = collectorItemRepository;
this.serviceRepository = serviceRepository;
this.pipelineRepository = pipelineRepository; //TODO - Review if we need this param, seems it is never used according to PMD
}
@Override
public Iterable all() {
return dashboardRepository.findAll(new Sort(Sort.Direction.ASC, "title"));
}
@Override
public Dashboard get(ObjectId id) {
Dashboard dashboard = dashboardRepository.findOne(id);
if (!dashboard.getApplication().getComponents().isEmpty()) {
// Add transient Collector instance to each CollectorItem
Map> itemMap = dashboard.getApplication().getComponents().get(0).getCollectorItems();
Iterable collectors = collectorsFromItems(itemMap);
for (List collectorItems : itemMap.values()) {
for (CollectorItem collectorItem : collectorItems) {
collectorItem.setCollector(getCollector(collectorItem.getCollectorId(), collectors));
}
}
}
return dashboard;
}
@Override
public Dashboard create(Dashboard dashboard) throws HygieiaException {
Iterable components = componentRepository.save(dashboard.getApplication().getComponents());
try {
return dashboardRepository.save(dashboard);
} catch (Exception e) {
componentRepository.delete(components);
throw new HygieiaException("Failed creating dashboard.", HygieiaException.ERROR_INSERTING_DATA);
}
}
@Override
public Dashboard update(Dashboard dashboard) throws HygieiaException {
return create(dashboard);
}
@Override
public void delete(ObjectId id) {
Dashboard dashboard = dashboardRepository.findOne(id);
if(!isSafeDelete(dashboard)){
throw new UnsafeDeleteException("Cannot delete team dashboard "+dashboard.getTitle()+" as it is referenced by program dashboards.");
}
componentRepository.delete(dashboard.getApplication().getComponents());
// Remove this Dashboard's services and service dependencies
serviceRepository.delete(serviceRepository.findByDashboardId(id));
for (com.capitalone.dashboard.model.Service service : serviceRepository.findByDependedBy(id)) { //NOPMD - using fully qualified or we pickup an incorrect spring class
service.getDependedBy().remove(id);
serviceRepository.save(service);
}
dashboardRepository.delete(dashboard);
}
private boolean isSafeDelete(Dashboard dashboard){
boolean isSafe = false;
if(dashboard.getType() == null || dashboard.getType().equals(DashboardType.Team)){
isSafe = isSafeTeamDashboardDelete(dashboard);
}else {
isSafe = true;
}
return isSafe;
}
private boolean isSafeTeamDashboardDelete(Dashboard dashboard){
boolean isSafe = false;
List productCollectors = collectorRepository.findByCollectorType(CollectorType.Product);
if(productCollectors.isEmpty()){
return true;
}
Collector productCollector = productCollectors.get(0);
CollectorItem teamDashboardCollectorItem = collectorItemRepository.findTeamDashboardCollectorItemsByCollectorIdAndDashboardId(productCollector.getId(), dashboard.getId().toString());
//// TODO: 1/21/16 Is this safe? What if we add a new team dashbaord and quickly add it to a product and then delete it?
if(teamDashboardCollectorItem == null){
return true;
}
if(dashboardRepository.findProductDashboardsByTeamDashboardCollectorItemId(teamDashboardCollectorItem.getId().toString()).isEmpty()) {
isSafe = true;
}
return isSafe;
}
@Override
public Component associateCollectorToComponent(ObjectId componentId, List collectorItemIds) {
if (componentId == null || collectorItemIds == null) {
// Not all widgets gather data from collectors
return null;
}
com.capitalone.dashboard.model.Component component = componentRepository.findOne(componentId); //NOPMD - using fully qualified name for clarity
//we can not assume what collector item is added, what is removed etc so, we will
//refresh the association. First disable all collector items, then remove all and re-add
//First: disable all collectorItems of the Collector TYPEs that came in with the request.
//Second: remove all the collectorItem association of the Collector Type that came in
HashSet incomingTypes = new HashSet<>();
HashSet toSaveCollectorItemList = new HashSet<>();
for (ObjectId collectorItemId : collectorItemIds) {
CollectorItem collectorItem = collectorItemRepository.findOne(collectorItemId);
Collector collector = collectorRepository.findOne(collectorItem.getCollectorId());
if (!incomingTypes.contains(collector.getCollectorType())) {
incomingTypes.add(collector.getCollectorType());
List cItems = component.getCollectorItems(collector.getCollectorType());
if (!CollectionUtils.isEmpty(cItems)) {
for (CollectorItem ci : cItems) {
ci.setEnabled(false);
toSaveCollectorItemList.add(ci);
}
}
component.getCollectorItems().remove(collector.getCollectorType());
}
}
//Last step: add collector items that came in
for (ObjectId collectorItemId : collectorItemIds) {
CollectorItem collectorItem = collectorItemRepository.findOne(collectorItemId);
Collector collector = collectorRepository.findOne(collectorItem.getCollectorId());
component.addCollectorItem(collector.getCollectorType(), collectorItem);
if (!collectorItem.isEnabled()) {
toSaveCollectorItemList.remove(collectorItem);
collectorItem.setEnabled(true);
toSaveCollectorItemList.add(collectorItem);
}
// set transient collector property
collectorItem.setCollector(collector);
}
collectorItemRepository.save(toSaveCollectorItemList);
componentRepository.save(component);
return component;
}
@Override
public Widget addWidget(Dashboard dashboard, Widget widget) {
widget.setId(ObjectId.get());
dashboard.getWidgets().add(widget);
dashboardRepository.save(dashboard);
return widget;
}
@Override
public Widget getWidget(Dashboard dashboard, ObjectId widgetId) {
return Iterables.find(dashboard.getWidgets(), new WidgetByIdPredicate(widgetId));
}
@Override
public Widget updateWidget(Dashboard dashboard, Widget widget) {
int index = dashboard.getWidgets().indexOf(widget);
dashboard.getWidgets().set(index, widget);
dashboardRepository.save(dashboard);
return widget;
}
private static final class WidgetByIdPredicate implements Predicate {
private final ObjectId widgetId;
public WidgetByIdPredicate(ObjectId widgetId) {
this.widgetId = widgetId;
}
@Override
public boolean apply(Widget widget) {
return widget.getId().equals(widgetId);
}
}
private Iterable collectorsFromItems(Map> itemMap) {
Set collectorIds = new HashSet<>();
for (List collectorItems : itemMap.values()) {
for (CollectorItem collectorItem : collectorItems) {
collectorIds.add(collectorItem.getCollectorId());
}
}
return collectorRepository.findAll(collectorIds);
}
private Collector getCollector(final ObjectId collectorId, Iterable collectors) {
return Iterables.tryFind(collectors, new Predicate() {
@Override
public boolean apply(Collector collector) {
return collector.getId().equals(collectorId);
}
}).orNull();
}
@Override
public List getOwnedDashboards(String owner) {
List myDashboard=dashboardRepository.findByOwner(owner);
return myDashboard;
}
@Override
public String getDashboardOwner(String dashboardTitle) {
String dashboardOwner=dashboardRepository.findByTitle(dashboardTitle).get(0).getOwner();
return dashboardOwner;
}
@SuppressWarnings("unused")
private DashboardType getDashboardType(Dashboard dashboard){
if(dashboard.getType() != null){
return dashboard.getType();
}
return DashboardType.Team;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy