/*
 *
 *  Copyright 2012-2014 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.agreement;

import java.util.List;

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

import com.google.common.base.Objects;
import com.google.common.base.Predicate;

import org.apache.isis.applib.annotation.Bounded;
import org.apache.isis.applib.annotation.Immutable;
import org.apache.isis.applib.annotation.Programmatic;
import org.apache.isis.applib.annotation.Title;

import org.estatio.dom.EstatioImmutableObject;
import org.estatio.dom.JdoColumnLength;
import org.estatio.dom.WithTitleComparable;

@javax.jdo.annotations.PersistenceCapable(identityType=IdentityType.DATASTORE)
@javax.jdo.annotations.DatastoreIdentity(
        strategy=IdGeneratorStrategy.NATIVE, 
        column="id")
@javax.jdo.annotations.Queries({
        @javax.jdo.annotations.Query(
                name = "findByTitle", language = "JDOQL",
                value = "SELECT "
                        + "FROM org.estatio.dom.agreement.AgreementRoleType "
                        + "WHERE title == :title "),
        @javax.jdo.annotations.Query(
                name = "findByAgreementType", language = "JDOQL",
                value = "SELECT "
                        + "FROM org.estatio.dom.agreement.AgreementRoleType "
                        + "WHERE appliesTo == :agreementType ")
})
@Immutable
@Bounded
public class AgreementRoleType extends EstatioImmutableObject<AgreementRoleType> 
        implements WithTitleComparable<AgreementRoleType> {

    public AgreementRoleType() {
        super("title");
    }

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

    private String title;

    @javax.jdo.annotations.Column(allowsNull="false", length=JdoColumnLength.TITLE)
    @Title
    public String getTitle() {
        return title;
    }

    public void setTitle(final String title) {
        this.title = title;
    }

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

    private AgreementType appliesTo;

    @javax.jdo.annotations.Column(name="appliesToAgreementTypeId", allowsNull="false")
    public AgreementType getAppliesTo() {
        return appliesTo;
    }

    public void setAppliesTo(final AgreementType agreementType) {
        this.appliesTo = agreementType;
    }

    // //////////////////////////////////////
    
    @Programmatic
    public Predicate<? super AgreementRole> matchingRole() {
        return new Predicate<AgreementRole>() {
            @Override
            public boolean apply(final AgreementRole ar) {
                return ar != null && Objects.equal(ar.getType(), AgreementRoleType.this) ? true : false;
            }
        };
    }

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

    public static List<AgreementRoleType> applicableTo(final AgreementType at) {
        return at.getRoleTypesApplicableTo();
    }



}

@@ -786,16 +786,53 @@
 ityType;
+%0Aimport javax.jdo.annotations.Unique;
 %0A%0Aimport
@@ -1298,17 +1298,19 @@
 tityType
-=
+ = 
 Identity
@@ -1383,17 +1383,19 @@
 strategy
-=
+ = 
 IdGenera
@@ -1413,17 +1413,16 @@
 .NATIVE,
- 
 %0A       
@@ -1432,15 +1432,85 @@
 lumn
-=
+ = 
 %22id%22)%0A
+@Unique(name = %22AgreemenRoleType_title_UNQ%22, members = %7B %22title%22 %7D)%0A
 @jav
@@ -2182,17 +2182,16 @@
 oleType%3E
- 
 %0A       
@@ -2422,25 +2422,27 @@
 n(allowsNull
-=
+ = 
 %22false%22, len
@@ -2444,17 +2444,19 @@
 , length
-=
+ = 
 JdoColum
@@ -2746,17 +2746,19 @@
 umn(name
-=
+ = 
 %22applies
@@ -2787,17 +2787,19 @@
 lowsNull
-=
+ = 
 %22false%22)
@@ -3033,20 +3033,16 @@
 ///////%0A
-    
 %0A    @Pr
@@ -3560,12 +3560,10 @@
 %0A    %7D%0A%0A
-%0A%0A
 %7D%0A

Add unique constraint on title to Agreement Role