[LIB-9] Add original chesshog-db-etude source files
authorgotty <gotty@hedgecode.org>
Sat, 15 Dec 2018 22:16:34 +0000 (01:16 +0300)
committergotty <gotty@hedgecode.org>
Sat, 15 Dec 2018 22:16:34 +0000 (01:16 +0300)
24 files changed:
chesshog-db-etude/pom.xml [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/Author.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/AuthorAdapter.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/DomainObject.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/Etude.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/EtudeAdapter.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/EtudeType.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/EtudeTypeAdapter.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/AuthorDTO.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/AuthorDTOAdapter.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/EtudeDTO.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/EtudeDTOAdapter.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/EtudeTypeDTO.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/EtudeTypeDTOAdapter.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/service/AuthorService.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/service/DomainService.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/service/EtudeService.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/service/EtudeTypeService.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/service/EtudeUtils.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/service/jpa/JpaAuthorService.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/service/jpa/JpaDomainService.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/service/jpa/JpaEtudeService.java [new file with mode: 0644]
chesshog-db-etude/src/main/java/org/hedgecode/chess/service/jpa/JpaEtudeTypeService.java [new file with mode: 0644]
chesshog-db-etude/src/main/resources/META-INF/persistence.xml [new file with mode: 0644]

diff --git a/chesshog-db-etude/pom.xml b/chesshog-db-etude/pom.xml
new file mode 100644 (file)
index 0000000..ea4b414
--- /dev/null
@@ -0,0 +1,79 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+  ~ Copyright (c) 2018. Developed by Hedgecode.
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~   http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.hedgecode.chess</groupId>
+        <artifactId>chesshog</artifactId>
+        <version>0.1-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>chesshog-db-etude</artifactId>
+    <version>0.1-SNAPSHOT</version>
+    <packaging>jar</packaging>
+
+    <name>Hedgecode ChessHog DB Engine Module</name>
+    <description>
+        Hedgecode ChessHog DB Engine Module.
+    </description>
+
+    <properties>
+        <chessHogVersion>0.1-SNAPSHOT</chessHogVersion>
+        <hibernateCoreVersion>5.3.7.Final</hibernateCoreVersion>
+        <hibernateJpaApiVersion>1.0.1.Final</hibernateJpaApiVersion>
+        <mysqlVersion>8.0.13</mysqlVersion>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.hedgecode.chess</groupId>
+            <artifactId>chesshog</artifactId>
+            <version>${chessHogVersion}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.hedgecode.chess</groupId>
+            <artifactId>chesshog-core</artifactId>
+            <version>${chessHogVersion}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.hibernate</groupId>
+            <artifactId>hibernate-core</artifactId>
+            <version>${hibernateCoreVersion}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.hibernate</groupId>
+            <artifactId>hibernate-entitymanager</artifactId>
+            <version>${hibernateCoreVersion}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.hibernate.javax.persistence</groupId>
+            <artifactId>hibernate-jpa-2.0-api</artifactId>
+            <version>${hibernateJpaApiVersion}</version>
+        </dependency>
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <version>${mysqlVersion}</version>
+        </dependency>
+    </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/Author.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/Author.java
new file mode 100644 (file)
index 0000000..7aab668
--- /dev/null
@@ -0,0 +1,120 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.domain;
+
+import java.util.Date;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.Table;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+@Entity
+@Table(
+        name = "authors"
+)
+@NamedQueries({
+        @NamedQuery(
+                name = Author.FIND_ALL,
+                query = "select a from Author a"
+        ),
+        @NamedQuery(
+                name = Author.FIND_BY_ID,
+                query = "select a from Author a where a.id = :id"
+        ),
+        @NamedQuery(
+                name = Author.FIND_BY_NAME,
+                query = "select a from Author a where a.name like :name"
+        ),
+        @NamedQuery(
+                name = Author.FIND_BY_BIRTHDATE,
+                query = "select a from Author a where a.birthdate = :birthdate"
+        )
+})
+public class Author extends DomainObject {
+
+    public static final String FIND_ALL = "Author.findAll";
+    public static final String FIND_BY_ID = "Author.findById";
+    public static final String FIND_BY_NAME = "Author.findByName";
+    public static final String FIND_BY_BIRTHDATE = "Author.findByBirthDate";
+
+    public static final String NAME_PROPERTY = "name";
+    public static final String BIRTHDATE_PROPERTY = "birthdate";
+
+    @Id
+    @Column(
+            name = "f_id"
+    )
+    @GeneratedValue(
+            strategy = GenerationType.AUTO
+    )
+    private Long id;
+
+    @Column(
+            name = "f_name", nullable = false
+    )
+    private String name;
+
+    @Column(
+            name = "f_birthdate"
+    )
+    @Temporal(
+            value = TemporalType.DATE
+    )
+    private Date birthdate;
+
+    @Column(
+            name = "f_deathdate"
+    )
+    @Temporal(
+            value = TemporalType.DATE
+    )
+    private Date deathdate;
+
+    @Column(
+            name = "f_biography"
+    )
+    private String biography;
+
+    @Column(
+            name = "f_comment"
+    )
+    private String comment;
+
+    public <TargetType> TargetType assemble(TargetType target, AuthorAdapter<TargetType> adapter) {
+        adapter.setId(target, id);
+        adapter.setName(target, name);
+        adapter.setBirthdate(target, birthdate);
+        adapter.setDeathdate(target, deathdate);
+        adapter.setBiography(target, biography);
+        adapter.setComment(target, comment);
+
+        return target;
+    }
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/AuthorAdapter.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/AuthorAdapter.java
new file mode 100644 (file)
index 0000000..96bb009
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.domain;
+
+import java.util.Date;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public interface AuthorAdapter<TargetType> {
+
+    void setId(TargetType target, Long id);
+    void setName(TargetType target, String name);
+    void setBirthdate(TargetType target, Date birthdate);
+    void setDeathdate(TargetType target, Date deathdate);
+    void setBiography(TargetType target, String biography);
+    void setComment(TargetType target, String comment);
+
+    class Empty<TargetType> implements AuthorAdapter<TargetType> {
+
+        @Override
+        public void setId(TargetType target, Long id) {
+        }
+
+        @Override
+        public void setName(TargetType target, String name) {
+        }
+
+        @Override
+        public void setBirthdate(TargetType target, Date birthdate) {
+        }
+
+        @Override
+        public void setDeathdate(TargetType target, Date deathdate) {
+        }
+
+        @Override
+        public void setBiography(TargetType target, String biography) {
+        }
+
+        @Override
+        public void setComment(TargetType target, String comment) {
+        }
+    }
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/DomainObject.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/DomainObject.java
new file mode 100644 (file)
index 0000000..923e4f5
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.domain;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public abstract class DomainObject {
+
+    public static final String ID_PROPERTY = "id";
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/Etude.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/Etude.java
new file mode 100644 (file)
index 0000000..eccbd44
--- /dev/null
@@ -0,0 +1,200 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.domain;
+
+import java.math.BigInteger;
+import java.util.Date;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.Lob;
+import javax.persistence.ManyToOne;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.Table;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+import javax.persistence.Transient;
+
+import org.hedgecode.chess.position.Position;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+@Entity
+@Table(
+        name = "etudes"
+)
+@NamedQueries({
+        @NamedQuery(
+                name = Etude.FIND_ALL,
+                query = "select e from Etude e"
+        ),
+        @NamedQuery(
+                name = Etude.FIND_BY_ID,
+                query = "select e from Etude e where e.id = :id"
+        ),
+        @NamedQuery(
+                name = Etude.FIND_BY_HASH,
+                query = "select e from Etude e where e.hash = :hash"
+        ),
+        @NamedQuery(
+                name = Etude.FIND_BY_TYPE,
+                query = "select e from Etude e where e.etudeType = :etudeType"
+        ),
+        @NamedQuery(
+                name = Etude.FIND_BY_AUTHOR,
+                query = "select e from Etude e where e.author = :author"
+        ),
+        @NamedQuery(
+                name = Etude.FIND_BY_BLOB,
+                query = "select e from Etude e where e.blob = :blob"
+        )
+})
+public class Etude extends DomainObject {
+
+    public static final String FIND_ALL = "Etude.findAll";
+    public static final String FIND_BY_ID = "Etude.findById";
+    public static final String FIND_BY_HASH = "Etude.findByHash";
+    public static final String FIND_BY_TYPE = "Etude.findByType";
+    public static final String FIND_BY_AUTHOR = "Etude.findByAuthor";
+    public static final String FIND_BY_BLOB = "Etude.findByBlob";
+
+    public static final String HASH_PROPERTY = "hash";
+    public static final String TYPE_PROPERTY = "type";
+    public static final String AUTHOR_PROPERTY = "author";
+    public static final String BLOB_PROPERTY = "blob";
+
+    @Id
+    @Column(
+            name = "f_id"
+    )
+    @GeneratedValue(
+            strategy = GenerationType.AUTO
+    )
+    private Long id;
+
+    @Column(
+            name = "f_hash", nullable = false
+    )
+    private BigInteger hash;
+
+    @Column(
+            name = "f_name"
+    )
+    private String name;
+
+    @ManyToOne(
+            fetch = FetchType.LAZY
+    )
+    @JoinColumn(
+            name = "f_type", nullable = false
+    )
+    private EtudeType etudeType;
+
+    @ManyToOne(
+            fetch = FetchType.LAZY
+    )
+    @JoinColumn(
+            name = "f_author"
+    )
+    private Author author;
+
+    @Column(
+            name = "f_author_name"
+    )
+    private String authorName;
+
+    @Column(
+            name = "f_fen", nullable = false
+    )
+    private String fen;
+
+    @Column(
+            name = "f_pgn"
+    )
+    private String pgn;
+
+    @Lob
+    @Column(
+            name = "f_blob", length = 32, nullable = false
+    )
+    private byte[] blob;
+
+    @Column(
+            name = "f_date"
+    )
+    @Temporal(
+            value = TemporalType.DATE
+    )
+    private Date date;
+
+    @Column(
+            name = "f_description"
+    )
+    private String description;
+
+
+
+    @Transient
+    private Position position;
+
+    public Etude() {
+    }
+
+    public Etude(Position position) {
+        this.position = position;
+    }
+
+    public <TargetType> TargetType assemble(TargetType target, EtudeAdapter<TargetType> adapter) {
+        adapter.setId(target, id);
+        adapter.setHash(target, hash);
+        adapter.setEtudeType(target, etudeType);
+        adapter.setAuthor(target, author);
+        adapter.setAuthorName(target, authorName);
+        adapter.setFen(target, fen);
+        adapter.setPgn(target, pgn);
+        adapter.setBlob(target, blob);
+        adapter.setDate(target, date);
+        adapter.setDescription(target, description);
+
+        return target;
+    }
+
+
+
+
+    private void assignFromPosition() {
+
+    }
+
+    private void assignFromPGN() {
+
+    }
+
+    public Position getPosition() {
+        return position;
+    }
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/EtudeAdapter.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/EtudeAdapter.java
new file mode 100644 (file)
index 0000000..c1dea0b
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.domain;
+
+import java.math.BigInteger;
+import java.util.Date;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public interface EtudeAdapter<TargetType> {
+
+    void setId(TargetType target, Long id);
+    void setHash(TargetType target, BigInteger hash);
+    void setName(TargetType target, String name);
+    void setEtudeType(TargetType target, EtudeType etudeType);
+    void setAuthor(TargetType target, Author author);
+    void setAuthorName(TargetType target, String authorName);
+    void setFen(TargetType target, String fen);
+    void setPgn(TargetType target, String pgn);
+    void setBlob(TargetType target, byte[] blob);
+    void setDate(TargetType target, Date date);
+    void setDescription(TargetType target, String description);
+
+    class Empty<TargetType> implements EtudeAdapter<TargetType> {
+
+        @Override
+        public void setId(TargetType target, Long id) {
+        }
+
+        @Override
+        public void setHash(TargetType target, BigInteger hash) {
+        }
+
+        @Override
+        public void setName(TargetType target, String name) {
+        }
+
+        @Override
+        public void setEtudeType(TargetType target, EtudeType etudeType) {
+        }
+
+        @Override
+        public void setAuthor(TargetType target, Author author) {
+        }
+
+        @Override
+        public void setAuthorName(TargetType target, String authorName) {
+        }
+
+        @Override
+        public void setFen(TargetType target, String fen) {
+        }
+
+        @Override
+        public void setPgn(TargetType target, String pgn) {
+        }
+
+        @Override
+        public void setBlob(TargetType target, byte[] blob) {
+        }
+
+        @Override
+        public void setDate(TargetType target, Date date) {
+        }
+
+        @Override
+        public void setDescription(TargetType target, String description) {
+        }
+    }
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/EtudeType.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/EtudeType.java
new file mode 100644 (file)
index 0000000..a9de8aa
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.domain;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.Table;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+@Entity
+@Table(
+        name = "etude_types"
+)
+@NamedQueries({
+        @NamedQuery(
+                name = EtudeType.FIND_ALL,
+                query = "select et from EtudeType et"
+        ),
+        @NamedQuery(
+                name = EtudeType.FIND_BY_ID,
+                query = "select et from EtudeType et where et.id = :id"
+        ),
+        @NamedQuery(
+                name = EtudeType.FIND_BY_BRIEF,
+                query = "select et from EtudeType et where et.brief = :brief"
+        )
+})
+public class EtudeType extends DomainObject {
+
+    public static final String FIND_ALL = "EtudeType.findAll";
+    public static final String FIND_BY_ID = "EtudeType.findById";
+    public static final String FIND_BY_BRIEF = "EtudeType.findByBrief";
+
+    public static final String BRIEF_PROPERTY = "brief";
+
+    @Id
+    @Column(
+            name = "f_id"
+    )
+    @GeneratedValue(
+            strategy = GenerationType.AUTO
+    )
+    private Long id;
+
+    @Column(
+            name = "f_brief", nullable = false
+    )
+    private String brief;
+
+    @Column(
+            name = "f_name", nullable = false
+    )
+    private String name;
+
+    @Column(
+            name = "f_description"
+    )
+    private String description;
+
+    public <TargetType> TargetType assemble(TargetType target, EtudeTypeAdapter<TargetType> adapter) {
+        adapter.setId(target, id);
+        adapter.setBrief(target, brief);
+        adapter.setName(target, name);
+        adapter.setDescription(target, description);
+
+        return target;
+    }
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/EtudeTypeAdapter.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/domain/EtudeTypeAdapter.java
new file mode 100644 (file)
index 0000000..abc827f
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.domain;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public interface EtudeTypeAdapter<TargetType> {
+
+    void setId(TargetType target, Long id);
+    void setBrief(TargetType target, String brief);
+    void setName(TargetType target, String name);
+    void setDescription(TargetType target, String description);
+
+    class Empty<TargetType> implements EtudeTypeAdapter<TargetType> {
+
+        @Override
+        public void setId(TargetType target, Long id) {
+        }
+
+        @Override
+        public void setBrief(TargetType target, String brief) {
+        }
+
+        @Override
+        public void setName(TargetType target, String name) {
+        }
+
+        @Override
+        public void setDescription(TargetType target, String description) {
+        }
+    }
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/AuthorDTO.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/AuthorDTO.java
new file mode 100644 (file)
index 0000000..eb9b57e
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.dto;
+
+import java.util.Date;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class AuthorDTO {
+
+    /**
+     *
+     */
+    private Long id;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    /**
+     *
+     */
+    private String name;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     *
+     */
+    private Date birthdate;
+
+    public Date getBirthdate() {
+        return birthdate;
+    }
+
+    public void setBirthdate(Date birthdate) {
+        this.birthdate = birthdate;
+    }
+
+    /**
+     *
+     */
+    private Date deathdate;
+
+    public Date getDeathdate() {
+        return deathdate;
+    }
+
+    public void setDeathdate(Date deathdate) {
+        this.deathdate = deathdate;
+    }
+
+    /**
+     *
+     */
+    private String biography;
+
+    public String getBiography() {
+        return biography;
+    }
+
+    public void setBiography(String biography) {
+        this.biography = biography;
+    }
+
+    /**
+     *
+     */
+    private String comment;
+
+    public String getComment() {
+        return comment;
+    }
+
+    public void setComment(String comment) {
+        this.comment = comment;
+    }
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/AuthorDTOAdapter.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/AuthorDTOAdapter.java
new file mode 100644 (file)
index 0000000..ecda84a
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.dto;
+
+import java.util.Date;
+
+import org.hedgecode.chess.domain.AuthorAdapter;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class AuthorDTOAdapter implements AuthorAdapter<AuthorDTO> {
+
+    @Override
+    public void setId(AuthorDTO target, Long id) {
+        target.setId(id);
+    }
+
+    @Override
+    public void setName(AuthorDTO target, String name) {
+        target.setName(name);
+    }
+
+    @Override
+    public void setBirthdate(AuthorDTO target, Date birthdate) {
+        target.setBirthdate(birthdate);
+    }
+
+    @Override
+    public void setDeathdate(AuthorDTO target, Date deathdate) {
+        target.setDeathdate(deathdate);
+    }
+
+    @Override
+    public void setBiography(AuthorDTO target, String biography) {
+        target.setBiography(biography);
+    }
+
+    @Override
+    public void setComment(AuthorDTO target, String comment) {
+        target.setComment(comment);
+    }
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/EtudeDTO.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/EtudeDTO.java
new file mode 100644 (file)
index 0000000..cea3653
--- /dev/null
@@ -0,0 +1,172 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.dto;
+
+import java.math.BigInteger;
+import java.util.Date;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class EtudeDTO {
+
+    /**
+     *
+     */
+    private Long id;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    /**
+     *
+     */
+    private BigInteger hash;
+
+    public BigInteger getHash() {
+        return hash;
+    }
+
+    public void setHash(BigInteger hash) {
+        this.hash = hash;
+    }
+
+    /**
+     *
+     */
+    private String name;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     *
+     */
+    private Long etudeTypeId;
+
+    public Long getEtudeTypeId() {
+        return etudeTypeId;
+    }
+
+    public void setEtudeTypeId(Long etudeTypeId) {
+        this.etudeTypeId = etudeTypeId;
+    }
+
+    /**
+     *
+     */
+    private Long authorId;
+
+    public Long getAuthorId() {
+        return authorId;
+    }
+
+    public void setAuthorId(Long authorId) {
+        this.authorId = authorId;
+    }
+
+    /**
+     *
+     */
+    private String authorName;
+
+    public String getAuthorName() {
+        return authorName;
+    }
+
+    public void setAuthorName(String authorName) {
+        this.authorName = authorName;
+    }
+
+    /**
+     *
+     */
+    private String fen;
+
+    public String getFen() {
+        return fen;
+    }
+
+    public void setFen(String fen) {
+        this.fen = fen;
+    }
+
+    /**
+     *
+     */
+    private String pgn;
+
+    public String getPgn() {
+        return pgn;
+    }
+
+    public void setPgn(String pgn) {
+        this.pgn = pgn;
+    }
+
+    /**
+     *
+     */
+    private byte[] blob;
+
+    public byte[] getBlob() {
+        return blob;
+    }
+
+    public void setBlob(byte[] blob) {
+        this.blob = blob;
+    }
+
+    /**
+     *
+     */
+    private Date date;
+
+    public Date getDate() {
+        return date;
+    }
+
+    public void setDate(Date date) {
+        this.date = date;
+    }
+
+    /**
+     *
+     */
+    private String description;
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/EtudeDTOAdapter.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/EtudeDTOAdapter.java
new file mode 100644 (file)
index 0000000..a9196bf
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.dto;
+
+import java.math.BigInteger;
+import java.util.Date;
+
+import org.hedgecode.chess.domain.Author;
+import org.hedgecode.chess.domain.EtudeAdapter;
+import org.hedgecode.chess.domain.EtudeType;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class EtudeDTOAdapter implements EtudeAdapter<EtudeDTO> {
+
+    @Override
+    public void setId(EtudeDTO target, Long id) {
+        target.setId(id);
+    }
+
+    @Override
+    public void setHash(EtudeDTO target, BigInteger hash) {
+        target.setHash(hash);
+    }
+
+    @Override
+    public void setName(EtudeDTO target, String name) {
+        target.setName(name);
+    }
+
+    @Override
+    public void setEtudeType(EtudeDTO target, EtudeType etudeType) {
+/* todo
+        target.setEtudeTypeId(etudeType.getId());
+*/
+    }
+
+    @Override
+    public void setAuthor(EtudeDTO target, Author author) {
+/* todo
+        target.setAuthorId(author.getId());
+*/
+    }
+
+    @Override
+    public void setAuthorName(EtudeDTO target, String authorName) {
+        target.setAuthorName(authorName);
+    }
+
+    @Override
+    public void setFen(EtudeDTO target, String fen) {
+        target.setFen(fen);
+    }
+
+    @Override
+    public void setPgn(EtudeDTO target, String pgn) {
+        target.setPgn(pgn);
+    }
+
+    @Override
+    public void setBlob(EtudeDTO target, byte[] blob) {
+        target.setBlob(blob);
+    }
+
+    @Override
+    public void setDate(EtudeDTO target, Date date) {
+        target.setDate(date);
+    }
+
+    @Override
+    public void setDescription(EtudeDTO target, String description) {
+        target.setDescription(description);
+    }
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/EtudeTypeDTO.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/EtudeTypeDTO.java
new file mode 100644 (file)
index 0000000..61dee73
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.dto;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class EtudeTypeDTO {
+
+    /**
+     *
+     */
+    private Long id;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    /**
+     *
+     */
+    private String brief;
+
+    public String getBrief() {
+        return brief;
+    }
+
+    public void setBrief(String brief) {
+        this.brief = brief;
+    }
+
+    /**
+     *
+     */
+    private String name;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     *
+     */
+    private String description;
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/EtudeTypeDTOAdapter.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/dto/EtudeTypeDTOAdapter.java
new file mode 100644 (file)
index 0000000..ca032ff
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.dto;
+
+import org.hedgecode.chess.domain.EtudeTypeAdapter;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class EtudeTypeDTOAdapter implements EtudeTypeAdapter<EtudeTypeDTO> {
+
+    @Override
+    public void setId(EtudeTypeDTO target, Long id) {
+        target.setId(id);
+    }
+
+    @Override
+    public void setBrief(EtudeTypeDTO target, String brief) {
+        target.setBrief(brief);
+    }
+
+    @Override
+    public void setName(EtudeTypeDTO target, String name) {
+        target.setName(name);
+    }
+
+    @Override
+    public void setDescription(EtudeTypeDTO target, String description) {
+        target.setDescription(description);
+    }
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/AuthorService.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/AuthorService.java
new file mode 100644 (file)
index 0000000..bb47e26
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.service;
+
+import java.util.Date;
+import java.util.List;
+
+import org.hedgecode.chess.domain.Author;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public interface AuthorService extends DomainService<Author> {
+
+    List<Author> findAll();
+
+    Author findById(Long id);
+
+    List<Author> findByName(String name);
+
+    List<Author> findByBirthDate(Date birthdate);
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/DomainService.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/DomainService.java
new file mode 100644 (file)
index 0000000..df37b09
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.service;
+
+import org.hedgecode.chess.domain.DomainObject;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public interface DomainService<DomainType extends DomainObject> {
+
+    DomainType create(DomainType domainObject);
+
+    DomainType update(DomainType domainObject);
+
+    void remove(DomainType domainObject);
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/EtudeService.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/EtudeService.java
new file mode 100644 (file)
index 0000000..86563c0
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.service;
+
+import java.math.BigInteger;
+import java.util.List;
+
+import org.hedgecode.chess.domain.Author;
+import org.hedgecode.chess.domain.Etude;
+import org.hedgecode.chess.domain.EtudeType;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public interface EtudeService extends DomainService<Etude> {
+
+    List<Etude> findAll();
+
+    Etude findById(Long id);
+
+    List<Etude> findByHash(BigInteger hash);
+
+    List<Etude> findByType(EtudeType etudeType);
+
+    List<Etude> findByAuthor(Author author);
+
+    List<Etude> findByBlob(byte[] blob);
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/EtudeTypeService.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/EtudeTypeService.java
new file mode 100644 (file)
index 0000000..51abdc8
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.service;
+
+import java.util.List;
+
+import org.hedgecode.chess.domain.EtudeType;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public interface EtudeTypeService extends DomainService<EtudeType> {
+
+    List<EtudeType> findAll();
+
+    EtudeType findById(Long id);
+
+    EtudeType findByBrief(String brief);
+
+    EtudeType findDefault();
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/EtudeUtils.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/EtudeUtils.java
new file mode 100644 (file)
index 0000000..0556cd1
--- /dev/null
@@ -0,0 +1,182 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.service;
+
+import java.math.BigInteger;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.hedgecode.chess.position.Color;
+import org.hedgecode.chess.position.ColorPiece;
+import org.hedgecode.chess.position.Piece;
+import org.hedgecode.chess.position.Position;
+import org.hedgecode.chess.position.Positions;
+import org.hedgecode.chess.position.Square;
+import org.hedgecode.chess.position.SquareSort;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public final class EtudeUtils {
+
+    private static final byte PIECE_MASK   = 0b1111;
+    private static final byte PIECE_LENGTH = 4;
+
+    private static final byte NUM_WHITE_PAWN   = 0;
+    private static final byte NUM_WHITE_KNIGHT = 1;
+    private static final byte NUM_WHITE_BISHOP = 2;
+    private static final byte NUM_WHITE_ROOK   = 3;
+    private static final byte NUM_WHITE_QUEEN  = 4;
+
+    private static final byte NUM_BLACK_PAWN   = 5;
+    private static final byte NUM_BLACK_KNIGHT = 6;
+    private static final byte NUM_BLACK_BISHOP = 7;
+    private static final byte NUM_BLACK_ROOK   = 8;
+    private static final byte NUM_BLACK_QUEEN  = 9;
+
+    private static final byte BLOB_PIECE_EMPTY  = 0b0000;
+
+    private static final byte BLOB_WHITE_PAWN   = 0b0001;
+    private static final byte BLOB_WHITE_KNIGHT = 0b0010;
+    private static final byte BLOB_WHITE_BISHOP = 0b0011;
+    private static final byte BLOB_WHITE_ROOK   = 0b0100;
+    private static final byte BLOB_WHITE_QUEEN  = 0b0101;
+    private static final byte BLOB_WHITE_KING   = 0b0110;
+
+    private static final byte BLOB_BLACK_PAWN   = 0b1001;
+    private static final byte BLOB_BLACK_KNIGHT = 0b1010;
+    private static final byte BLOB_BLACK_BISHOP = 0b1011;
+    private static final byte BLOB_BLACK_ROOK   = 0b1100;
+    private static final byte BLOB_BLACK_QUEEN  = 0b1101;
+    private static final byte BLOB_BLACK_KING   = 0b1110;
+
+    private static final Map<ColorPiece, Byte> PIECE_NUMBERS = new HashMap<ColorPiece, Byte>() {
+        {
+            put(ColorPiece.WHITE_PAWN, NUM_WHITE_PAWN);     put(ColorPiece.BLACK_PAWN, NUM_BLACK_PAWN);
+            put(ColorPiece.WHITE_KNIGHT, NUM_WHITE_KNIGHT); put(ColorPiece.BLACK_KNIGHT, NUM_BLACK_KNIGHT);
+            put(ColorPiece.WHITE_BISHOP, NUM_WHITE_BISHOP); put(ColorPiece.BLACK_BISHOP, NUM_BLACK_BISHOP);
+            put(ColorPiece.WHITE_ROOK, NUM_WHITE_ROOK);     put(ColorPiece.BLACK_ROOK, NUM_BLACK_ROOK);
+            put(ColorPiece.WHITE_QUEEN, NUM_WHITE_QUEEN);   put(ColorPiece.BLACK_QUEEN, NUM_BLACK_QUEEN);
+        }
+    };
+
+    private static final Map<ColorPiece, Byte> BLOB_PIECES = new HashMap<ColorPiece, Byte>() {
+        {
+            put(ColorPiece.WHITE_PAWN, BLOB_WHITE_PAWN);     put(ColorPiece.BLACK_PAWN, BLOB_BLACK_PAWN);
+            put(ColorPiece.WHITE_KNIGHT, BLOB_WHITE_KNIGHT); put(ColorPiece.BLACK_KNIGHT, BLOB_BLACK_KNIGHT);
+            put(ColorPiece.WHITE_BISHOP, BLOB_WHITE_BISHOP); put(ColorPiece.BLACK_BISHOP, BLOB_BLACK_BISHOP);
+            put(ColorPiece.WHITE_ROOK, BLOB_WHITE_ROOK);     put(ColorPiece.BLACK_ROOK, BLOB_BLACK_ROOK);
+            put(ColorPiece.WHITE_QUEEN, BLOB_WHITE_QUEEN);   put(ColorPiece.BLACK_QUEEN, BLOB_BLACK_QUEEN);
+            put(ColorPiece.WHITE_KING, BLOB_WHITE_KING);     put(ColorPiece.BLACK_KING, BLOB_BLACK_KING);
+        }
+    };
+
+    public static BigInteger hashPosition(Position position) {
+        StringBuilder sb = new StringBuilder();
+        sb.append(
+                numSquare(
+                        kingSquare(Color.WHITE, position)
+                )
+        ).append(
+                numSquare(
+                        kingSquare(Color.BLACK, position)
+                )
+        );
+        int empty = 0;
+        for (Map.Entry<Square, ColorPiece> entry : position.getSquares(SquareSort.A8H1).entrySet()) {
+            ColorPiece colorPiece = entry.getValue();
+            if (colorPiece != null && !Piece.KING.equals(colorPiece.piece())) {
+                if (empty > 0)
+                    sb.append(empty);
+                sb.append(
+                        PIECE_NUMBERS.get(colorPiece)
+                );
+                empty = 0;
+            } else {
+                ++empty;
+            }
+        }
+        if (empty > 0)
+            sb.append(empty);
+
+        return new BigInteger(
+                sb.toString() /* todo: SQL: NUMERIC(64,0) */
+        );
+    }
+
+    public static byte[] blobPosition(Position position) {
+        byte[] blob = new byte[Square.getSize() * Square.getSize()];
+        int i = 0;
+        for (Map.Entry<Square, ColorPiece> entry : position.getSquares(SquareSort.A8H1).entrySet()) {
+            ColorPiece colorPiece = entry.getValue();
+            blob[i++] = colorPiece != null ? BLOB_PIECES.get(colorPiece) : BLOB_PIECE_EMPTY;
+        }
+        return compress(blob);
+    }
+
+    private static Square kingSquare(Color color, Position position) {
+        Square kingSquare = null;
+
+        Map<Square, ColorPiece> king = position.getSquarePieces(color, Piece.KING);
+
+        if (king.isEmpty() || king.size() > 1)
+            throw new RuntimeException("Incorrect position"); // todo
+
+        for (Square square : king.keySet()) {
+            kingSquare = square;
+        }
+        return kingSquare;
+    }
+
+    private static int numSquare(Square square) {
+        int v = square.getV() + 1;
+        int h = Square.getSize() - square.getH();
+        return Square.getSize() * (h - 1) + v;
+    }
+
+    private static byte[] compress(byte[] bytes) {
+        byte[] cmpBytes = new byte[bytes.length / 2];
+        byte high, low;
+        int i = 0;
+        for (int j = 0; j < cmpBytes.length; ++j) {
+            high = (byte) ((bytes[i++] & PIECE_MASK) << PIECE_LENGTH);
+            low  = (byte) (bytes[i++] & PIECE_MASK);
+            cmpBytes[j] = (byte) (high | low);
+        }
+        return cmpBytes;
+    }
+
+    private EtudeUtils() {
+        throw new AssertionError(
+                "No org.hedgecode.chess.service.EtudeUtils instances!"
+        );
+    }
+
+
+    public static void main(String... args) {
+        System.out.println(
+                hashPosition(Positions.INITIAL.getPosition())
+        );
+        System.out.println(
+                Arrays.toString(blobPosition(Positions.INITIAL.getPosition()))
+        );
+    }
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/jpa/JpaAuthorService.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/jpa/JpaAuthorService.java
new file mode 100644 (file)
index 0000000..3033337
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.service.jpa;
+
+import java.util.Date;
+import java.util.List;
+
+import javax.persistence.Query;
+
+import org.hedgecode.chess.domain.Author;
+import org.hedgecode.chess.service.AuthorService;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class JpaAuthorService extends JpaDomainService<Author> implements AuthorService {
+
+    @Override
+    @SuppressWarnings(
+            "unchecked"
+    )
+    public List<Author> findAll() {
+        return getEntityManager().createNamedQuery(
+                Author.FIND_ALL
+        ).getResultList();
+    }
+
+    @Override
+    @SuppressWarnings(
+            "unchecked"
+    )
+    public Author findById(Long id) {
+        Query query = getEntityManager().createNamedQuery(
+                Author.FIND_BY_ID
+        );
+        query.setParameter(Author.ID_PROPERTY, id);
+        return (Author) query.getSingleResult();
+    }
+
+    @Override
+    @SuppressWarnings(
+            "unchecked"
+    )
+    public List<Author> findByName(String name) {
+        Query query = getEntityManager().createNamedQuery(
+                Author.FIND_BY_NAME
+        );
+        query.setParameter(Author.NAME_PROPERTY, name);
+        return query.getResultList();
+    }
+
+    @Override
+    @SuppressWarnings(
+            "unchecked"
+    )
+    public List<Author> findByBirthDate(Date birthdate) {
+        Query query = getEntityManager().createNamedQuery(
+                Author.FIND_BY_BIRTHDATE
+        );
+        query.setParameter(Author.BIRTHDATE_PROPERTY, birthdate);
+        return query.getResultList();
+    }
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/jpa/JpaDomainService.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/jpa/JpaDomainService.java
new file mode 100644 (file)
index 0000000..37a4dcb
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.service.jpa;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Persistence;
+
+import org.hedgecode.chess.domain.DomainObject;
+import org.hedgecode.chess.service.DomainService;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public abstract class JpaDomainService<DomainType extends DomainObject> implements DomainService<DomainType> {
+
+    // todo: maybe single entityManager
+    private DomainManager domainManager;
+
+    JpaDomainService() {
+        domainManager = new DomainManager();
+    }
+
+    EntityManager getEntityManager() {
+        return domainManager.getEntityManager();
+    }
+
+    @Override
+    public DomainType create(DomainType domainObject) {
+        getEntityManager().persist(
+                domainObject
+        );
+        return domainObject;
+    }
+
+    @Override
+    public DomainType update(DomainType domainObject) {
+        getEntityManager().persist(
+                domainObject
+        );
+        return domainObject;
+    }
+
+    @Override
+    public void remove(DomainType domainObject) {
+        getEntityManager().remove(
+                domainObject
+        );
+    }
+
+    private class DomainManager {
+
+        private static final String PERSISTENCE_UNIT_NAME = "db-etude-persistence-unit";
+
+        private EntityManager entityManager;
+
+        EntityManager getEntityManager() {
+            if (entityManager == null)
+                createEntityManager();
+            return entityManager;
+        }
+
+        void createEntityManager() {
+            entityManager = Persistence.createEntityManagerFactory(
+                    PERSISTENCE_UNIT_NAME
+            ).createEntityManager();
+        }
+
+    }
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/jpa/JpaEtudeService.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/jpa/JpaEtudeService.java
new file mode 100644 (file)
index 0000000..79a701e
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.service.jpa;
+
+import java.math.BigInteger;
+import java.util.List;
+
+import javax.persistence.Query;
+
+import org.hedgecode.chess.domain.Author;
+import org.hedgecode.chess.domain.Etude;
+import org.hedgecode.chess.domain.EtudeType;
+import org.hedgecode.chess.service.EtudeService;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class JpaEtudeService extends JpaDomainService<Etude> implements EtudeService {
+
+    @Override
+    @SuppressWarnings(
+            "unchecked"
+    )
+    public List<Etude> findAll() {
+        return getEntityManager().createNamedQuery(
+                Etude.FIND_ALL
+        ).getResultList();
+    }
+
+    @Override
+    @SuppressWarnings(
+            "unchecked"
+    )
+    public Etude findById(Long id) {
+        Query query = getEntityManager().createNamedQuery(
+                Etude.FIND_BY_ID
+        );
+        query.setParameter(Etude.ID_PROPERTY, id);
+        return (Etude) query.getSingleResult();
+    }
+
+    @Override
+    @SuppressWarnings(
+            "unchecked"
+    )
+    public List<Etude> findByHash(BigInteger hash) {
+        Query query = getEntityManager().createNamedQuery(
+                Etude.FIND_BY_HASH
+        );
+        query.setParameter(Etude.HASH_PROPERTY, hash);
+        return query.getResultList();
+    }
+
+    @Override
+    @SuppressWarnings(
+            "unchecked"
+    )
+    public List<Etude> findByType(EtudeType etudeType) {
+        Query query = getEntityManager().createNamedQuery(
+                Etude.FIND_BY_TYPE
+        );
+        query.setParameter(Etude.TYPE_PROPERTY, etudeType);
+        return query.getResultList();
+    }
+
+    @Override
+    @SuppressWarnings(
+            "unchecked"
+    )
+    public List<Etude> findByAuthor(Author author) {
+        Query query = getEntityManager().createNamedQuery(
+                Etude.FIND_BY_AUTHOR
+        );
+        query.setParameter(Etude.AUTHOR_PROPERTY, author);
+        return query.getResultList();
+    }
+
+    @Override
+    @SuppressWarnings(
+            "unchecked"
+    )
+    public List<Etude> findByBlob(byte[] blob) {
+        Query query = getEntityManager().createNamedQuery(
+                Etude.FIND_BY_BLOB
+        );
+        query.setParameter(Etude.BLOB_PROPERTY, blob);
+        return query.getResultList();
+    }
+
+}
diff --git a/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/jpa/JpaEtudeTypeService.java b/chesshog-db-etude/src/main/java/org/hedgecode/chess/service/jpa/JpaEtudeTypeService.java
new file mode 100644 (file)
index 0000000..adf4b13
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.service.jpa;
+
+import java.util.List;
+
+import javax.persistence.Query;
+
+import org.hedgecode.chess.domain.EtudeType;
+import org.hedgecode.chess.service.EtudeTypeService;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class JpaEtudeTypeService extends JpaDomainService<EtudeType> implements EtudeTypeService {
+
+    private static final long DEFAULT_ID = 1;
+
+    @Override
+    @SuppressWarnings(
+            "unchecked"
+    )
+    public List<EtudeType> findAll() {
+        return getEntityManager().createNamedQuery(
+                EtudeType.FIND_ALL
+        ).getResultList();
+    }
+
+    @Override
+    @SuppressWarnings(
+            "unchecked"
+    )
+    public EtudeType findById(Long id) {
+        Query query = getEntityManager().createNamedQuery(
+                EtudeType.FIND_BY_ID
+        );
+        query.setParameter(EtudeType.ID_PROPERTY, id);
+        return (EtudeType) query.getSingleResult();
+    }
+
+    @Override
+    @SuppressWarnings(
+            "unchecked"
+    )
+    public EtudeType findByBrief(String brief) {
+        Query query = getEntityManager().createNamedQuery(
+                EtudeType.FIND_BY_BRIEF
+        );
+        query.setParameter(EtudeType.BRIEF_PROPERTY, brief);
+        return (EtudeType) query.getSingleResult();
+    }
+
+    @Override
+    @SuppressWarnings(
+            "unchecked"
+    )
+    public EtudeType findDefault() {
+        return findById(
+                DEFAULT_ID
+        );
+    }
+
+}
diff --git a/chesshog-db-etude/src/main/resources/META-INF/persistence.xml b/chesshog-db-etude/src/main/resources/META-INF/persistence.xml
new file mode 100644 (file)
index 0000000..8d0db9d
--- /dev/null
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+  ~ Copyright (c) 2018. Developed by Hedgecode.
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~   http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<persistence xmlns="http://java.sun.com/xml/ns/persistence"
+             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
+             version="1.0">
+
+    <persistence-unit name="db-etude-persistence-unit" transaction-type="RESOURCE_LOCAL">
+        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
+
+        <class>org.hedgecode.chess.domain.Etude</class>
+
+        <properties>
+            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
+            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/dbetude"/>
+            <property name="hibernate.connection.username" value=""/>
+            <property name="hibernate.connection.password" value=""/>
+            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
+            <property name="hibernate.hbm2ddl.auto" value="update"/>
+        </properties>
+    </persistence-unit>
+
+</persistence>
\ No newline at end of file