Search in sources :

Example 1 with Project

use of io.crnk.example.dropwizard.mongo.domain.model.Project in project crnk-framework by crnk-project.

the class TaskToProjectRepository method removeRelations.

/**
 * A simple implementation of the removeRelations method which presents the general concept of the method.
 * It SHOULD NOT be used in production because of possible race condition - production ready code should perform an
 * atomic operation.
 *
 * @param task
 * @param projectIds
 * @param fieldName
 */
@Override
public void removeRelations(Task task, Iterable<ObjectId> projectIds, String fieldName) {
    try {
        if (PropertyUtils.getProperty(task, fieldName) != null) {
            Iterable<Project> projects = (Iterable<Project>) PropertyUtils.getProperty(task, fieldName);
            Iterator<Project> iterator = projects.iterator();
            while (iterator.hasNext()) {
                for (ObjectId projectIdToRemove : projectIds) {
                    if (iterator.next().getId().equals(projectIdToRemove)) {
                        iterator.remove();
                        break;
                    }
                }
            }
            List<Project> newProjectList = new ArrayList<>();
            for (Project project : projects) {
                newProjectList.add(project);
            }
            PropertyUtils.setProperty(task, fieldName, newProjectList);
            taskRepository.save(task);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Project(io.crnk.example.dropwizard.mongo.domain.model.Project) ObjectId(org.bson.types.ObjectId)

Example 2 with Project

use of io.crnk.example.dropwizard.mongo.domain.model.Project in project crnk-framework by crnk-project.

the class TaskToProjectRepository method setRelation.

@Override
public void setRelation(Task task, ObjectId projectId, String fieldName) {
    Project project = projectRepository.findOne(projectId, null);
    try {
        PropertyUtils.setProperty(task, fieldName, project);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    taskRepository.save(task);
}
Also used : Project(io.crnk.example.dropwizard.mongo.domain.model.Project)

Example 3 with Project

use of io.crnk.example.dropwizard.mongo.domain.model.Project in project crnk-framework by crnk-project.

the class TaskToProjectRepository method addRelations.

/**
 * A simple implementation of the addRelations method which presents the general concept of the method.
 * It SHOULD NOT be used in production because of possible race condition - production ready code should perform an
 * atomic operation.
 *
 * @param task
 * @param projectIds
 * @param fieldName
 */
@Override
public void addRelations(Task task, Iterable<ObjectId> projectIds, String fieldName) {
    List<Project> newProjectList = new LinkedList<>();
    Iterable<Project> projectsToAdd = projectRepository.findAll(projectIds, null);
    for (Project project : projectsToAdd) {
        newProjectList.add(project);
    }
    try {
        if (PropertyUtils.getProperty(task, fieldName) != null) {
            Iterable<Project> projects = (Iterable<Project>) PropertyUtils.getProperty(task, fieldName);
            for (Project project : projects) {
                newProjectList.add(project);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    try {
        PropertyUtils.setProperty(task, fieldName, newProjectList);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    taskRepository.save(task);
}
Also used : Project(io.crnk.example.dropwizard.mongo.domain.model.Project)

Example 4 with Project

use of io.crnk.example.dropwizard.mongo.domain.model.Project in project crnk-framework by crnk-project.

the class TaskToProjectRepository method findManyTargets.

@Override
public ResourceList<Project> findManyTargets(ObjectId objectId, String fieldName, QuerySpec requestParams) {
    Task task = taskRepository.findOne(objectId, requestParams);
    try {
        DefaultResourceList<Project> result = new DefaultResourceList<>();
        result.addAll((Collection<Project>) PropertyUtils.getProperty(task, fieldName));
        return result;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Project(io.crnk.example.dropwizard.mongo.domain.model.Project) Task(io.crnk.example.dropwizard.mongo.domain.model.Task) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList)

Aggregations

Project (io.crnk.example.dropwizard.mongo.domain.model.Project)4 DefaultResourceList (io.crnk.core.resource.list.DefaultResourceList)1 Task (io.crnk.example.dropwizard.mongo.domain.model.Task)1 ObjectId (org.bson.types.ObjectId)1