001/*
002 * Copyright (c) 2013 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.domain;
017
018import java.io.Serializable;
019import java.util.Objects;
020import java.util.Optional;
021
022/**
023 * @author Maxim Yunusov
024 * @version 1.0 24.11.13
025 */
026public class Project implements Serializable {
027
028    private static final long serialVersionUID = -8859706675434765594L;
029
030    private String id;
031
032    private String name;
033
034    private int version;
035
036    private String description;
037
038    private String models;
039
040    private String view;
041
042    @SuppressWarnings("unused")
043    public Project() {
044    }
045
046    public Project(final String id, final String name, final int version, final String description) {
047        this.id = id;
048        this.name = name;
049        this.version = version;
050        this.description = description;
051    }
052/*
053
054    public Project cloneWith(final String rawData) {
055        final Project result = new Project(this.id, this.name, this.version);
056        result.raw = rawData;
057        return result;
058    }*/
059
060    public Project brief() {
061        return new Project(this.id, this.name, this.version, this.description);
062    }
063
064    public String getName() {
065        return name;
066    }
067
068    public String getId() {
069        return id;
070    }
071
072    public int getVersion() {
073        return version;
074    }
075
076    public String getDescription() {
077        return description;
078    }
079
080    public String getModels() {
081        return models;
082    }
083
084    public String getView() {
085        return view;
086    }
087
088    public void setModels(final String models) {
089        this.models = models;
090    }
091
092    public void setView(final String view) {
093        this.view = view;
094    }
095
096    public void incVersion() {
097        this.version++;
098    }
099
100    public void makeVersion() {
101        this.version = 1;
102    }
103
104    public void checkUniqueId(Optional<Project> otherWithSameId) throws ConflictException {
105        if (otherWithSameId.isPresent()) {
106            throw new ConflictException("Another project with same id '%s' already exists.", id);
107        }
108    }
109
110    public boolean isSame(final Optional<Project> other) {
111        //CHECKSTYLE:OFF: checkstyle:simplifybooleanexpression
112        return other.isPresent()
113                && Objects.equals(id, other.get().id)
114                && Objects.equals(this.name, other.get().name)
115                && this.version == other.get().version &&
116                Objects.equals(this.description, other.get().description)
117                && Objects.equals(this.models, other.get().models)
118                && Objects.equals(this.view, other.get().view);
119        //CHECKSTYLE:ON: checkstyle:simplifybooleanexpression
120    }
121
122    public void checkNamesakes(final Optional<Project> namesake) throws ConflictException {
123        if (namesake.isPresent()) {
124            if (!namesake.get().getId().equals(id)) {
125                throw new ConflictException("Another project with same '%s' already exists.", name);
126            }
127        }
128    }
129
130    public void checkConflictWith(final Optional<Project> prevVersionOfThisProject) throws ConflictException {
131        if (prevVersionOfThisProject.isPresent()) {
132            if (version - 1 != prevVersionOfThisProject.get().version) {
133                throw new ConflictException("Project '%s' has been changed by another user.", name);
134            }
135        }
136    }
137
138    @Override
139    public boolean equals(Object o) {
140        if (this == o) {
141            return true;
142        }
143        if (o == null || getClass() != o.getClass()) {
144            return false;
145        }
146        final Project project = (Project) o;
147        return Objects.equals(version, project.version) && Objects.equals(id, project.id);
148    }
149
150    @Override
151    public int hashCode() {
152        return Objects.hash(id, version);
153    }
154
155    @Override
156    public String toString() {
157        return "Project{" +
158                "id='" + id + '\'' +
159                ", name='" + name + '\'' +
160                ", version=" + version +
161                '}';
162    }
163
164}