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.AccountingDocument;
024    import org.kuali.kfs.sys.document.authorization.AccountingLineAuthorizer;
025    import org.kuali.kfs.sys.document.service.AccountingLineAuthorizationTransformer;
026    import org.kuali.kfs.sys.document.web.TableJoining;
027    import org.kuali.rice.kim.bo.Person;
028    import org.kuali.rice.kns.util.GlobalVariables;
029    
030    /**
031     * Like a regular accounting line rendering transformer, though this  
032     */
033    public class AccountingLineAuthorizationTransformerImpl implements AccountingLineAuthorizationTransformer {
034    
035        /**
036         * Performs transformations to the element rendering tree based on the authorization's reactions to the accounting line
037         * @param elements the element rendering tree
038         * @param accountingLine the accounting line to be rendered
039         * @param document the document that accounting line lives on
040         * @param lineAuthorizer the authorizer for the accounting line
041         * @param newLine is this line a new line or a line already on a document?
042         * 
043         */
044        public void transformElements(List<TableJoining> elements, AccountingLine accountingLine, AccountingDocument document, AccountingLineAuthorizer lineAuthorizer, boolean newLine, String accountingLinePropertyName) {
045            final Person currentUser = GlobalVariables.getUserSession().getPerson();
046            removeUnviewableBlocks(elements, lineAuthorizer.getUnviewableBlocks(document, accountingLine, newLine, currentUser));
047        }
048        
049        /**
050         * 
051         * @param elements the elements of the rendering tree
052         * @param unviewableBlocks a Set of the names of blocks that are not viewable
053         */
054        protected void removeUnviewableBlocks(List<TableJoining> elements, Set<String> unviewableBlocks) {
055            if (unviewableBlocks.size() > 0) {
056                Set<TableJoining> elementsToRemove = new HashSet<TableJoining>();
057                for (TableJoining element : elements) {
058                    if (unviewableBlocks.contains(element.getName())) {
059                        elementsToRemove.add(element);
060                    } else {
061                        element.removeUnviewableBlocks(unviewableBlocks);
062                    }
063                }
064                elements.removeAll(elementsToRemove);
065            }
066        }
067    }
068