All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.deephacks.tools4j.config.examples.family.FamilyTestData Maven / Gradle / Ivy

There is a newer version: 0.15.0
Show newest version
/**
 * 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.deephacks.tools4j.config.examples.family;

import org.deephacks.tools4j.config.admin.AdminContext;
import org.deephacks.tools4j.config.model.Bean;
import org.deephacks.tools4j.config.model.Bean.BeanId;
import org.deephacks.tools4j.config.model.Lookup;

public class FamilyTestData {
    static AdminContext admin = Lookup.get().lookup(AdminContext.class);

    public static Bean createFamily(String prefix, String childGender) {
        int counter = 0;
        String lastName = "lastName";
        Bean male = getMale(id(prefix, counter++), lastName);
        Bean female = getFemale(id(prefix, counter++), lastName);
        Bean child = getMale(id(prefix, counter++), lastName);
        child.setProperty("gender", childGender);
        Bean marriage = getMarriage(male.getId().getInstanceId(), female.getId().getInstanceId());
        male.addReference("children", child.getId());
        female.addReference("children", child.getId());
        marriage.addReference("children", child.getId());

        admin.create(child);
        admin.create(male);
        admin.create(female);
        admin.create(marriage);

        return child;
    }

    public static Bean createFamily(String prefix, Bean child1, Bean child2, String childGender) {
        int counter = 0;
        String lastName = "lastName";

        Bean male = child1;
        Bean FEMALE = child2;
        Bean child = getMale(id(prefix, counter++), lastName);
        child.setProperty("gender", childGender);
        Bean marriage = getMarriage(male.getId().getInstanceId(), FEMALE.getId().getInstanceId());
        male.addReference("children", child.getId());
        FEMALE.addReference("children", child.getId());

        marriage.addReference("children", child.getId());
        admin.create(child);
        admin.merge(male);
        admin.merge(FEMALE);
        admin.create(marriage);
        return child;
    }

    private static String id(String prefix, int counter) {
        return prefix + "." + ++counter;
    }

    public static Bean getMale(String name, String lastName) {
        Bean b = getPerson(name, lastName);
        b.addProperty("gender", "MALE");
        return b;
    }

    public static Bean getFemale(String name, String lastName) {
        Bean b = getPerson(name, lastName);
        b.addProperty("gender", "FEMALE");
        return b;
    }

    public static Bean getPerson(String name, String lastName) {
        Bean b = Bean.create(BeanId.create(name, "Person"));
        b.addProperty("firstName", name);
        b.addProperty("lastName", lastName);
        return b;
    }

    public static Bean getMarriage(String male, String FEMALE) {
        Bean b = Bean.create(BeanId.create(male + "+" + FEMALE, "Marriage"));
        b.addReference("couple", BeanId.create(male, "Person"));
        b.addReference("couple", BeanId.create(FEMALE, "Person"));
        return b;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy