/*
 *
 *  Copyright 2012-2013 Eurocommercial Properties NV
 *
 *
 *  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.estatio.dom.index;

import java.math.BigDecimal;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.VersionStrategy;

import org.joda.time.LocalDate;

import org.apache.isis.applib.annotation.Disabled;
import org.apache.isis.applib.annotation.Hidden;
import org.apache.isis.applib.annotation.Immutable;
import org.apache.isis.applib.annotation.Prototype;
import org.apache.isis.applib.annotation.Title;
import org.apache.isis.applib.annotation.Where;

import org.estatio.dom.EstatioMutableObject;
import org.estatio.dom.WithStartDate;

/**
 * Holds the {@link #getValue() value} of an {@link #getIndexBase() index (base)} from a particular
 * {@link #getStartDate() point in time} (until succeeded by some other value).
 */
@javax.jdo.annotations.PersistenceCapable(identityType = IdentityType.DATASTORE)
@javax.jdo.annotations.DatastoreIdentity(
        strategy = IdGeneratorStrategy.NATIVE,
        column = "id")
@javax.jdo.annotations.Version(
        strategy = VersionStrategy.VERSION_NUMBER,
        column = "version")
@javax.jdo.annotations.Queries({
        @javax.jdo.annotations.Query(
                name = "findByIndexAndStartDate", language = "JDOQL",
                value = "SELECT "
                        + "FROM org.estatio.dom.index.IndexValue "
                        + "WHERE indexBase.index == :index "
                        + "   && startDate == :startDate"),
        @javax.jdo.annotations.Query(
                name = "findLastByIndex", language = "JDOQL",
                value = "SELECT "
                        + "FROM org.estatio.dom.index.IndexValue "
                        + "WHERE indexBase.index == :index "
                        + "ORDER BY startDate DESC")
})
@Immutable
public class IndexValue 
        extends EstatioMutableObject<IndexValue> 
        implements WithStartDate {

    public static final int VALUE_SCALE = 4;

    public IndexValue() {
        super("indexBase, startDate desc");
    }
    
    // //////////////////////////////////////

    @javax.jdo.annotations.Persistent
    private LocalDate startDate;

    @javax.jdo.annotations.Column(allowsNull="false")
    @Title(sequence = "2", prepend = ":")
    @Disabled
    @Override
    public LocalDate getStartDate() {
        return startDate;
    }

    @Override
    public void setStartDate(final LocalDate startDate) {
        this.startDate = startDate;
    }

    // //////////////////////////////////////

    private IndexBase indexBase;

    @javax.jdo.annotations.Column(name="indexBaseId", allowsNull="false")
    @Hidden(where = Where.PARENTED_TABLES)
    @Title(sequence = "2")
    @Disabled
    public IndexBase getIndexBase() {
        return indexBase;
    }

    public void setIndexBase(final IndexBase indexBase) {
        this.indexBase = indexBase;
    }

    public void modifyIndexBase(final IndexBase indexBase) {
        IndexBase currentIndexBase = getIndexBase();
        if (indexBase == null || indexBase.equals(currentIndexBase)) {
            return;
        }
        indexBase.addToValues(this);
    }

    public void clearIndexBase() {
        IndexBase currentIndexBase = getIndexBase();
        if (currentIndexBase == null) {
            return;
        }
        currentIndexBase.removeFromValues(this);
    }

    // //////////////////////////////////////

    private BigDecimal value;

    @javax.jdo.annotations.Column(scale = VALUE_SCALE, allowsNull="false")
    @Disabled
    public BigDecimal getValue() {
        return value;
    }

    public void setValue(final BigDecimal value) {
        this.value = value;
    }

    // //////////////////////////////////////

    @Prototype
    public void remove() {
        getContainer().remove(this);
    }


}
@@ -2438,16 +2438,148 @@
 mutable%0A
+@javax.jdo.annotations.Unique(%0A        name = %22IndexValue_indexBase_startDate_IDX%22,%0A        members = %7B %22indexBase%22, %22startDate%22 %7D)%0A
 public c
@@ -2593,17 +2593,16 @@
 dexValue
- 
 %0A       
@@ -2642,17 +2642,16 @@
 exValue%3E
- 
 %0A       
@@ -2805,13 +2805,9 @@
   %7D%0A
-    
 %0A
+
     
@@ -2957,33 +2957,35 @@
 olumn(allowsNull
-=
+ = 
 %22false%22)%0A    @Ti
@@ -3352,17 +3352,19 @@
 umn(name
-=
+ = 
 %22indexBa
@@ -3372,33 +3372,35 @@
 eId%22, allowsNull
-=
+ = 
 %22false%22)%0A    @Hi
@@ -4264,17 +4264,19 @@
 lowsNull
-=
+ = 
 %22false%22)
@@ -4570,13 +4570,12 @@
 s);%0A
+
     %7D%0A%0A
-%0A
 %7D

add index to index value