001package org.kuali.student.lum.common.server;
002
003import java.util.Iterator;
004import java.util.List;
005
006import org.kuali.student.r1.core.statement.dto.ReqCompFieldInfo;
007import org.kuali.student.r1.core.statement.dto.ReqComponentInfo;
008import org.kuali.student.r1.core.statement.dto.StatementTreeViewInfo;
009 
010/**
011 * 
012 * This class contains common utility methods used with statements.
013 * 
014 * @author Kuali Rice Team (kuali-rice@googlegroups.com)
015 *
016 */
017public class StatementUtil {
018    
019    /**
020     * WARNING: this constant is also declared in ProgramRequirementsSummaryView.  We cannot use
021     * the one from ProgramRequirementsSummaryView because we cannot compile it into this
022     * package.  
023     * 
024     * TODO: create a constants class that can be shared across view, server, etc.
025     */
026    public static final String NEW_STMT_TREE_ID = "NEWSTMTTREE";
027    
028    /**
029     * WARNING: this constant is also declared in ProgramRequirementsSummaryView.  We cannot use
030     * the one from ProgramRequirementsSummaryView because we cannot compile it into this
031     * package.  
032     * 
033     * TODO: create a constants class that can be shared across view, server, etc.
034     */
035    public static final String NEW_REQ_COMP_ID = "NEWREQCOMP";
036        
037        /**
038         * This method will recursively set the state of all statements in the tree.
039         * <p>
040         * WARNING: you must call the statement service in order to update statements.
041         * <p>
042         * 
043         * @param state is the state we should set all statements in the tree to
044         * @param statementTreeViewInfo the tree of statements
045         * @throws Exception
046         */
047        public static void updateStatementTreeViewInfoState(String state, StatementTreeViewInfo statementTreeViewInfo) {
048                // Set the state on the statement tree itself
049            statementTreeViewInfo.setState(state);
050             
051            // Get all the requirements components for this statement
052        List<ReqComponentInfo> reqComponents = statementTreeViewInfo.getReqComponents();
053        
054        // Loop over requirements and set the state for each requirement
055        for(Iterator<ReqComponentInfo> it = reqComponents.iterator(); it.hasNext();)
056                it.next().setState(state);
057        
058        // Loop over each statement and set the state for each statement (recursively calling this method)
059        for(Iterator<StatementTreeViewInfo> itr = statementTreeViewInfo.getStatements().iterator(); itr.hasNext();)
060                updateStatementTreeViewInfoState(state, (StatementTreeViewInfo)itr.next());
061        }
062           /**
063            * This method strips the statement ids from the statement tree prior to saving
064            * the tree, which allows the web service to generate new ids from the database.
065            * <p>
066            * The UI needs to create temporary ids in order to work with the statements
067            * in memory prior to storing them in the database.  On save, we strip
068            * the temporary ids in this method, then write the statement tree to the database,
069            * allowing the database to generate proper ids.
070            * <p>
071            * 
072            * @param tree
073            */
074           public static void stripStatementIds(StatementTreeViewInfo tree) {
075                List<StatementTreeViewInfo> statements = tree.getStatements();
076                List<ReqComponentInfo> reqComponentInfos = tree.getReqComponents();
077                
078                // If there are no statements in the tree, set its ID to null
079                if ((tree.getId() != null) && (tree.getId().indexOf(NEW_STMT_TREE_ID) >= 0)) {
080                    tree.setId(null);
081                }
082                
083                // If there are statements in the tree
084                if ((statements != null) && (statements.size() > 0)) {
085                    
086                    // retrieve all statements, loop over them, and recursively strip their IDs
087                    for (StatementTreeViewInfo statement : statements) {
088                        stripStatementIds(statement); // inside set the children of this statementTreeViewInfo
089                    }
090                } else if ((reqComponentInfos != null) && (reqComponentInfos.size() > 0)) {
091                    // If there are requirements, loop over them and set their IDs to null
092                    for (ReqComponentInfo reqComponent : reqComponentInfos) {
093                        if ((reqComponent.getId() != null) && (reqComponent.getId().indexOf(NEW_REQ_COMP_ID) >= 0)) {
094                            reqComponent.setId(null);
095                        }
096                        // Also set the field IDs to null
097                        for (ReqCompFieldInfo field : reqComponent.getReqCompFields()) {
098                            field.setId(null);
099                        }
100                         
101                    }
102                }
103            }
104}