001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.sys.document.service.impl;
017    
018    import java.util.HashSet;
019    import java.util.List;
020    import java.util.Set;
021    
022    import org.kuali.kfs.sys.businessobject.AccountingLine;
023    import org.kuali.kfs.sys.document.service.AccountingLineRenderingTransformation;
024    import org.kuali.kfs.sys.document.web.ReadOnlyable;
025    import org.kuali.kfs.sys.document.web.RenderableElement;
026    import org.kuali.kfs.sys.document.web.TableJoining;
027    
028    /**
029     * If all the fields of a line are read only, then any actions blocks should be completely removed.
030     */
031    public class AllReadOnlyNoActionsAccountingLineRenderingTransformationImpl implements AccountingLineRenderingTransformation {
032    
033        /**
034         * Traverses through the elements to see if they're all read only; if so, traverses through again and removes any action blocks
035         * @see org.kuali.kfs.sys.document.service.AccountingLineRenderingTransformation#transformElements(java.util.List, org.kuali.kfs.sys.businessobject.AccountingLine)
036         */
037        public void transformElements(List<TableJoining> elements, AccountingLine accountingLine) {
038            if (allReadOnly(elements)) {
039                removeActionBlocks(elements);
040            }
041        }
042        
043        /**
044         * Traverses all elements, determining if all of the elements are read only
045         * @param elements the elements to render
046         * @return true if all elements are read only, false otherwise
047         */
048        protected boolean allReadOnly(List<TableJoining> elements) {
049            for (TableJoining element : elements) {
050                if (element instanceof ReadOnlyable && !((ReadOnlyable)element).isReadOnly()) {
051                    return false;
052                }
053            }
054            return true;
055        }
056        
057        /**
058         * Takes any action blocks out of the line
059         * @param elements the elements which contain action blocks to remove
060         */
061        protected void removeActionBlocks(List<? extends TableJoining> elements) {
062            Set<TableJoining> elementsToRemove = new HashSet<TableJoining>();
063            for (TableJoining element : elements) {
064                element.removeAllActionBlocks();
065                if (element instanceof RenderableElement && ((RenderableElement)element).isActionBlock()) {
066                    elementsToRemove.add(element);
067                }
068            }
069            elements.removeAll(elementsToRemove);
070        }
071    
072    }