View Javadoc

1   /*
2    * Copyright (c) 2013 Maxim Yunusov
3    *    Licensed under the Apache License, Version 2.0 (the "License");
4    *    you may not use this file except in compliance with the License.
5    *    You may obtain a copy of the License at
6    *
7    *        http://www.apache.org/licenses/LICENSE-2.0
8    *
9    *    Unless required by applicable law or agreed to in writing, software
10   *    distributed under the License is distributed on an "AS IS" BASIS,
11   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   *    See the License for the specific language governing permissions and
13   *    limitations under the License.
14   */
15  
16  package org.maxur.perfmodel.backend.domain;
17  
18  import java.io.Serializable;
19  import java.util.Objects;
20  import java.util.Optional;
21  
22  /**
23   * @author Maxim Yunusov
24   * @version 1.0 24.11.13
25   */
26  public class Project implements Serializable {
27  
28      private static final long serialVersionUID = -8859706675434765594L;
29  
30      private String id;
31  
32      private String name;
33  
34      private int version;
35  
36      private String description;
37  
38      private String models;
39  
40      private String view;
41  
42      @SuppressWarnings("unused")
43      public Project() {
44      }
45  
46      public Project(final String id, final String name, final int version, final String description) {
47          this.id = id;
48          this.name = name;
49          this.version = version;
50          this.description = description;
51      }
52  /*
53  
54      public Project cloneWith(final String rawData) {
55          final Project result = new Project(this.id, this.name, this.version);
56          result.raw = rawData;
57          return result;
58      }*/
59  
60      public Project brief() {
61          return new Project(this.id, this.name, this.version, this.description);
62      }
63  
64      public String getName() {
65          return name;
66      }
67  
68      public String getId() {
69          return id;
70      }
71  
72      public int getVersion() {
73          return version;
74      }
75  
76      public String getDescription() {
77          return description;
78      }
79  
80      public String getModels() {
81          return models;
82      }
83  
84      public String getView() {
85          return view;
86      }
87  
88      public void setModels(final String models) {
89          this.models = models;
90      }
91  
92      public void setView(final String view) {
93          this.view = view;
94      }
95  
96      public void incVersion() {
97          this.version++;
98      }
99  
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 }