org.openkoala.koala.base.KmBaseLegacyEntity.java Source code

Java tutorial

Introduction

Here is the source code for org.openkoala.koala.base.KmBaseLegacyEntity.java

Source

/*
 * Copyright (c) Koala 2012-2014 All Rights Reserved
 * 
 * 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.openkoala.koala.base;

import java.io.Serializable;
import java.util.List;

import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.dayatang.domain.Entity;
import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;

/**
 * ??
 * 
 */
@MappedSuperclass
public abstract class KmBaseLegacyEntity implements Entity {

    /**
     * 
     */
    private static final long serialVersionUID = 871428741460277125L;

    private static EntityRepository repository;

    /**
     * 
     * @return
     */
    @Transient
    public boolean isNew() {
        return getId() == null;
    }

    public static EntityRepository getRepository() {
        if (repository == null) {
            repository = InstanceFactory.getInstance(EntityRepository.class, "km_repository");
        }
        return repository;
    }

    public static void setRepository(EntityRepository repository) {
        KmBaseLegacyEntity.repository = repository;
    }

    public void save() {
        getRepository().save(this);
    }

    public void remove() {
        getRepository().remove(this);
    }

    public static <T extends Entity> boolean exists(Class<T> clazz, Serializable id) {
        return getRepository().exists(clazz, id);
    }

    public static <T extends Entity> T get(Class<T> clazz, Serializable id) {
        return getRepository().get(clazz, id);
    }

    public static <T extends Entity> T getUnmodified(Class<T> clazz, T entity) {
        return getRepository().getUnmodified(clazz, entity);
    }

    public static <T extends Entity> T load(Class<T> clazz, Serializable id) {
        return getRepository().load(clazz, id);
    }

    public static <T extends Entity> List<T> findAll(Class<T> clazz) {
        return getRepository().findAll(clazz);
    }

    @Override
    public abstract int hashCode();

    @Override
    public abstract boolean equals(Object arg0);

    @Override
    public String toString() {
        // return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
        // .append(recordId).append(tariffNo).toString();
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE).toString();
    }
}