// GetProject returns a given project object
func GetProject(dbConn *sql.DB, id string) (types.Project, error) {
	//logit.Info.Println("GetProject called with id=" + id)
	project := types.Project{}

	err := dbConn.QueryRow(fmt.Sprintf("select id, name, description, to_char(updatedt, 'MM-DD-YYYY HH24:MI:SS') from project where id=%s", id)).Scan(&project.ID, &project.Name, &project.Desc, &project.UpdateDate)
	switch {
	case err == sql.ErrNoRows:
		logit.Info.Println("admindb:GetProject:no server with that id")
		return project, err
	case err != nil:
		logit.Info.Println("admindb:GetProject:" + err.Error())
		return project, err
	default:
		logit.Info.Println("admindb:GetProject: Project name returned is " + project.Name)
	}

	project.Containers = make(map[string]string)
	var containers []types.Container
	containers, err = GetAllContainersForProject(dbConn, project.ID)
	if err != nil {
		logit.Info.Println("admindb:GetProject:" + err.Error())
		return project, err
	}

	for i := range containers {
		project.Containers[containers[i].ID] = containers[i].Name
	}
	project.Clusters = make(map[string]string)
	var clusters []types.Cluster
	clusters, err = GetAllClustersForProject(dbConn, project.ID)
	if err != nil {
		logit.Info.Println("admindb:GetProject:" + err.Error())
		return project, err
	}

	for i := range clusters {
		project.Clusters[clusters[i].ID] = clusters[i].Name
	}

	return project, nil
}
// GetAllProjects returns a list of all project objects
func GetAllProjects(dbConn *sql.DB) ([]types.Project, error) {
	//logit.Info.Println("admindb:GetAllProjects: called")
	var rows *sql.Rows
	var err error
	rows, err = dbConn.Query("select id, name, description, to_char(updatedt, 'MM-DD-YYYY HH24:MI:SS') from project order by name")
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	projects := make([]types.Project, 0)
	var containers []types.Container
	var clusters []types.Cluster
	var proxies []types.Proxy

	for rows.Next() {
		project := types.Project{}
		if err = rows.Scan(
			&project.ID,
			&project.Name,
			&project.Desc,
			&project.UpdateDate); err != nil {
			return nil, err
		}

		project.Proxies = make(map[string]string)

		proxies, err = GetAllProxiesForProject(dbConn, project.ID)
		if err != nil {
			logit.Info.Println("admindb:GetAllProjects:" + err.Error())
			return projects, err
		}

		for i := range proxies {
			project.Proxies[proxies[i].ContainerID] = proxies[i].ContainerName
		}

		project.Containers = make(map[string]string)

		containers, err = GetAllContainersForProject(dbConn, project.ID)
		if err != nil {
			logit.Info.Println("admindb:GetAllProjects:" + err.Error())
			return projects, err
		}

		for i := range containers {
			project.Containers[containers[i].ID] = containers[i].Name
		}

		project.Clusters = make(map[string]string)

		clusters, err = GetAllClustersForProject(dbConn, project.ID)
		if err != nil {
			logit.Info.Println("admindb:GetAllProjects:" + err.Error())
			return projects, err
		}

		for i := range clusters {
			project.Clusters[clusters[i].ID] = clusters[i].Name
		}
		projects = append(projects, project)
	}
	if err = rows.Err(); err != nil {
		return nil, err
	}
	return projects, nil
}