package com.capitalone.dashboard.model;

import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A collection of widgets, collectors and application components that represent a software
 * project under development and/or in production use.
 *
 */
@Document(collection="dashboards")
public class Dashboard extends BaseModel {
    private String template;

    //NOTE Mongodb treats strings as different if they have different case
    @Indexed(unique=true)
    private String title;

    private List<Widget> widgets = new ArrayList<>();
    private String owner;
    private DashboardType type;

    private Application application;

    Dashboard() {
    }

    public Dashboard(String template, String title, Application application,String owner, DashboardType type) {
        this.template = template;
        this.title = title;
        this.application = application;
        this.owner = owner;
        this.type = type;
    }

    public String getTemplate() {
        return template;
    }

    public void setTemplate(String template) {
        this.template = template;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Application getApplication() {
        return application;
    }

    public void setApplication(Application application) {
        this.application = application;
    }

    public List<Widget> getWidgets() {
        return widgets;
    }

	public String getOwner() {
		return owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}

    public DashboardType getType(){ return this.type; }

    public void setType(DashboardType type) { this.type = type; }

    /**
     * Finds the mapped names for each stage type from the widget options
     * @return
     */
	public Map<PipelineStageType, String> findEnvironmentMappings(){
        Map<String, String> environmentMappings = null;
        for(Widget widget : this.getWidgets()) {
            if (widget.getName().equalsIgnoreCase("pipeline")) {
                environmentMappings =  (Map<String, String>) widget.getOptions().get("mappings");
            }
        }
        Map<PipelineStageType, String> stageTypeToEnvironmentNameMap = new HashMap<>();
        if(environmentMappings != null && !environmentMappings.isEmpty()){
            for (Map.Entry<String,String> mapping : environmentMappings.entrySet()) {
                stageTypeToEnvironmentNameMap.put(PipelineStageType.fromString((String) mapping.getKey()), (String) mapping.getValue());
            }
        }
        return stageTypeToEnvironmentNameMap;
    }

}

@@ -2052,24 +2052,25 @@
 ings()%7B%0A
+%0A
         
 Map%3CStri
@@ -2057,24 +2057,28 @@
 )%7B%0A%0A        
+Hash
 Map%3CString, 
@@ -2108,19 +2108,30 @@
 ings = n
-ull
+ew HashMap%3C%3E()
 ;%0A      
@@ -2258,53 +2258,42 @@
     
-environmentMappings
+HashMap%3C?, ?%3E gh
  =
- 
  (
-Map%3CString, String
+HashMap%3C?, ?
 %3E)
- 
 widg
@@ -2325,16 +2325,193 @@
 ings%22);%0A
+                for (Map.Entry%3C?, ?%3E entry : gh.entrySet()) %7B%0A                    environmentMappings.put((String) entry.getKey(), (String) entry.getValue());%0A                %7D%0A
         
@@ -2606,32 +2606,32 @@
 ew HashMap%3C%3E();%0A
+
         if(envir
@@ -2629,39 +2629,8 @@
  if(
-environmentMappings != null && 
 !env

Fix for issue 441