Package cli_fmw.delegate.directory

Source Code of cli_fmw.delegate.directory.DirectoryMagic$ItemSelectorEditable

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package cli_fmw.delegate.directory;

import cli_fmw.delegate.SharedBean;
import cli_fmw.main.ClipsException;
import cli_fmw.main.DirectoryItemTrasheException;
import cli_fmw.utils.SelectorEditableExceptional;
import framework.beans.directory.DirectoryBean;
import framework.beans.directory.DirectoryBeanRemote;
import framework.beans.directory.DirectoryItemDetails;
import java.util.ArrayList;
import java.util.List;


/**
*
* @param <BEANCLASS>
* @param <ITEMCLASS>
* @author axe
*/

abstract public class DirectoryMagic<
        BEANCLASS extends DirectoryBeanRemote<? extends DirectoryItemDetails>,
        ITEMCLASS extends DirectoryItem<? extends DirectoryItemDetails>
        >extends Directory<ITEMCLASS> {

    private SharedBean<BEANCLASS> bean;
   
    protected DirectoryMagic(String beanName)
            throws ClipsException {
        bean = new SharedBean<BEANCLASS>(beanName);
        bean.init();
        load();
    }

    protected DirectoryMagic(SharedBean<BEANCLASS> bean)
            throws ClipsException {
        this.bean = bean;
        bean.init();
    }

    protected SharedBean<BEANCLASS> getBean() {
        return bean;
    }
   
    protected void clearBean() {
        bean.clear();
    }
           
    @Override
    protected final void addLoadedItem(ITEMCLASS item) throws ClipsException {
        item.setDirectory(this);
        super.addLoadedItem(item);
    }

    public void registerSession() throws ClipsException {
        bean.registerSession();
    }

    @Override
    protected void load() throws ClipsException {
        try {
            List<? extends DirectoryItemDetails>  details = getBean().get().getDirectory();
      ArrayList<ITEMCLASS>    index = new ArrayList<ITEMCLASS>(details.size() + 10);
            for(int i=0; i<details.size(); i++) {
                ITEMCLASS item = createFromLoadedDetails(details.get(i));
        item.setDirectory(this);
                loadExtra(item);
        index.add(item);
        fastAddLoadedItem(item);
                //addLoadedItem(item);
            }
      setDirectoryIndex(index);

            ITEMCLASS itemNull = createNullItem();
            if(itemNull != null) {
                addLoadedItem(itemNull);
            }
            sort();
        } catch (Exception ex) {
            clearBean();
            throw new ClipsException("Ошибка при загрузке справочника '" + getDirectoryTitle() +"'",ex);
        }
    }
   
    protected ITEMCLASS createNullItem() {
        return null;
    }

    protected void loadExtra(ITEMCLASS item) throws  ClipsException {   
        // do nothing
    }

    public boolean canEdit() throws ClipsException {
        return bean.getBeanRights().isCommandAccessible(DirectoryBean.COMMAND_WRITE);
    }

    public boolean canAppend() throws ClipsException {
        return bean.getBeanRights().isCommandAccessible(DirectoryBean.COMMAND_CREATE);
    }
   
    /**
     *
     * @param item
     * @return
     * @throws ClipsException
     */
  @SuppressWarnings("unchecked")
    protected ITEMCLASS appendDB(ITEMCLASS item) throws ClipsException {
        try {
            DirectoryItem item1 = item;
            DirectoryBeanRemote<DirectoryItemDetails> get = (DirectoryBeanRemote<DirectoryItemDetails>) getBean().get();
            DirectoryItemDetails details = item.getDetails();
            DirectoryItemDetails append = get.append(details);
            item1.setDetails(append);
//            item.setDetails(.append(item.getDetails()));
            return (ITEMCLASS) item1;
        } catch(Exception ex) {
            clearBean();
            throw new ClipsException("Ошибка при вставке элемента справочника '"+ getDirectoryTitle() +"'",ex);
        }
    }

    protected void removeDB(ITEMCLASS item) throws ClipsException {
        int result;
        try {
            result = getBean().get().remove(item.getID());
        } catch(Exception ex) {
            clearBean();
            throw new ClipsException("Ошибка при удалении элемента справочника '"
                    + getDirectoryTitle() +"'",ex);
        }
        if (result == DirectoryBean.TRASHE) {
            item.getDetails().hidden = true;
            throw new DirectoryItemTrasheException("Удаление невозможно, элемент помещен в корзину");
        }
    }


 
    /**
     * Возвращает список итемов, кторые могут быть выбранными (Selectable)
     * @return
    */

  @Override
    public SelectorEditableExceptional<ITEMCLASS> getItems() /*throws DirectoryItemNotFoundException*/ {
        return new ItemSelectorEditable();
    }

   
    abstract protected ITEMCLASS createFromLoadedDetails(DirectoryItemDetails details);

    protected void removeFromExtraCache(ITEMCLASS item) {

    }
    protected void addToExtraCache(ITEMCLASS item) {

    }

    protected class ItemSelectorEditable extends ItemSelector
            implements SelectorEditableExceptional<ITEMCLASS> {

       @Override
        public int size() {
            return sortedItems.size();
        }

        @Override
        public ITEMCLASS get(int index) {
            if (index >= size()) {
                throw new IndexOutOfBoundsException("Index is out of range:" + index);
            }
            return sortedItems.get(index);
        }

        @Override
        public void remove(int index) throws ClipsException {
            ITEMCLASS item = get(index);
            removeDB(item);
            removeOrphanItem(item);
            removeFromExtraCache(item);
        }

        @Override
        public void removeByID(int id) throws ClipsException {
            for (int i = 0; i < sortedItems.size(); i++) {
                ITEMCLASS item = sortedItems.get(i);
                if (item.getID() == id) {
                    remove(i);
                }
            }
        }


        @Override
        public void append(ITEMCLASS item) throws ClipsException {
            appendDB(item);
            addLoadedItem(item);
            addToExtraCache(item);
        }

        @Override
        public void remove(ITEMCLASS element) throws ClipsException {
            int index = sortedItems.indexOf(element);
            if(index != -1) {
                remove(index);
            }
        }

        @Override
        public boolean contains(ITEMCLASS element) {
            return sortedItems.contains(element);
        }
    }
}
TOP

Related Classes of cli_fmw.delegate.directory.DirectoryMagic$ItemSelectorEditable

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.