// ConvertWorkItemTypeFromModel converts from models to app representation
9060
func ConvertWorkItemTypeFromModel(request *http.Request, t *workitem.WorkItemType) app.WorkItemTypeData {
	spaceTemplateRelatedURL := rest.AbsoluteURL(request, app.SpaceTemplateHref(t.SpaceTemplateID.String()))
	spaceRelatedURL := rest.AbsoluteURL(request, app.SpaceHref(space.SystemSpace.String()))
	var converted = app.WorkItemTypeData{
		Type: APIStringTypeWorkItemType,
		ID:   ptr.UUID(t.ID),
		Attributes: &app.WorkItemTypeAttributes{
			CreatedAt:    ptr.Time(t.CreatedAt.UTC()),
			UpdatedAt:    ptr.Time(t.UpdatedAt.UTC()),
			Version:      &t.Version,
			Description:  t.Description,
			Icon:         t.Icon,
			Name:         t.Name,
			Fields:       map[string]*app.FieldDefinition{},
			CanConstruct: ptr.Bool(t.CanConstruct),
		},
		Relationships: &app.WorkItemTypeRelationships{
			// TODO(kwk): The Space relationship should be deprecated after clients adopted
			Space:         app.NewSpaceRelation(space.SystemSpace, spaceRelatedURL),
			SpaceTemplate: app.NewSpaceTemplateRelation(t.SpaceTemplateID, spaceTemplateRelatedURL),
		},
	}
	for name, def := range t.Fields {
		ct := ConvertFieldTypeFromModel(def.Type)
		converted.Attributes.Fields[name] = &app.FieldDefinition{
			Required:    def.Required,
			Label:       def.Label,
			Description: def.Description,
			Type:        &ct,
		}
	}
	if len(t.ChildTypeIDs) > 0 {
		converted.Relationships.GuidedChildTypes = &app.RelationGenericList{
			Data: make([]*app.GenericData, len(t.ChildTypeIDs)),
		}
		for i, id := range t.ChildTypeIDs {
			converted.Relationships.GuidedChildTypes.Data[i] = &app.GenericData{
				ID:   ptr.String(id.String()),
				Type: ptr.String(APIStringTypeWorkItemType),
			}
		}
	}
	return converted
}
973638
func convertWorkerID(id interface{}) (string, error) {
	if id == nil {
		return "", nil
	}

	switch id.(type) {
	case int:
		num := id.(int)
		return fmt.Sprintf("%v",num), nil
	case uint64:
		num := id.(uint64)
		return fmt.Sprintf("%v",num), nil
	case float64:
		num := id.(float64)
		return fmt.Sprintf("%.0f",num), nil
	case json.Number:
		return string(id.(json.Number)), nil
	case string:
		return id.(string), nil
	}

	// An unexpected type. We'll get the type name for the error, but this
	// will fail the export.. so it only uses reflect once.
	name := reflect.TypeOf(id)
	return "", &UnknownElasticStructError{fmt.Sprintf("%v", name), "ccworkerid"}
}
377755
func NewWorkitemtypesController(service *goa.Service, db application.DB, config workItemTypesControllerConfiguration) *WorkitemtypesController {
	return &WorkitemtypesController{
		Controller: service.NewController("WorkitemtypesController"),
		db:         db,
		config:     config,
	}
}
592434
func (s *StartChildWorkflowExecutionFailedEventAttributes) SetWorkflowType(v *WorkflowType) *StartChildWorkflowExecutionFailedEventAttributes {
	s.WorkflowType = v
	return s
}
1189893
func (r *GormWorkItemRepository) CheckTypeAndSpaceShareTemplate(ctx context.Context, wit *WorkItemType, spaceID uuid.UUID) (bool, error) {
	// Prohibit creation of work items from a base type.
	if !wit.CanConstruct {
		return false, errors.NewForbiddenError(fmt.Sprintf("cannot construct work items from %q (%s)", wit.Name, wit.ID))
	}
	var exists bool
	// Prohibit creation of work items from a type that doesn't belong to current space template
	query := fmt.Sprintf(`
			SELECT EXISTS (
				SELECT 1 from %[1]s WHERE id=$1 AND space_template_id = (
					SELECT space_template_id FROM %[2]s WHERE id=$2
				)
			)`, wit.TableName(), space.Space{}.TableName())
	err := r.db.Raw(query, wit.ID, spaceID).Row().Scan(&exists)
	if err == nil && !exists {
		return false, errors.NewBadParameterErrorFromString(
			fmt.Sprintf("Workitem Type %q (ID: %s) does not belong to the current space template", wit.Name, wit.ID),
		)
	}
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"space_id":         spaceID,
			"workitem_type_id": wit.ID,
			"err":              err,
		}, "unable to fetch workitem types related to current space")
		return false, errors.NewInternalError(ctx, errs.Wrapf(err, "unable to verify if %s exists", wit.ID))
	}
	return true, nil
}
920653
func includeParentWorkItem(ctx context.Context, ancestors link.AncestorList, childLinks link.WorkItemLinkList) WorkItemConvertFunc {
	return func(request *http.Request, wi *workitem.WorkItem, wi2 *app.WorkItem) error {
		var parentID *uuid.UUID
		// If we have an ancestry we can lookup the parent in no time.
		if ancestors != nil && len(ancestors) != 0 {
			p := ancestors.GetParentOf(wi.ID)
			if p != nil {
				parentID = &p.ID
			}
		}
		// If no parent ID was found in the ancestor list, see if the child
		// link list contains information to use.
		if parentID == nil && childLinks != nil && len(childLinks) != 0 {
			p := childLinks.GetParentIDOf(wi.ID, link.SystemWorkItemLinkTypeParentChildID)
			if p != uuid.Nil {
				parentID = &p
			}
		}
		if wi2.Relationships.Parent == nil {
			wi2.Relationships.Parent = &app.RelationKindUUID{}
		}
		if parentID != nil {
			if wi2.Relationships.Parent.Data == nil {
				wi2.Relationships.Parent.Data = &app.DataKindUUID{}
			}
			wi2.Relationships.Parent.Data.ID = *parentID
			wi2.Relationships.Parent.Data.Type = APIStringTypeWorkItem
		}
		return nil
	}
}
450207
func (r Layout_Item) GetLayoutItemType() (resp datatypes.Layout_Item_Type, err error) {
	err = r.Session.DoRequest("SoftLayer_Layout_Item", "getLayoutItemType", nil, &r.Options, &resp)
	return
}
934360
func NewWorkItemTypeGroupController(service *goa.Service, db application.DB) *WorkItemTypeGroupController {
	return &WorkItemTypeGroupController{
		Controller: service.NewController("WorkItemTypeGroupController"),
		db:         db,
	}
}
654457
func (m *ModelStatus) UnitWorkloadVersion(unitName string) (string, error) {
	info, err := m.getStatus(globalWorkloadVersionKey(unitName), "workload")
	if err != nil {
		return "", err
	}
	return info.Message, nil
}
1331167
func getWorkItemsOfLinks(ctx context.Context, appl application.Application, req *http.Request, linksDataArr []*app.WorkItemLinkData) ([]*app.WorkItem, error) {
	// Build our "set" of distinct work item IDs
	idMap := map[uuid.UUID]struct{}{}
	idArr := []uuid.UUID{}
	for _, linkData := range linksDataArr {
		src := linkData.Relationships.Source.Data.ID
		tgt := linkData.Relationships.Target.Data.ID
		if _, ok := idMap[src]; !ok {
			idMap[src] = struct{}{}
			idArr = append(idArr, src)
		}
		if _, ok := idMap[tgt]; !ok {
			idMap[tgt] = struct{}{}
			idArr = append(idArr, tgt)
		}
	}

	// Fetch all work items specified in the array
	res := []*app.WorkItem{}
	for _, id := range idArr {
		wi, err := appl.WorkItems().LoadByID(ctx, id)
		if err != nil {
			return nil, errs.WithStack(err)
		}
		wit, err := appl.WorkItemTypes().Load(ctx, wi.Type)
		if err != nil {
			return nil, errs.Wrapf(err, "failed to load work item type: %s", wi.Type)
		}
		converted, err := ConvertWorkItem(req, *wit, *wi)
		if err != nil {
			return nil, errs.WithStack(err)
		}
		res = append(res, converted)
	}
	return res, nil
}
935423
func (s *StartWorkflowExecutionInput) SetWorkflowType(v *WorkflowType) *StartWorkflowExecutionInput {
	s.WorkflowType = v
	return s
}
348597
func NewWorkItemTypeGroupsController(service *goa.Service, db application.DB) *WorkItemTypeGroupsController {
	return &WorkItemTypeGroupsController{
		Controller: service.NewController("WorkItemTypeGroupsController"),
		db:         db,
	}
}
318408
func ConvertSpaceFromModel(request *http.Request, sp space.Space, options ...SpaceConvertFunc) (*app.Space, error) {
	selfURL := rest.AbsoluteURL(request, app.SpaceHref(sp.ID))
	spaceIDStr := sp.ID.String()
	relatedIterations := rest.AbsoluteURL(request, fmt.Sprintf("/api/spaces/%s/iterations", spaceIDStr))
	relatedAreas := rest.AbsoluteURL(request, fmt.Sprintf("/api/spaces/%s/areas", spaceIDStr))
	relatedBacklog := rest.AbsoluteURL(request, fmt.Sprintf("/api/spaces/%s/backlog", spaceIDStr))
	relatedCodebases := rest.AbsoluteURL(request, fmt.Sprintf("/api/spaces/%s/codebases", spaceIDStr))
	relatedWorkItems := rest.AbsoluteURL(request, fmt.Sprintf("/api/spaces/%s/workitems", spaceIDStr))
	relatedWorkItemTypes := rest.AbsoluteURL(request, app.SpaceTemplateHref(sp.SpaceTemplateID)+"/workitemtypes")
	relatedWorkItemLinkTypes := rest.AbsoluteURL(request, app.SpaceTemplateHref(sp.SpaceTemplateID)+"/workitemlinktypes")
	relatedOwners := rest.AbsoluteURL(request, app.UsersHref(sp.OwnerID.String()))
	relatedCollaborators := rest.AbsoluteURL(request, fmt.Sprintf("/api/spaces/%s/collaborators", spaceIDStr))
	relatedFilters := rest.AbsoluteURL(request, "/api/filters")
	relatedLabels := rest.AbsoluteURL(request, fmt.Sprintf("/api/spaces/%s/labels", spaceIDStr))
	relatedWorkitemTypeGroups := rest.AbsoluteURL(request, app.SpaceTemplateHref(sp.SpaceTemplateID)+"/workitemtypegroups")
	relatedSpaceTemplateURL := rest.AbsoluteURL(request, app.SpaceTemplateHref(sp.SpaceTemplateID))

	s := &app.Space{
		ID:   &sp.ID,
		Type: APIStringTypeSpace,
		Attributes: &app.SpaceAttributes{
			Name:        &sp.Name,
			Description: &sp.Description,
			CreatedAt:   &sp.CreatedAt,
			UpdatedAt:   &sp.UpdatedAt,
			Version:     &sp.Version,
		},
		Links: &app.GenericLinksForSpace{
			Self:    &selfURL,
			Related: &selfURL, //TODO (xcoulon): remove this link
			Backlog: &app.BacklogGenericLink{ //TODO (xcoulon): remove this link
				Self: &relatedBacklog,
			},
			Workitemtypes:     &relatedWorkItemTypes,     //TODO (xcoulon): remove this link
			Workitemlinktypes: &relatedWorkItemLinkTypes, //TODO (xcoulon): remove this link
			Filters:           &relatedFilters,           //TODO (xcoulon): remove this link
		},
		Relationships: &app.SpaceRelationships{
			Areas: &app.RelationGeneric{
				Links: &app.GenericLinks{
					Related: &relatedAreas,
				},
			},
			Backlog: &app.RelationGeneric{
				Links: &app.GenericLinks{
					Related: &relatedBacklog,
				},
			},
			Codebases: &app.RelationGeneric{
				Links: &app.GenericLinks{
					Related: &relatedCodebases,
				},
			},
			Collaborators: &app.RelationGeneric{
				Links: &app.GenericLinks{
					Related: &relatedCollaborators,
				},
			},
			Filters: &app.RelationGeneric{
				Links: &app.GenericLinks{
					Related: &relatedFilters,
				},
			},
			OwnedBy: &app.SpaceOwnedBy{
				Data: &app.IdentityRelationData{
					Type: "identities",
					ID:   &sp.OwnerID,
				},
				Links: &app.GenericLinks{
					Related: &relatedOwners,
				},
			},
			Iterations: &app.RelationGeneric{
				Links: &app.GenericLinks{
					Related: &relatedIterations,
				},
			},
			Labels: &app.RelationGeneric{
				Links: &app.GenericLinks{
					Related: &relatedLabels,
				},
			},
			Workitems: &app.RelationGeneric{
				Links: &app.GenericLinks{
					Related: &relatedWorkItems,
				},
			},
			Workitemtypes: &app.RelationGeneric{
				Links: &app.GenericLinks{
					Related: &relatedWorkItemTypes,
				},
			},
			Workitemlinktypes: &app.RelationGeneric{
				Links: &app.GenericLinks{
					Related: &relatedWorkItemLinkTypes,
				},
			},
			Workitemtypegroups: &app.RelationGeneric{
				Links: &app.GenericLinks{
					Related: &relatedWorkitemTypeGroups,
				},
			},
			SpaceTemplate: app.NewSpaceTemplateRelation(sp.SpaceTemplateID, relatedSpaceTemplateURL),
		},
	}
	// apply options (ie, if extra content needs to be provided in the response element)
	for _, option := range options {
		err := option(request, &sp, s)
		if err != nil {
			return nil, err
		}
	}
	return s, nil
}
1172179
func (ec *EstablishingController) processNextWorkItem() bool {
	key, quit := ec.queue.Get()
	if quit {
		return false
	}
	defer ec.queue.Done(key)

	err := ec.syncFn(key.(string))
	if err == nil {
		ec.queue.Forget(key)
		return true
	}

	utilruntime.HandleError(fmt.Errorf("%v failed with: %v", key, err))
	ec.queue.AddRateLimited(key)

	return true
}
767722
func (f FieldDefinition) ConvertFromModel(name string, value interface{}) (interface{}, error) {
	if f.Required && value == nil {
		return nil, fmt.Errorf("Value %s is required", name)
	}
	return f.Type.ConvertFromModel(value)
}
80159
func (c *DiscoveryController) processNextWorkItem() bool {
	key, quit := c.queue.Get()
	if quit {
		return false
	}
	defer c.queue.Done(key)

	err := c.syncFn(key.(schema.GroupVersion))
	if err == nil {
		c.queue.Forget(key)
		return true
	}

	utilruntime.HandleError(fmt.Errorf("%v failed with: %v", key, err))
	c.queue.AddRateLimited(key)

	return true
}
451037
func (r *GormWorkItemTypeRepository) loadChildTypeList(ctx context.Context, parentTypeID uuid.UUID) ([]uuid.UUID, error) {
	types := []ChildType{}
	db := r.db.Model(&types).Where("parent_work_item_type_id=?", parentTypeID).Order("position ASC").Find(&types)
	if db.RecordNotFound() {
		log.Error(ctx, map[string]interface{}{"wit_id": parentTypeID}, "work item type child types not found")
		return nil, errors.NewNotFoundError("work item type child types", parentTypeID.String())
	}
	if err := db.Error; err != nil {
		return nil, errors.NewInternalError(ctx, err)
	}
	res := make([]uuid.UUID, len(types))
	for i, childType := range types {
		res[i] = childType.ChildWorkItemTypeID
	}
	return res, nil
}