001    /**
002     * 
003     */
004    package org.mycore.datamodel.classifications2.impl;
005    
006    import java.util.ArrayList;
007    import java.util.Collection;
008    
009    import org.mycore.datamodel.classifications2.MCRCategory;
010    
011    class MCRCategoryChildList extends ArrayList<MCRCategory> {
012        private static final long serialVersionUID = 5844882597476033744L;
013    
014        private MCRCategory root;
015    
016        private MCRCategory thisCategory;
017    
018        /**
019         * @param root
020         * @param thisCategory
021         */
022        public MCRCategoryChildList(MCRCategory root, MCRCategory thisCategory) {
023            super();
024            this.root = root;
025            this.thisCategory = thisCategory;
026        }
027    
028        @Override
029        public void add(int index, MCRCategory element) {
030            super.add(index, MCRCategoryImpl.wrapCategory(element, thisCategory, root));
031        }
032    
033        @Override
034        public boolean add(MCRCategory e) {
035            return super.add(MCRCategoryImpl.wrapCategory(e, thisCategory, root));
036        }
037    
038        @Override
039        public boolean addAll(Collection<? extends MCRCategory> c) {
040            return super.addAll(MCRCategoryImpl.wrapCategories(c, thisCategory, root));
041        }
042    
043        @Override
044        public boolean addAll(int index, Collection<? extends MCRCategory> c) {
045            return super.addAll(index, MCRCategoryImpl.wrapCategories(c, thisCategory, root));
046        }
047    
048        @Override
049        public void clear() {
050            for (int i = 0; i < size(); i++) {
051                removeAncestorReferences(get(i));
052            }
053            super.clear();
054        }
055    
056        @Override
057        public MCRCategory remove(int index) {
058            MCRCategory category = super.remove(index);
059            removeAncestorReferences(category);
060            return category;
061        }
062    
063        @Override
064        public boolean remove(Object o) {
065            boolean removed = super.remove(o);
066            if (removed) {
067                removeAncestorReferences((MCRCategory) o);
068            }
069            return removed;
070        }
071    
072        /**
073         * @param category
074         */
075        private void removeAncestorReferences(MCRCategory category) {
076            if (category instanceof MCRAbstractCategoryImpl) {
077                ((MCRAbstractCategoryImpl) category).parent = null;
078                ((MCRAbstractCategoryImpl) category).root = null;
079            }
080        }
081    
082        @Override
083        protected void removeRange(int fromIndex, int toIndex) {
084            for (int i = fromIndex; i < toIndex; i++) {
085                removeAncestorReferences(get(i));
086            }
087            super.removeRange(fromIndex, toIndex);
088        }
089    
090        @Override
091        public MCRCategory set(int index, MCRCategory element) {
092            MCRCategory category = super.set(index, element);
093            if (category != element) {
094                removeAncestorReferences(category);
095            }
096            return category;
097        }
098    
099    }