private void removeComponentLeadsForUser(final ApplicationUser user) { for (final ProjectComponent component : getComponentsUserLeads(user)) { MutableProjectComponent newProjectComponent = MutableProjectComponent.copy(component); newProjectComponent.setLead(null); if (component.getAssigneeType() == AssigneeTypes.COMPONENT_LEAD) { newProjectComponent.setAssigneeType(AssigneeTypes.PROJECT_DEFAULT); } try { componentManager.update(newProjectComponent); } catch (EntityNotFoundException e) { // We only just fetched this component, but perhaps someone deleted it concurrently ... } } }
/** * Convert the specified MutableProjectComponent to a ProjectComponent object. * * @param value MutableProjectComponent to be converted into a ProjectComponent. * @return new instance of ProjectComponent with same values as given in the parameter object */ public ProjectComponent convertToProjectComponent(MutableProjectComponent value) { return new ProjectComponentImpl(value.getId(), value.getName(), value.getDescription(), value.getLead(), value.getAssigneeType(), value.getProjectId(), value.getGenericValue()); }
public MutableProjectComponent(Long id, String name, String description, String lead, long assigneeType, Long projectId) { this.id = id; setDescription(description); this.lead = lead; this.name = name; this.projectId = projectId; this.assigneeType = assigneeType; }
/** * Persist the component. If component has no ID (null) it is inserted to the database and added to the cache, * otherwise an update operation is performed on both cache and database. * * @param component component to persist * @throws EntityNotFoundException in case of update if the component does not exist (maybe was deleted :-) * @throws com.atlassian.jira.exception.DataAccessException if cannot persist the component */ public MutableProjectComponent store(MutableProjectComponent component) throws EntityNotFoundException, DataAccessException { MutableProjectComponent copy = MutableProjectComponent.copy(component); MutableProjectComponent newComponent; if (copy.getId() == null) { newComponent = insert(copy); } else { newComponent = update(copy); } return newComponent; }
validateName(component); Long id = component.getId(); if (id == null) String name = component.getName(); componentParams.put(FIELD_DESCRIPTION, component.getDescription()); componentParams.put(FIELD_LEAD, component.getLead()); componentParams.put(FIELD_PROJECT, component.getProjectId()); componentParams.put(FIELD_ASSIGNEE_TYPE, component.getAssigneeType()); component.setId(componentGV.getLong(FIELD_ID)); component.setGenericValue(componentGV); if (findComponentGV(FIELD_ID, component.getId()) == null) GenericValue componentGV = component.getGenericValue(); if (componentGV == null) componentGV.setString(FIELD_NAME, component.getName()); componentGV.setString(FIELD_DESCRIPTION, component.getDescription()); componentGV.setString(FIELD_LEAD, component.getLead()); componentGV.set(FIELD_ASSIGNEE_TYPE, component.getAssigneeType()); try return MutableProjectComponent.copy(component);
currentComponent.setDescription(bean.getDescription()); currentComponent.setName(bean.getName()); currentComponent.setLead(bean.getLead().getName()); currentComponent.setLead(null); currentComponent.setLead(leadUser.getKey()); currentComponent.setAssigneeType(bean.getAssigneeType().getId());
try MutableProjectComponent old = find(component.getId()); if (!old.equalsName(component)) if (containsName(component.getName(), component.getProjectId())) throw new IllegalArgumentException("New component name '" + component.getName() + "' is not unique!");
@Override void validateData() { // validate lead validateLead(this, component.getLead()); // validate name Long id = component.getId(); try { ProjectComponent oldComponent = projectComponentManager.find(id); String oldName = oldComponent.getName(); if (!oldName.equalsIgnoreCase(component.getName())) { validateName(this, oldName, component.getName(), oldComponent.getProjectId()); } } catch (EntityNotFoundException e) { if (id != null) { addErrorKey(KEY_NO_SUCH_COMPONENT_WITH_ID, id.toString(), ErrorCollection.Reason.VALIDATION_FAILED); } else { addErrorKey(KEY_NO_SUCH_COMPONENT, null, ErrorCollection.Reason.VALIDATION_FAILED); } } }
/** * Looks up all components that are related to the project with given ID. * * @param projectId project ID * @return a collection of ProjectComponent objects that are related to the project with given ID */ public Collection<MutableProjectComponent> findAllForProject(Long projectId) { Collection<ProjectComponent> components = projectIdToComponentsMap.get(projectId); return MutableProjectComponent.copy(components); }
public ErrorCollection execute(ApplicationUser user) throws GenericEntityException { ErrorCollection errorCollection = new SimpleErrorCollection(); final Set<Map.Entry<ProjectComponent, Long>> componentEntries = componentAssigneeTypes.entrySet(); for (final Map.Entry<ProjectComponent, Long> entry : componentEntries) { final Long assigneeType = entry.getValue(); final Long componentId = entry.getKey().getId(); MutableProjectComponent projectComponent = MutableProjectComponent.copy(projectComponentService.find(user, errorCollection, componentId)); projectComponent.setAssigneeType(assigneeType); projectComponentService.update(user, errorCollection, projectComponent); } return errorCollection; }
/** * Copy given component * * @param c component to copy * @return new instance of MutableProjectComponent set with values of the given component */ public static MutableProjectComponent copy(final ProjectComponent c) { MutableProjectComponent mutableProjectComponent = new MutableProjectComponent(c.getId(), c.getName(), c.getDescription(), c.getLead(), c.getAssigneeType(), c.getProjectId()); mutableProjectComponent.setGenericValue(c.getGenericValue()); return mutableProjectComponent; }
try final String name = component.getName(); if (containsName(name, component.getProjectId())) componentIdToComponentMap.remove(component.getId()); projectIdToComponentsMap.remove(component.getProjectId());
/** * Returns true if same object, instance of MutableProjectComponent and equal ID. * * @param o object to compare with * @return true if same object, instance of MutableProjectComponent and equal ID; false otherwise */ public boolean equals(Object o) { if (this == o) { return true; } if (o == null || !(o instanceof MutableProjectComponent)) { return false; } Long thatId = ((MutableProjectComponent) o).getId(); return thatId != null && thatId.equals(id); }
/** * Create a new ProjectComponent object associated with the project with the ID specified and with the values given. * * @param name name of component * @param description description of component * @param lead user name associated with component * @param projectId ID of project that component is associated with * @return new instance of ProjectComponent with the values specified */ public ProjectComponent create(String name, String description, String lead, long assigneeType, Long projectId) throws IllegalArgumentException { try { MutableProjectComponent component = new MutableProjectComponent(null, name, description, lead, assigneeType, projectId); ProjectComponent projectComponent = convertToProjectComponent(store.store(component)); eventPublisher.publish(new ProjectComponentCreatedEvent(projectComponent)); return projectComponent; } catch (EntityNotFoundException e) { // Insert should never cause this exception - so this return statement should never be reached return null; } }
/** * Retrieves all ProjectComponents that have the given user as their lead. * Not synchronised, because findAll() returns a private copy of all components. * * @param userKey key of the lead user * @return possibly empty Collection of ProjectComponents. */ public Collection findComponentsBylead(String userKey) { Collection<ProjectComponent> leadComponents = new ArrayList<ProjectComponent>(); Collection components = findAll(); for (final Object component : components) { MutableProjectComponent projectComponent = (MutableProjectComponent) component; if (projectComponent != null && TextUtils.stringSet(projectComponent.getLead()) && projectComponent.getLead().equals(userKey)) { leadComponents.add(projectComponent); } } return leadComponents; }
/** * Copy given collection of components * * @param c collection of components to copy * @return new instance collection of MutableProjectComponent objects set with values of the given components */ public static Collection<MutableProjectComponent> copy(final Collection<ProjectComponent> c) { if (c != null && !c.isEmpty()) { final List copyList = Lists.newArrayListWithCapacity(c.size()); for (final Object aC : c) { copyList.add(MutableProjectComponent.copy((ProjectComponent) aC)); } return copyList; } else { return Collections.EMPTY_LIST; } }
/** * Convert the specified GenericValue representing a MutableProjectComponent object to a MutableProjectComponent * object. * * @param componentGV GenericValue object representing a MutableProjectComponent object to be converted to a * MutableProjectComponent object * @return new MutableProjectComponent object that represents the original GenericValue object */ public MutableProjectComponent convertToComponent(GenericValue componentGV) { Long id = componentGV.getLong(OfBizProjectComponentStore.FIELD_ID); String name = componentGV.getString(OfBizProjectComponentStore.FIELD_NAME); String description = componentGV.getString(OfBizProjectComponentStore.FIELD_DESCRIPTION); String lead = componentGV.getString(OfBizProjectComponentStore.FIELD_LEAD); Long projectId = componentGV.getLong(OfBizProjectComponentStore.FIELD_PROJECT); Long assigneeTypeLong = componentGV.getLong(OfBizProjectComponentStore.FIELD_ASSIGNEE_TYPE); long assigneeType; if (assigneeTypeLong != null) { assigneeType = assigneeTypeLong; } else { assigneeType = AssigneeTypes.PROJECT_DEFAULT; } MutableProjectComponent mutableProjectComponent = new MutableProjectComponent(id, name, description, lead, assigneeType, projectId); mutableProjectComponent.setGenericValue(componentGV); return mutableProjectComponent; }
/** * Validates that the name is not null * * @param component component to validate * @throws IllegalArgumentException if name is null */ private void validateName(MutableProjectComponent component) throws IllegalArgumentException { if (component.getName() == null) { throw new IllegalArgumentException("Name is required, was null."); } }
@Override void executeOnSuccess() { try { result = projectComponentManager.update(component); } catch (EntityNotFoundException e) { Long id = component.getId(); if (id != null) { addErrorKey(KEY_NO_SUCH_COMPONENT_WITH_ID, id.toString(), ErrorCollection.Reason.SERVER_ERROR); } else { addErrorKey(KEY_NO_SUCH_COMPONENT, null, ErrorCollection.Reason.SERVER_ERROR); } } }