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@Bean(typeName="Pet", fluentSetters=true, properties="id,species,name,tags,price,status,photo") 031@Entity(name="PetstorePet") 032public class Pet { 033 034 @Column @Id @GeneratedValue 035 @Schema(description="Unique identifier for this pet.") 036 @Html(link="servlet:/pet/{id}") 037 private long id; 038 039 @Column(length=50) 040 @Schema(description="Pet name.", minLength=3, maxLength=50) 041 private String name; 042 043 @Column 044 @Schema(description="Price of pet.", maximum="999.99") 045 @Html(render=PriceRender.class) 046 private float price; 047 048 @Column 049 @Schema(description="Pet species.") 050 private Species species; 051 052 @ElementCollection @OrderColumn 053 @Schema(description="Pet attributes.", example="friendly,smart") 054 private List<String> tags; 055 056 @Column @Enumerated(STRING) 057 @Schema(description="Pet species.") 058 private PetStatus status; 059 060 @Column 061 @Schema(description="Photo URL.") 062 @URI 063 private String photo; 064 065 public Pet apply(CreatePet x) { 066 this.name = x.getName(); 067 this.price = x.getPrice(); 068 this.species = x.getSpecies(); 069 this.tags = x.getTags() == null ? null : Arrays.asList(x.getTags()); 070 this.photo = x.getPhoto(); 071 return this; 072 } 073 074 public Pet apply(UpdatePet x) { 075 this.id = x.getId(); 076 this.name = x.getName(); 077 this.price = x.getPrice(); 078 this.species = x.getSpecies(); 079 this.tags = Arrays.asList(x.getTags()); 080 this.status = x.getStatus(); 081 this.photo = x.getPhoto(); 082 return this; 083 } 084 085 //----------------------------------------------------------------------------------------------------------------- 086 // Bean properties 087 //----------------------------------------------------------------------------------------------------------------- 088 089 public long getId() { 090 return id; 091 } 092 093 public Pet id(long id) { 094 this.id = id; 095 return this; 096 } 097 098 public String getName() { 099 return name; 100 } 101 102 public Pet name(String name) { 103 this.name = name; 104 return this; 105 } 106 107 public float getPrice() { 108 return price; 109 } 110 111 public Pet price(float price) { 112 this.price = price; 113 return this; 114 } 115 116 public Species getSpecies() { 117 return species; 118 } 119 120 public Pet species(Species species) { 121 this.species = species; 122 return this; 123 } 124 125 public List<String> getTags() { 126 return tags; 127 } 128 129 public Pet tags(List<String> tags) { 130 this.tags = tags; 131 return this; 132 } 133 134 public Pet tags(String...tags) { 135 this.tags = Arrays.asList(tags); 136 return this; 137 } 138 139 public PetStatus getStatus() { 140 return status; 141 } 142 143 public Pet status(PetStatus status) { 144 this.status = status; 145 return this; 146 } 147 148 public String getPhoto() { 149 return photo; 150 } 151 152 public Pet photo(String photo) { 153 this.photo = photo; 154 return this; 155 } 156 157 public boolean hasStatus(PetStatus...statuses) { 158 for (PetStatus status : statuses) 159 if (this.status == status) 160 return true; 161 return false; 162 } 163 164 public boolean hasTag(String...tags) { 165 for (String tag : tags) 166 for (String t : this.tags) 167 if (t.equals(tag)) 168 return true; 169 return false; 170 } 171 172 public java.net.URI getEdit() { 173 return java.net.URI.create("servlet:/pet/edit/{id}"); 174 } 175 176 //----------------------------------------------------------------------------------------------------------------- 177 // Other 178 //----------------------------------------------------------------------------------------------------------------- 179 180 /** 181 * This shows an example generated from a static method. 182 */ 183 @Example 184 public static Pet example() { 185 return new Pet() 186 .id(123) 187 .species(Species.DOG) 188 .name("Doggie") 189 .tags("friendly","smart") 190 .status(PetStatus.AVAILABLE); 191 } 192 193 194 public static final class PriceRender extends HtmlRender<Float> { 195 @Override 196 public Object getContent(SerializerSession session, Float value) { 197 return value == null ? null : String.format("$%.2f", value); 198 } 199 } 200}