001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  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 distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.examples.rest.petstore.dto;
014
015import static javax.persistence.EnumType.*;
016
017import java.util.*;
018
019import javax.persistence.*;
020
021import org.apache.juneau.annotation.*;
022import org.apache.juneau.html.*;
023import org.apache.juneau.html.annotation.*;
024import org.apache.juneau.jsonschema.annotation.*;
025import org.apache.juneau.serializer.*;
026
027/**
028 * Pet bean.
029 *
030 * <ul class='seealso'>
031 *    <li class='extlink'>{@source}
032 * </ul>
033 */
034@Bean(typeName="Pet", fluentSetters=true, properties="id,species,name,tags,price,status,photo")
035@Entity(name="PetstorePet")
036public class Pet {
037
038   @Column @Id @GeneratedValue
039   @Schema(description="Unique identifier for this pet.")
040   @Html(link="servlet:/pet/{id}")
041   private long id;
042
043   @Column(length=50)
044   @Schema(description="Pet name.", minLength=3, maxLength=50)
045   private String name;
046
047   @Column
048   @Schema(description="Price of pet.", maximum="999.99")
049   @Html(render=PriceRender.class)
050   private float price;
051
052   @Column
053   @Schema(description="Pet species.")
054   private Species species;
055
056   @ElementCollection @OrderColumn
057   @Schema(description="Pet attributes.", example="friendly,smart")
058   private List<String> tags;
059
060   @Column @Enumerated(STRING)
061   @Schema(description="Pet species.")
062   private PetStatus status;
063
064   @Column
065   @Schema(description="Photo URL.")
066   @URI
067   private String photo;
068
069   /**
070    * Applies the specified data to this object.
071    *
072    * @param x The data to apply.
073    * @return This object.
074    */
075   public Pet apply(CreatePet x) {
076      this.name = x.getName();
077      this.price = x.getPrice();
078      this.species = x.getSpecies();
079      this.tags = x.getTags() == null ? null : Arrays.asList(x.getTags());
080      this.photo = x.getPhoto();
081      return this;
082   }
083
084   /**
085    * Applies the specified data to this object.
086    *
087    * @param x The data to apply.
088    * @return This object.
089    */
090   public Pet apply(UpdatePet x) {
091      this.id = x.getId();
092      this.name = x.getName();
093      this.price = x.getPrice();
094      this.species = x.getSpecies();
095      this.tags = Arrays.asList(x.getTags());
096      this.status = x.getStatus();
097      this.photo = x.getPhoto();
098      return this;
099   }
100
101   //-----------------------------------------------------------------------------------------------------------------
102   // Bean properties
103   //-----------------------------------------------------------------------------------------------------------------
104
105   /**
106    * @return The <bc>id</jc> property value.
107    */
108   public long getId() {
109      return id;
110   }
111
112   /**
113    * @param value The <bc>id</jc> property value.
114    * @return This object (for method chaining).
115    */
116   public Pet id(long value) {
117      this.id = value;
118      return this;
119   }
120
121   /**
122    * @return The <bc>name</jc> property value.
123    */
124   public String getName() {
125      return name;
126   }
127
128   /**
129    * @param value The <bc>name</jc> property value.
130    * @return This object (for method chaining).
131    */
132   public Pet name(String value) {
133      this.name = value;
134      return this;
135   }
136
137   /**
138    * @return The <bc>price</jc> property value.
139    */
140   public float getPrice() {
141      return price;
142   }
143
144   /**
145    * @param value The <bc>price</jc> property value.
146    * @return This object (for method chaining).
147    */
148   public Pet price(float value) {
149      this.price = value;
150      return this;
151   }
152
153   /**
154    * @return The <bc>species</jc> property value.
155    */
156   public Species getSpecies() {
157      return species;
158   }
159
160   /**
161    * @param value The <bc>species</jc> property value.
162    * @return This object (for method chaining).
163    */
164   public Pet species(Species value) {
165      this.species = value;
166      return this;
167   }
168
169   /**
170    * @return The <bc>tags</jc> property value.
171    */
172   public List<String> getTags() {
173      return tags;
174   }
175
176   /**
177    * @param value The <bc>tags</jc> property value.
178    * @return This object (for method chaining).
179    */
180   public Pet tags(List<String> value) {
181      this.tags = value;
182      return this;
183   }
184
185   /**
186    * @param value The <bc>tags</jc> property value.
187    * @return This object (for method chaining).
188    */
189   public Pet tags(String...value) {
190      this.tags = Arrays.asList(value);
191      return this;
192   }
193
194   /**
195    * @return The <bc>status</jc> property value.
196    */
197   public PetStatus getStatus() {
198      return status;
199   }
200
201   /**
202    * @param value The <bc>status</jc> property value.
203    * @return This object (for method chaining).
204    */
205   public Pet status(PetStatus value) {
206      this.status = value;
207      return this;
208   }
209
210   /**
211    * @return The <bc>photo</jc> property value.
212    */
213   public String getPhoto() {
214      return photo;
215   }
216
217   /**
218    * @param value The <bc>photo</jc> property value.
219    * @return This object (for method chaining).
220    */
221   public Pet photo(String value) {
222      this.photo = value;
223      return this;
224   }
225
226   /**
227    * @param statuses The statuses to match against.
228    * @return <jk>true</jk> if this pet matches at least one of the specified statuses.
229    */
230   public boolean hasStatus(PetStatus...statuses) {
231      for (PetStatus status : statuses)
232         if (this.status == status)
233            return true;
234      return false;
235   }
236
237   /**
238    * @param tags The tags to match against.
239    * @return <jk>true</jk> if this pet matches at least one of the specified tags.
240    */
241   public boolean hasTag(String...tags) {
242      for (String tag : tags)
243         for (String t : this.tags)
244            if (t.equals(tag))
245               return true;
246      return false;
247   }
248
249   /**
250    * @return Edit page link.
251    */
252   public java.net.URI getEdit() {
253      return java.net.URI.create("servlet:/pet/edit/{id}");
254   }
255
256   //-----------------------------------------------------------------------------------------------------------------
257   // Other
258   //-----------------------------------------------------------------------------------------------------------------
259
260   /**
261    * This shows an example generated from a static method.
262    *
263    * @return POJO example.
264    */
265   @Example
266   public static Pet example() {
267      return new Pet()
268         .id(123)
269         .species(Species.DOG)
270         .name("Doggie")
271         .tags("friendly","smart")
272         .status(PetStatus.AVAILABLE);
273   }
274
275   /**
276    * Used to control format of prices in HTML view.
277    */
278   public static final class PriceRender extends HtmlRender<Float> {
279      @Override
280      public Object getContent(SerializerSession session, Float value) {
281         return value == null ? null : String.format("$%.2f", value);
282      }
283   }
284}