001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 the ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package org.conqat.engine.index.shared;
018
019import java.io.Serializable;
020import java.util.Optional;
021
022import javax.annotation.Nullable;
023
024import org.conqat.lib.commons.js_export.ExportToJavaScript;
025import org.conqat.lib.commons.string.StringUtils;
026
027import com.fasterxml.jackson.annotation.JsonCreator;
028import com.fasterxml.jackson.annotation.JsonProperty;
029import com.google.common.base.Preconditions;
030
031/**
032 * Descriptor for a single project.
033 */
034@ExportToJavaScript
035public class ProjectInfo implements Serializable {
036
037        /** Serial version UID. */
038        private static final long serialVersionUID = 1;
039
040        /** The name of the JSON property name for {@link #id}. */
041        private static final String ID_PROPERTY = "id";
042
043        /** The name of the JSON property name for {@link #name}. */
044        private static final String NAME_PROPERTY = "name";
045
046        /** The name of the JSON property name for {@link #alias}. */
047        private static final String ALIAS_PROPERTY = "alias";
048
049        /** The name of the JSON property name for {@link #parentProjectId}. */
050        private static final String PARENT_PROJECT_ID_PROPERTY = "parentProjectId";
051
052        /** The name of the JSON property name for {@link #description}. */
053        private static final String DESCRIPTION_PROPERTY = "description";
054
055        /** The name of the JSON property name for {@link #creationTimestamp}. */
056        private static final String CREATION_TIMESTAMP_PROPERTY = "creationTimestamp";
057
058        /** ID of the project */
059        @JsonProperty(ID_PROPERTY)
060        private final String id;
061
062        /**
063         * An alias ID that can be used to access this project. This may be null if the
064         * project has no alias.
065         */
066        @JsonProperty(ALIAS_PROPERTY)
067        @Nullable
068        private String alias;
069
070        /**
071         * An optional project id of a parent project, which provides mountable stores
072         * shared with this project.
073         */
074        @JsonProperty(PARENT_PROJECT_ID_PROPERTY)
075        @Nullable
076        private String parentProjectId;
077
078        /** The project name. */
079        @JsonProperty(NAME_PROPERTY)
080        private String name;
081
082        /** The description, may be null. */
083        @JsonProperty(DESCRIPTION_PROPERTY)
084        @Nullable
085        private String description;
086
087        /** The creation time of the project. */
088        @JsonProperty(CREATION_TIMESTAMP_PROPERTY)
089        private long creationTimestamp;
090
091        /** Whether this project is in the process of being deleted. */
092        @JsonProperty("deleting")
093        private boolean deleting = false;
094
095        /** Whether this project is in the process of being re-analyzed. */
096        @JsonProperty("reanalyzing")
097        private boolean reanalyzing = false;
098
099        @JsonCreator
100        public ProjectInfo(@JsonProperty(ID_PROPERTY) String id, @JsonProperty(NAME_PROPERTY) String name,
101                        @JsonProperty(ALIAS_PROPERTY) String alias,
102                        @JsonProperty(PARENT_PROJECT_ID_PROPERTY) String parentProjectId,
103                        @JsonProperty(DESCRIPTION_PROPERTY) String description,
104                        @JsonProperty(CREATION_TIMESTAMP_PROPERTY) long creationTimestamp) {
105                this.id = id;
106                this.name = name;
107                this.alias = alias;
108                this.parentProjectId = parentProjectId;
109                this.description = description;
110                this.creationTimestamp = creationTimestamp;
111
112                if (parentProjectId != null) {
113                        Preconditions.checkState(!StringUtils.isEmpty(parentProjectId),
114                                        "Can't have an empty parent project id. Must be either null or a non-empty string.");
115                }
116        }
117
118        /** Returns the project id. */
119        public String getId() {
120                return id;
121        }
122
123        /** Returns {@link #alias}. */
124        public String getAlias() {
125                return alias;
126        }
127
128        /** Sets {@link #alias}. */
129        public void setAlias(String alias) {
130                this.alias = alias;
131        }
132
133        /** @see #parentProjectId */
134        public Optional<String> getParentProjectId() {
135                return Optional.ofNullable(parentProjectId);
136        }
137
138        /**
139         * Returns the {@link #alias} if present. Otherwise the {@link #id} is returned.
140         * Can be used in case the REST API or UI code expects the alias to shadow the
141         * id.
142         */
143        public String getProjectAliasOrId() {
144                if (alias != null) {
145                        return alias;
146                }
147                return id;
148        }
149
150        /** Returns {@link #name}. */
151        public String getName() {
152                return name;
153        }
154
155        /** Sets {@link #name}. */
156        public void setName(String name) {
157                this.name = name;
158        }
159
160        /** Returns {@link #description}. */
161        public String getDescription() {
162                return description;
163        }
164
165        /** Sets {@link #description}. */
166        public void setDescription(String description) {
167                this.description = description;
168        }
169
170        /** Returns {@link #creationTimestamp}. */
171        public long getCreationTimestamp() {
172                return creationTimestamp;
173        }
174
175        /** Sets the creation timestamp. */
176        public void setCreationTimestamp(long creationTimestamp) {
177                this.creationTimestamp = creationTimestamp;
178        }
179
180        /** Returns {@link #deleting}. */
181        public boolean isDeleting() {
182                return deleting;
183        }
184
185        /** @see #isReanalyzing */
186        public boolean isReanalyzing() {
187                return reanalyzing;
188        }
189
190        /**
191         * Returns whether this project is currently in the process of either being
192         * reanalyzed or deleted.
193         */
194        public boolean isDeletingOrReanalyzing() {
195                return isDeleting() || isReanalyzing();
196        }
197
198        /** @see #deleting */
199        public void setDeleting(boolean deleting) {
200                this.deleting = deleting;
201        }
202
203        /** @see #isReanalyzing */
204        public void setReanalyzing(boolean reanalyzing) {
205                this.reanalyzing = reanalyzing;
206        }
207}