PageRenderTime 33ms CodeModel.GetById 17ms app.highlight 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/java/server/web/BeslagslisterController.java

https://bitbucket.org/mgyth/server2
Java | 307 lines | 245 code | 39 blank | 23 comment | 19 complexity | 7240a0b26c0ed2c4799410b7e35c5bcd MD5 | raw file
  1package server.web;
  2
  3import java.io.Serializable;
  4import java.util.ResourceBundle;
  5import java.util.logging.Logger;
  6import javax.ejb.EJB;
  7import javax.faces.application.FacesMessage;
  8import javax.faces.bean.ManagedBean;
  9import javax.faces.bean.SessionScoped;
 10import javax.faces.component.UIComponent;
 11import javax.faces.context.FacesContext;
 12import javax.faces.convert.Converter;
 13import javax.faces.convert.FacesConverter;
 14import javax.faces.event.ActionEvent;
 15import javax.faces.model.DataModel;
 16import javax.faces.model.ListDataModel;
 17import javax.faces.model.SelectItem;
 18import server.ejb.BeslagslisterFacade;
 19import server.entity.Beslagslister;
 20import server.entity.Doerer;
 21import server.web.util.JsfUtil;
 22import server.web.util.PaginationHelper;
 23import server.web.util.SerializableListDataModel;
 24
 25@ManagedBean(name = "beslagslisterController")
 26@SessionScoped
 27public class BeslagslisterController implements Serializable {
 28
 29    private Beslagslister current;
 30    private Beslagslister currentAjax;
 31    private SerializableListDataModel items = null;
 32    @EJB
 33    private server.ejb.BeslagslisterFacade ejbFacade;
 34    private PaginationHelper pagination;
 35    private int selectedItemIndex;
 36
 37    public BeslagslisterController() {
 38    }
 39    
 40    public void prepareDelete(Beslagslister beslag){
 41        this.currentAjax = beslag;
 42    }
 43
 44    public Beslagslister getSelected() {
 45        if (current == null) {
 46            current = new Beslagslister();
 47            selectedItemIndex = -1;
 48        }
 49        return current;
 50    }
 51
 52    private BeslagslisterFacade getFacade() {
 53        return ejbFacade;
 54    }
 55
 56    public void createBeslagFromFile(Beslagslister beslag) {
 57        getFacade().create(beslag);
 58    }
 59
 60    public PaginationHelper getPagination() {
 61        if (pagination == null) {
 62            pagination = new PaginationHelper(10) {
 63                @Override
 64                public int getItemsCount() {
 65                    return getFacade().count();
 66                }
 67
 68                @Override
 69                public SerializableListDataModel createPageDataModel() {
 70                    return new SerializableListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
 71                }
 72            };
 73        }
 74        return pagination;
 75    }
 76
 77    public String prepareList() {
 78        recreateModel();
 79        return "/beslagslister/List";
 80    }
 81
 82    public String prepareView() {
 83        current = (Beslagslister) getItems().getRowData();
 84        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
 85        return "/beslagslister/View";
 86    }
 87    
 88    public void prepareViewProsjekt(Beslagslister beslag) {
 89        current = beslag;
 90        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
 91
 92    }
 93
 94    public String prepareCreate() {
 95        current = new Beslagslister();
 96        selectedItemIndex = -1;
 97        return "/beslagslister/Create";
 98    }
 99
100    public String create() {
101        try {
102            getFacade().create(current);
103            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Database: ", ResourceBundle.getBundle("/Bundle").getString("BeslagslisterUpdated")));
104            return prepareCreate();
105        } catch (Exception e) {
106            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Database: ", ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured") + " " + e));
107            return null;
108        }
109    }
110    
111    public void prepareCreateAjax(Doerer doren) {
112        current = new Beslagslister();
113        current.setDoridfk(doren);
114        doren.addBeslagslisterCollection(current);
115        selectedItemIndex = -1;
116    }
117
118    public void createAjax() {
119        try {
120            getFacade().create(current);
121            recreateModel();
122//            current.getDoridfk().getBeslagslisterCollection().add(current);
123            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Database: ", ResourceBundle.getBundle("/Bundle").getString("BeslagslisterUpdated")));
124        } catch (Exception e) {
125            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Database: ", ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured") + " " + e));
126        }
127    }
128    
129    public void newProjectView() {
130        current = null;
131    }
132
133    public String prepareEdit() {
134        current = (Beslagslister) getItems().getRowData();
135        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
136        return "/beslagslister/Edit";
137    }
138    
139    public void updateDialog() {
140        try {
141            getFacade().edit(current);
142            recreateModel();
143            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Database: ", ResourceBundle.getBundle("/Bundle").getString("DoererUpdated")));
144        } catch (Exception e) {
145            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Database: ", ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured") + " " + e));
146        }
147    }
148
149    public String update() {
150        try {
151            getFacade().edit(current);
152            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Database: ", ResourceBundle.getBundle("/Bundle").getString("BeslagslisterUpdated")));
153            return "/beslagslister/View";
154        } catch (Exception e) {
155            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Database: ", ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured") + " " + e));
156            return null;
157        }
158    }
159
160    /**
161     * Sletter
162     * beslaget,
163     * setter
164     * index
165     * til
166     * -1
167     * og
168     * ombygger
169     * pagineringen
170     * og
171     * modellen
172     *
173     * @param
174     * Beslagslister
175     * som
176     * skal
177     * slettes
178     */
179    public void destroyAjax(Beslagslister beslag) {
180        current = beslag;
181        performDestroy();
182        beslag.getDoridfk().getBeslagslisterCollection().remove(beslag);
183        recreatePagination();
184        recreateModel();
185    }
186
187    public String destroy() {
188        current = (Beslagslister) getItems().getRowData();
189        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
190        performDestroy();
191        recreatePagination();
192        recreateModel();
193        return "/beslagslister/List";
194    }
195
196    public String destroyAndView() {
197        performDestroy();
198        recreateModel();
199        updateCurrentItem();
200        if (selectedItemIndex >= 0) {
201            return "/beslagslister/View";
202        } else {
203            // all items were removed - go back to list
204            recreateModel();
205            return "/beslagslister/List";
206        }
207    }
208
209    private void performDestroy() {
210        try {
211            getFacade().remove(current);
212            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Database: ", ResourceBundle.getBundle("/Bundle").getString("BeslagslisterDeleted")));
213        } catch (Exception e) {
214            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Database: ", ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured") + " " + e));
215        }
216    }
217
218    private void updateCurrentItem() {
219        int count = getFacade().count();
220        if (selectedItemIndex >= count) {
221            // selected index cannot be bigger than number of items:
222            selectedItemIndex = count - 1;
223            // go to previous page if last page disappeared:
224            if (pagination.getPageFirstItem() >= count) {
225                pagination.previousPage();
226            }
227        }
228        if (selectedItemIndex >= 0) {
229            current = getFacade().findRange(new int[]{selectedItemIndex, selectedItemIndex + 1}).get(0);
230        }
231    }
232
233    public DataModel getItems() {
234        if (items == null) {
235            items = getPagination().createPageDataModel();
236        }
237        return items;
238    }
239
240    private void recreateModel() {
241        items = null;
242    }
243
244    private void recreatePagination() {
245        pagination = null;
246    }
247
248    public String next() {
249        getPagination().nextPage();
250        recreateModel();
251        return "/beslagslister/List";
252    }
253
254    public String previous() {
255        getPagination().previousPage();
256        recreateModel();
257        return "/beslagslister/List";
258    }
259
260    public SelectItem[] getItemsAvailableSelectMany() {
261        return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
262    }
263
264    public SelectItem[] getItemsAvailableSelectOne() {
265        return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
266    }
267
268    @FacesConverter(forClass = Beslagslister.class)
269    public static class BeslagslisterControllerConverter implements Converter {
270
271        @Override
272        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
273            if (value == null || value.length() == 0) {
274                return null;
275            }
276            BeslagslisterController controller = (BeslagslisterController) facesContext.getApplication().getELResolver().
277                    getValue(facesContext.getELContext(), null, "beslagslisterController");
278            return controller.ejbFacade.find(getKey(value));
279        }
280
281        java.lang.Integer getKey(String value) {
282            java.lang.Integer key;
283            key = Integer.valueOf(value);
284            return key;
285        }
286
287        String getStringKey(java.lang.Integer value) {
288            StringBuilder sb = new StringBuilder();
289            sb.append(value);
290            return sb.toString();
291        }
292
293        @Override
294        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
295            if (object == null) {
296                return null;
297            }
298            if (object instanceof Beslagslister) {
299                Beslagslister o = (Beslagslister) object;
300                return getStringKey(o.getIDBeslagsliste());
301            } else {
302                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Beslagslister.class.getName());
303            }
304        }
305    }
306    private static final Logger LOG = Logger.getLogger(BeslagslisterController.class.getName());
307}