001/*
002 * Copyright (c) 2015 Maxim Yunusov
003 *    Licensed under the Apache License, Version 2.0 (the "License");
004 *    you may not use this file except in compliance with the License.
005 *    You may obtain a copy of the License at
006 *
007 *        http://www.apache.org/licenses/LICENSE-2.0
008 *
009 *    Unless required by applicable law or agreed to in writing, software
010 *    distributed under the License is distributed on an "AS IS" BASIS,
011 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 *    See the License for the specific language governing permissions and
013 *    limitations under the License.
014 */
015
016package org.maxur.perfmodel.backend.rest.dto;
017
018import com.fasterxml.jackson.annotation.JsonRawValue;
019import com.fasterxml.jackson.databind.JsonNode;
020import org.jsondoc.core.annotation.ApiObject;
021import org.jsondoc.core.annotation.ApiObjectField;
022import org.maxur.perfmodel.backend.domain.Project;
023
024import javax.xml.bind.annotation.XmlRootElement;
025import java.io.Serializable;
026import java.util.Collection;
027import java.util.List;
028
029import static java.util.stream.Collectors.toList;
030
031/**
032 * @author Maxim Yunusov
033 * @version 1.0 24.11.13
034 */
035@SuppressWarnings("unused")
036@XmlRootElement
037@ApiObject(name = "Project")
038public class ProjectDto implements Serializable {
039
040    private static final long serialVersionUID = -5236202133124299315L;
041
042    @ApiObjectField(required = true, description = "The project identifier")
043    private String id;
044
045    @ApiObjectField(required = true, description = "The project name")
046    private String name;
047
048    @ApiObjectField(required = true, description = "The project version")
049    private int version;
050
051    @ApiObjectField(required = false, description = "The project description")
052    private String description;
053
054    @ApiObjectField(required = false, description = "The Serialized presentation of the object's models", format="[]")
055    @JsonRawValue
056    private String models;
057
058    @ApiObjectField(required = false, description = "The Serialized presentation of the object's view", format="{}")
059    @JsonRawValue
060    private String view;
061
062    @SuppressWarnings("UnusedDeclaration")
063    public ProjectDto() {
064    }
065
066    public ProjectDto(final String id, final String name, final int version, final String description) {
067        this.id = id;
068        this.name = name;
069        this.version = version;
070        this.description = description;
071    }
072
073    public String getId() {
074        return id;
075    }
076
077
078    public String getName() {
079        return name;
080    }
081
082    public int getVersion() {
083        return version;
084    }
085
086    public String getDescription() {
087        return description;
088    }
089
090    public String getModels() {
091        return models;
092    }
093
094    public String getView() {
095        return view;
096    }
097
098    public void setModels(final JsonNode models) {
099        this.models = models == null ? null : models.toString();
100    }
101
102    public void setView(final JsonNode view) {
103        this.view = view == null ? null : view.toString();
104    }
105
106    /**
107     * Creation method. Create DTO List by Project List.
108     * <p>
109     * @param all list of Projects.
110     * @return project's dto List.
111     */
112    public static List<ProjectDto> dtoList(final Collection<Project> all) {
113        return all.stream().map(ProjectDto::dto).collect(toList());
114    }
115
116    /**
117     * Creation method. Create Full DTO by Project.
118     * <p>
119     * @param project domain object 'Project'.
120     * @return project's dto.
121     */
122    public static ProjectDto dto(final Project project) {
123        final ProjectDto dto =
124                new ProjectDto(project.getId(), project.getName(), project.getVersion(), project.getDescription());
125        dto.models = project.getModels();
126        dto.view = project.getView();
127        return dto;
128    }
129
130    /**
131     * Create entity object 'Project' from this dto.
132     *
133     * @return The Project.
134     */
135    public Project assemble() {
136        final Project result = new Project(id, name, version, description);
137        result.setModels(models);
138        result.setView(view);
139        return result;
140    }
141}