/** See the file "LICENSE" for the full license governing this code. */
package au.org.ands.vocabs.toolkit.db.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import com.fasterxml.jackson.databind.JsonNode;

import au.org.ands.vocabs.toolkit.db.TasksUtils;

/**
 * Versions model class.
 */
@Entity
@Table(name = "versions",
uniqueConstraints = @UniqueConstraint(columnNames = "repository_id"))
public class Versions {

    /** Key of the release date used in the data field. */
    private static final String RELEASE_DATE_KEY = "release_date";

    /** id. */
    private Integer id;
    /** title. */
    private String title;
    /** status. */
    private String status;
    /** vocabId. */
    private Integer vocabId;
    /** data. */
    private String data;
    /** respositoryId. */
    private String repositoryId;

    /** Get the id.
     * @return The id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

    /** Set the id.
     * @param anId the id
     */
    public void setId(final Integer anId) {
        id = anId;
    }

    /** Get the title.
     * @return The title
     */
    @Column(name = "title")
    public String getTitle() {
        return title;
    }

    /** Set the title.
     * @param aTitle the title
     */
    public void setTitle(final String aTitle) {
        title = aTitle;
    }

    /** Get the status.
     * @return The status
     */
    @Column(name = "status", length = 45)
    public String getStatus() {
        return status;
    }

    /** Set the status.
     * @param aStatus the status
     */
    public void setStatus(final String aStatus) {
        status = aStatus;
    }

    /** Get the release date. This value is stored in the data field. Note
     * the Transient annotation, as this is not a column in the table.
     * @return The release date
     */
    @Transient
    public String getReleaseDate() {
        if (data == null || data.isEmpty()) {
            return null;
        }
        JsonNode dataJson = TasksUtils.jsonStringToTree(data);
        JsonNode releaseDate = dataJson.get(RELEASE_DATE_KEY);
        if (releaseDate == null) {
            return null;
        }
        return releaseDate.asText();
    }

    // Uncomment and test the following when needed. It has not yet
    // been tested!
//    /** Set the release date. This value is stored in the data field.
//     * Note the Transient annotation, as this is not a column in the table.
//     * @param aReleaseDate the release date
//     */
//    @Transient
//    public void setReleaseDate(final String aReleaseDate) {
//        if (data == null || data.isEmpty()) {
//            data = "{}";
//        }
//        JsonNode dataJson = TasksUtils.jsonStringToTree(data);
//        JsonObjectBuilder jobData = Json.createObjectBuilder();
//        Iterator<Entry<String, JsonNode>> dataJsonIterator =
//                dataJson.fields();
//        while (dataJsonIterator.hasNext()) {
//            Entry<String, JsonNode> entry = dataJsonIterator.next();
//            jobData.add(entry.getKey(), entry.getValue().asText());
//        }
//        jobData.add(RELEASE_DATE_KEY, aReleaseDate);
//        data = jobData.build().toString();
//    }

    /** Get the vocab id.
     * @return The vocab id
     */
    @Column(name = "vocab_id")
    public Integer getVocabId() {
        return vocabId;
    }

    /** Set the vocab id.
     * @param aVocabId the vocab id
     */
    public void setVocabId(final Integer aVocabId) {
        vocabId = aVocabId;
    }

    /** Get the data.
     * @return The data
     */
    @Column(name = "data", length = 65535)
    public String getData() {
        return data;
    }

    /** Set the data.
     * @param aData the data
     */
    public void setData(final String aData) {
        data = aData;
    }

    /** Get the repository id.
     * @return The repository id
     */
    @Column(name = "repository_id", unique = true, length = 128)
    public String getRepositoryId() {
        return repositoryId;
    }

    /** Set the repository id.
     * @param aRepositoryId the repository id
     */
    public void setRepositoryId(final String aRepositoryId) {
        repositoryId = aRepositoryId;
    }

}

@@ -1011,67 +1011,8 @@
 ata;
-%0A    /** respositoryId. */%0A    private String repositoryId;
 %0A%0A  
@@ -4128,409 +4128,6 @@
  %7D%0A%0A
-    /** Get the repository id.%0A     * @return The repository id%0A     */%0A    @Column(name = %22repository_id%22, unique = true, length = 128)%0A    public String getRepositoryId() %7B%0A        return repositoryId;%0A    %7D%0A%0A    /** Set the repository id.%0A     * @param aRepositoryId the repository id%0A     */%0A    public void setRepositoryId(final String aRepositoryId) %7B%0A        repositoryId = aRepositoryId;%0A    %7D%0A%0A
 %7D%0A

Remove unused repository_id column from Versions db model