com.jk.faces.mb.JKOrmManagedBean Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2002-2016 Jalal Kiswani.
*
* 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 com.jk.faces.mb;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.component.EditableValueHolder;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlForm;
import javax.faces.context.FacesContext;
import javax.faces.context.PartialViewContext;
import com.jk.db.dataaccess.orm.JKEntity;
import com.jk.db.dataaccess.orm.JKOrmDataAccess;
import com.jk.db.datasource.JKDataSourceFactory;
import com.jk.exceptions.JKException;
import com.jk.util.JKObjectUtil;
/**
* The Class JKOrmManagedBean.
*
* @author Jalal Kiswani
* @param
* the generic type
*/
public abstract class JKOrmManagedBean extends JKManagedBean {
private T entity;
private List all;
private Class entityClass;
private boolean alwaysRefreshList;
private boolean resetAfterSave = true;
/**
* Instantiates a new JK orm managed bean.
*/
public JKOrmManagedBean() {
ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
this.entityClass = (Class) genericSuperclass.getActualTypeArguments()[0];
}
/**
* Insert.
*
* @return the string
*/
public String insert() {
if (entity == null) {
throw new IllegalStateException("Entity is null while calling merge");
}
beforeInsert();
getDataAccess().insert(entity);
reloadAll();
success("INSERT_SUCCESSFULLY");
afterInsert();
if (isResetAfterSave()) {
reset();
} else {
// force reload value from database
find(entity.getIdValue());
}
return null;
}
/**
* Update.
*
* @return the string
*/
public String update() {
if (entity == null) {
throw new IllegalStateException("Entity is null while calling merge");
}
beforeUpdate();
getDataAccess().insert(entity);
reloadAll();
success("UPDATED_SUCCESSFULLY");
afterUpdate();
if (isResetAfterSave()) {
reset();
} else {
// force reload value from database
find(entity.getIdValue());
}
return null;
}
protected void beforeInsert() {
}
protected void afterInsert() {
}
protected void beforeUpdate() {
}
protected void afterUpdate() {
}
protected void beforeDelete() {
}
/**
* Find.
*
* @param object
* the object
* @return the string
*/
public String find(Object object) {
T entity = (T) getDataAccess().find(getEntityClass(), object);
setEntity(entity);
return null;
}
protected Class getEntityClass() {
return entityClass;
}
/**
* Gets the all.
*
* @return the all
*/
public List getAll() {
if (all == null) {
reloadAll();
}
return all;
}
/**
* Delete.
*
* @return the string
*/
// ///////////////////////////////////////////////////
public String delete() {
beforeDelete();
getDataAccess().delete(getIdValue(), getEntityClass());
afterDelete();
reloadAll();
success("DELETED_SUCCESSFULLY");
reset();
return null;
}
protected void afterDelete() {
}
/**
* Gets the id value.
*
* @return the id value
*/
public Object getIdValue() {
return getEntity().getIdValue();
}
/**
* Reload all.
*/
public void reloadAll() {
all = getDataAccess().getList(getEntityClass());
}
/**
* Gets the entity.
*
* @return the entity
*/
public T getEntity() {
if (entity == null) {
entity = createEmptyEntity();
}
return entity;
}
protected T createEmptyEntity() {
return JKObjectUtil.newInstance(getEntityClass());
}
/**
* Sets the entity.
*
* @param entity
* the new entity
*/
public void setEntity(T entity) {
// to avoid loosing any local views value
if (entity != this.entity) {
if (this.entity == null || (!this.entity.equals(entity))) {
this.entity = entity;
}
}
}
// ///////////////////////////////////////////////////
protected JKOrmDataAccess getDataAccess() {
return JKDataSourceFactory.getOrmDataAccess();
}
/**
* Reset.
*
* @return the string
*/
// ///////////////////////////////////////////////////
public String reset() {
entity = null;
return null;
}
/**
* Checks if is always refresh list.
*
* @return true, if is always refresh list
*/
// ///////////////////////////////////////////////////
public boolean isAlwaysRefreshList() {
return alwaysRefreshList;
}
/**
* Sets the always refresh list.
*
* @param alwaysRefreshList
* the new always refresh list
*/
public void setAlwaysRefreshList(boolean alwaysRefreshList) {
this.alwaysRefreshList = alwaysRefreshList;
}
/**
* Sets the id value.
*
* @param value
* the new id value
*/
public void setIdValue(Object value) {
JKObjectUtil.setPeopertyValue(getEntity(), getEntity().getIdColumn().getFieldName(), value);
}
/**
* Duplicate.
*
* @return the string
*/
public String duplicate() {
entity = JKObjectUtil.cloneBean(getEntity());
setIdValue(0);
return null;
}
/**
* Checks if is reset after save.
*
* @return true, if is reset after save
*/
public boolean isResetAfterSave() {
return resetAfterSave;
}
/**
* Sets the reset after save.
*
* @param resetAfterSave
* the new reset after save
*/
public void setResetAfterSave(boolean resetAfterSave) {
this.resetAfterSave = resetAfterSave;
}
/**
* Gets the empty entity.
*
* @return the empty entity
*/
public JKEntity getEmptyEntity() {
return createEmptyEntity();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy