📄 recordbean.java
字号:
package cn.hxex.library.view.bean;
import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import cn.hxex.library.exception.LibraryException;
import cn.hxex.library.model.Category;
import cn.hxex.library.model.Product;
import cn.hxex.library.model.Record;
import cn.hxex.library.view.util.FacesUtils;
import cn.hxex.library.view.util.ViewUtils;
public class RecordBean extends BaseBean
{
private String id;
private String categoryId;
private List categorys;
private String productId;
private List products;
private Integer num;
private String type;
/**
* @return Returns the id.
*/
public String getId()
{
return id;
}
/**
* @param id The id to set.
*/
public void setId(String id)
{
this.id = id;
System.out.println( "Set ID:\t" + this.id );
}
/**
* @return Returns the categoryId.
*/
public String getCategoryId()
{
return categoryId;
}
/**
* @param categoryId The categoryId to set.
*/
public void setCategoryId(String categoryId)
{
this.categoryId = categoryId;
System.out.println( "Set CategoryID:\t" + this.categoryId );
}
/**
* @return Returns the categorys.
*/
public List getCategorys()
{
return categorys;
}
/**
* @param categorys The categorys to set.
*/
public void setCategorys(List categorys)
{
this.categorys = categorys;
}
/**
* @return Returns the num.
*/
public Integer getNum()
{
return num;
}
/**
* @param num The num to set.
*/
public void setNum(Integer num)
{
this.num = num;
System.out.println( "Set Num:\t" + this.num );
}
/**
* @return Returns the productId.
*/
public String getProductId()
{
return productId;
}
/**
* @param productId The productId to set.
*/
public void setProductId(String productId)
{
this.productId = productId;
System.out.println( "Set ProductID:\t" + this.productId );
}
/**
* @return Returns the products.
*/
public List getProducts()
{
return products;
}
/**
* @param products The products to set.
*/
public void setProducts(List products)
{
this.products = products;
}
/**
* @return Returns the type.
*/
public String getType()
{
return type;
}
/**
* @param type The type to set.
*/
public void setType(String type)
{
this.type = type;
}
public void init()
{
this.logger.info( "====================init()===============" );
this.categorys = this.getServiceLocator().getCategoryService().getAllCategorys();
if( this.categorys.isEmpty() )
return;
if( this.categoryId==null )
{
this.categoryId = ((Category)this.categorys.get( 0 )).getId();
}
if( this.categoryId==null )
return;
Iterator it = this.categorys.iterator();
while( it.hasNext() )
{
Category category = (Category)it.next();
if( this.categoryId.equals( category.getId() ) )
{
this.products = ViewUtils.convertToList( category.getProducts() );
break;
}
}
if( this.productId==null && !this.products.isEmpty() )
{
this.productId = ( (Product)this.products.get(0) ).getId();
}
}
public SelectItem[] getCategoryItems()
{
this.logger.info( "getCategoryItems was invoked!" );
SelectItem[] items = new SelectItem[this.getCategorys().size()];
for( int i=0; i<this.getCategorys().size(); i++ )
{
Category category = (Category)this.getCategorys().get( i );
items[i] = new SelectItem( category.getId(), category.getName() );
}
return items;
}
public SelectItem[] getProductItems()
{
this.logger.info( "getProductItems was invoked!" );
SelectItem[] items = new SelectItem[this.getProducts().size()];
for( int i=0; i<this.getProducts().size(); i++ )
{
Product product = (Product)this.getProducts().get( i );
items[i] = new SelectItem( product.getId(), product.getName() );
}
return items;
}
public SelectItem[] getRecordTypeItems()
{
this.logger.info( "getRecordTypeItems was invoked!" );
SelectItem[] items = new SelectItem[2];
items[0] = new SelectItem( "in", "in" );
items[1] = new SelectItem( "out", "out" );
return items;
}
public void changeCategory( ValueChangeEvent event )
{
this.logger.info( "changeCategory was invoked!" );
this.categoryId = String.valueOf( event.getNewValue() );
if( this.categoryId==null )
return;
Iterator it = this.categorys.iterator();
while( it.hasNext() )
{
Category category = (Category)it.next();
if( this.categoryId.equals( category.getId() ) )
{
this.products = ViewUtils.convertToList( category.getProducts() );
break;
}
}
System.out.println( "productId:\t" + this.productId );
if( !this.products.isEmpty() )
{
this.productId = ( (Product)this.products.get(0) ).getId();
System.out.println( "productId:\t" + this.productId );
}
}
public String createAction()
{
Record record = convertToRecord();
try
{
Product product = this.getServiceLocator().getProductService().getProduct( this.productId );
record.setProduct( product );
this.getServiceLocator().getRecordService().saveRecord( record );
FacesUtils.resetManagedBean( BeanNames.RECORD_BEAN );
FacesUtils.addInfoMessageById( "recordBean_create_record_success" );
}
catch( LibraryException ex )
{
FacesUtils.addErrorMessageById( ex.getMessage() );
}
return NavigationResults.SUCCESS;
}
public String editAction()
{
if( this.id!=null )
{
try
{
Record record = this.getServiceLocator().getRecordService().getRecord( this.id );
convertToRecordBean( this, record );
return NavigationResults.RECORD_EDIT;
}
catch( LibraryException ex )
{
FacesUtils.addErrorMessageById( ex.getMessage() );
}
}
return NavigationResults.RECORD_LIST;
}
public String updateAction()
{
try
{
Record record = convertToRecord();
Product product = this.getServiceLocator().getProductService().getProduct( this.productId );
record.setProduct( product );
this.getServiceLocator().getRecordService().updateRecord( record );
FacesUtils.addInfoMessageById("recordBean_update_record_success");
// FacesUtils.resetManagedBean( BeanNames.RECORD_LIST_BEAN );
RecordListBean recordListBean = (RecordListBean)FacesUtils.getManagedBean( BeanNames.RECORD_LIST_BEAN );
recordListBean.setProductId( this.productId );
recordListBean.init();
return NavigationResults.RECORD_LIST;
}
catch( LibraryException ex )
{
FacesUtils.addErrorMessageById( ex.getMessage() );
return NavigationResults.RETRY;
}
}
public String deleteAction()
{
try
{
Record record = convertToRecord();
this.getServiceLocator().getRecordService().deleteRecord( record );
FacesUtils.resetManagedBean( BeanNames.RECORD_LIST_BEAN );
FacesUtils.addErrorMessageById( "recordBean_delete_record_success" );
}
catch( LibraryException ex )
{
FacesUtils.addErrorMessageById( ex.getMessage() );
}
return NavigationResults.RECORD_LIST;
}
private Record convertToRecord()
{
Record record = new Record();
record.setId( this.id );
if( this.num!=null )
{
int number = this.num.intValue();
if( "out".equals( this.type ) )
number = -number;
record.setNum( number );
}
record.setRecordtime( new Timestamp( System.currentTimeMillis() ) );
return record;
}
private RecordBean convertToRecordBean( RecordBean recordBean, Record record )
{
recordBean.setId( record.getId() );
recordBean.setNum( Math.abs( record.getNum() ) );
recordBean.setType( record.getNum() > 0 ? "in" : "out" );
recordBean.setProductId( record.getProduct().getId() );
recordBean.setCategoryId( record.getProduct().getCategory().getId() );
recordBean.setProducts( ViewUtils.convertToList( record.getProduct().getCategory().getProducts() ) );
return recordBean;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -