Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.prime.shiftlog; import com.kahlon.guard.controller.AbstractPerson; import com.prime.exception.GuardException; import com.prime.exception.MessageEnum; import com.kahlon.guard.service.ImageService; import com.prime.image.Image; import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.PostConstruct; import javax.faces.event.ActionEvent; import javax.faces.view.ViewScoped; import javax.inject.Inject; import javax.inject.Named; import org.apache.commons.io.IOUtils; import org.primefaces.event.FileUploadEvent; import org.primefaces.extensions.component.masterdetail.SelectLevelEvent; import org.primefaces.model.LazyDataModel; /** * Shift Log Manager * * @author prime */ @Named("shiftLog") @ViewScoped public class ShiftLogManager extends AbstractPerson implements Serializable { private static final long serialVersionUID = 9196263323752494128L; @Inject ShiftLogService service; @Inject ImageService imageService; @Inject Logger logger; private ShiftLog shiftLog; private List<Image> images; private LazyDataModel<ShiftLog> lazyModel; private List<ShiftLog> filteredValues; private int counter = 0; //wizard controller private boolean errorOccured = false; private int currentLevel = 1; @PostConstruct public void init() { logger.log(Level.INFO, "Initialize ShiftLog Manager"); lazyModel = new LazyShiftLogDataModel(service); } /** * Handle navigation * * @param selectLevelEvent * @return */ public int handleNavigation(SelectLevelEvent selectLevelEvent) { if (errorOccured) { return 2; } else { return selectLevelEvent.getNewLevel(); } } /** * Reset ShiftLog * * @param event */ public void reset(ActionEvent event) { if (shiftLog != null && shiftLog.getId() != null) { shiftLog = service.find(shiftLog.getId()); } init(); } /** * Create * * @param event */ public void doCreate(ActionEvent event) { shiftLog = new ShiftLog(); images = new ArrayList<>(); } /** * Save * * @param event */ public void doSave(ActionEvent event) { try { if (shiftLog.getId() == null) { service.create(shiftLog); MessageEnum.SUCCESS.toString(); } else { service.update(shiftLog); MessageEnum.UPDATED.toString(); } shiftLog = service.find(shiftLog.getId()); init(); } catch (GuardException e) { logger.log(Level.INFO, e.getMessage()); } } /** * Delete * * @param obj */ public void doDeleteItem(Object obj) { try { if (obj instanceof ShiftLog) { ShiftLog sl = (ShiftLog) obj; service.delete(sl); } MessageEnum.DELETED.toString(); init(); } catch (GuardException e) { logger.log(Level.INFO, e.getMessage()); } } /** * Upload file * * @param event * @throws java.io.IOException */ public void onFileUpload(FileUploadEvent event) throws IOException { byte[] data = IOUtils.toByteArray(event.getFile().getInputstream()); Image image = new Image(); image.setContent(data); try { imageService.createImage(image); images.add(image); shiftLog.setImages(images); } catch (GuardException e) { logger.log(Level.INFO, e.getMessage()); } } //getter and setter public ShiftLog getShiftLog() { return shiftLog; } public void setShiftLog(ShiftLog shiftLog) { if (shiftLog != null && !shiftLog.getImages().isEmpty()) { images = shiftLog.getImages(); } this.shiftLog = shiftLog; } public List<Image> getImages() { return images; } public void setImages(List<Image> images) { this.images = images; } public LazyDataModel<ShiftLog> getLazyModel() { return lazyModel; } public List<ShiftLog> getFilteredValues() { return filteredValues; } public void setFilteredValues(List<ShiftLog> filteredValues) { this.filteredValues = filteredValues; } public boolean isErrorOccured() { return errorOccured; } public void setErrorOccured(boolean errorOccured) { this.errorOccured = errorOccured; } public int getCurrentLevel() { return currentLevel; } public void setCurrentLevel(int currentLevel) { this.currentLevel = currentLevel; } public int getCounter() { return counter++; } public void setCounter(int counter) { this.counter = counter; } }