eb601af4c52a45f9cfefaf72e3f67375cdfffcf3
[chesshog.git] / chesshog-db-etude / src / main / java / org / hedgecode / chess / domain / Etude.java
1 /*
2  * Copyright (c) 2018. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.chess.domain;
18
19 import java.math.BigInteger;
20 import java.util.Date;
21
22 import javax.persistence.Column;
23 import javax.persistence.Entity;
24 import javax.persistence.FetchType;
25 import javax.persistence.GeneratedValue;
26 import javax.persistence.GenerationType;
27 import javax.persistence.Id;
28 import javax.persistence.JoinColumn;
29 import javax.persistence.Lob;
30 import javax.persistence.ManyToOne;
31 import javax.persistence.NamedQueries;
32 import javax.persistence.NamedQuery;
33 import javax.persistence.Table;
34 import javax.persistence.Temporal;
35 import javax.persistence.TemporalType;
36 import javax.persistence.Transient;
37
38 import org.hedgecode.chess.position.Position;
39
40 /**
41  *
42  *
43  * @author Dmitry Samoshin aka gotty
44  */
45 @Entity
46 @Table(
47         name = "etudes"
48 )
49 @NamedQueries({
50         @NamedQuery(
51                 name = Etude.FIND_ALL,
52                 query = "select e from Etude e"
53         ),
54         @NamedQuery(
55                 name = Etude.FIND_BY_ID,
56                 query = "select e from Etude e where e.id = :id"
57         ),
58         @NamedQuery(
59                 name = Etude.FIND_BY_HASH,
60                 query = "select e from Etude e where e.hash = :hash"
61         ),
62         @NamedQuery(
63                 name = Etude.FIND_BY_TYPE,
64                 query = "select e from Etude e where e.etudeType = :etudeType"
65         ),
66         @NamedQuery(
67                 name = Etude.FIND_BY_AUTHOR,
68                 query = "select e from Etude e where e.author = :author"
69         ),
70         @NamedQuery(
71                 name = Etude.FIND_BY_BLOB,
72                 query = "select e from Etude e where e.blob = :blob"
73         )
74 })
75 public class Etude extends DomainObject<Etude> {
76
77     public static final String FIND_ALL = "Etude.findAll";
78     public static final String FIND_BY_ID = "Etude.findById";
79     public static final String FIND_BY_HASH = "Etude.findByHash";
80     public static final String FIND_BY_TYPE = "Etude.findByType";
81     public static final String FIND_BY_AUTHOR = "Etude.findByAuthor";
82     public static final String FIND_BY_BLOB = "Etude.findByBlob";
83
84     public static final String HASH_PARAMETER = "hash";
85     public static final String TYPE_PARAMETER = "type";
86     public static final String AUTHOR_PARAMETER = "author";
87     public static final String BLOB_PARAMETER = "blob";
88
89     @Id
90     @Column(
91             name = "f_id"
92     )
93     @GeneratedValue(
94             strategy = GenerationType.AUTO
95     )
96     private Long id;
97
98     @Column(
99             name = "f_hash", nullable = false
100     )
101     private BigInteger hash;
102
103     @Column(
104             name = "f_name"
105     )
106     private String name;
107
108     @ManyToOne(
109             fetch = FetchType.LAZY
110     )
111     @JoinColumn(
112             name = "f_type", nullable = false
113     )
114     private EtudeType etudeType;
115
116     @ManyToOne(
117             fetch = FetchType.LAZY
118     )
119     @JoinColumn(
120             name = "f_author"
121     )
122     private Author author;
123
124     @Column(
125             name = "f_author_name"
126     )
127     private String authorName;
128
129     @Column(
130             name = "f_fen", nullable = false
131     )
132     private String fen;
133
134     @Column(
135             name = "f_pgn"
136     )
137     private String pgn;
138
139     @Lob
140     @Column(
141             name = "f_blob", length = 32, nullable = false
142     )
143     private byte[] blob;
144
145     @Column(
146             name = "f_date"
147     )
148     @Temporal(
149             value = TemporalType.DATE
150     )
151     private Date date;
152
153     @Column(
154             name = "f_description"
155     )
156     private String description;
157
158
159
160     @Transient
161     private Position position;
162
163     public Etude() {
164     }
165
166     public Etude(Position position) {
167         this.position = position;
168     }
169
170     public <TargetType> TargetType assemble(TargetType target, EtudeAdapter<TargetType> adapter) {
171         adapter.setId(target, id);
172         adapter.setHash(target, hash);
173         adapter.setEtudeType(target, etudeType);
174         adapter.setAuthor(target, author);
175         adapter.setAuthorName(target, authorName);
176         adapter.setFen(target, fen);
177         adapter.setPgn(target, pgn);
178         adapter.setBlob(target, blob);
179         adapter.setDate(target, date);
180         adapter.setDescription(target, description);
181
182         return target;
183     }
184
185
186
187
188     private void assignFromPosition() {
189
190     }
191
192     private void assignFromPGN() {
193
194     }
195
196     public Position getPosition() {
197         return position;
198     }
199
200     @Override
201     public Long getId() {
202         return id;
203     }
204
205 }