View Javadoc

1   package org.kuali.student.lum.workflow;
2   
3   import java.util.Iterator;
4   import java.util.List;
5   
6   import org.kuali.student.r1.core.statement.dto.ReqCompFieldInfo;
7   import org.kuali.student.r1.core.statement.dto.ReqComponentInfo;
8   import org.kuali.student.r1.core.statement.dto.StatementTreeViewInfo;
9    
10  /**
11   * 
12   * This class contains common utility methods used with statements.
13   * 
14   * @author Kuali Rice Team (kuali-rice@googlegroups.com)
15   *
16   */
17  public class StatementUtil {
18      
19      /**
20       * WARNING: this constant is also declared in ProgramRequirementsSummaryView.  We cannot use
21       * the one from ProgramRequirementsSummaryView because we cannot compile it into this
22       * package.  
23       * 
24       * TODO: create a constants class that can be shared across view, server, etc.
25       */
26      public static final String NEW_STMT_TREE_ID = "NEWSTMTTREE";
27      
28      /**
29       * WARNING: this constant is also declared in ProgramRequirementsSummaryView.  We cannot use
30       * the one from ProgramRequirementsSummaryView because we cannot compile it into this
31       * package.  
32       * 
33       * TODO: create a constants class that can be shared across view, server, etc.
34       */
35      public static final String NEW_REQ_COMP_ID = "NEWREQCOMP";
36  	
37  	/**
38  	 * This method will recursively set the state of all statements in the tree.
39  	 * <p>
40  	 * WARNING: you must call the statement service in order to update statements.
41  	 * <p>
42  	 * 
43  	 * @param state is the state we should set all statements in the tree to
44  	 * @param statementTreeViewInfo the tree of statements
45  	 * @throws Exception
46  	 */
47  	public static void updateStatementTreeViewInfoState(String state, StatementTreeViewInfo statementTreeViewInfo) {
48  		// Set the state on the statement tree itself
49  	    statementTreeViewInfo.setState(state);
50  	     
51  	    // Get all the requirements components for this statement
52          List<ReqComponentInfo> reqComponents = statementTreeViewInfo.getReqComponents();
53          
54          // Loop over requirements and set the state for each requirement
55          for(Iterator<ReqComponentInfo> it = reqComponents.iterator(); it.hasNext();)
56          	it.next().setState(state);
57          
58          // Loop over each statement and set the state for each statement (recursively calling this method)
59          for(Iterator<StatementTreeViewInfo> itr = statementTreeViewInfo.getStatements().iterator(); itr.hasNext();)
60          	updateStatementTreeViewInfoState(state, (StatementTreeViewInfo)itr.next());
61  	}
62   	   /**
63  	    * This method strips the statement ids from the statement tree prior to saving
64  	    * the tree, which allows the web service to generate new ids from the database.
65  	    * <p>
66  	    * The UI needs to create temporary ids in order to work with the statements
67  	    * in memory prior to storing them in the database.  On save, we strip
68  	    * the temporary ids in this method, then write the statement tree to the database,
69  	    * allowing the database to generate proper ids.
70  	    * <p>
71  	    * 
72  	    * @param tree
73  	    */
74   	   public static void stripStatementIds(StatementTreeViewInfo tree) {
75  	        List<StatementTreeViewInfo> statements = tree.getStatements();
76  	        List<ReqComponentInfo> reqComponentInfos = tree.getReqComponents();
77  	        
78  	        // If there are no statements in the tree, set its ID to null
79  	        if ((tree.getId() != null) && (tree.getId().indexOf(NEW_STMT_TREE_ID) >= 0)) {
80  	            tree.setId(null);
81  	        }
82  	        
83  	        // If there are statements in the tree
84  	        if ((statements != null) && (statements.size() > 0)) {
85  	            
86  	            // retrieve all statements, loop over them, and recursively strip their IDs
87   	            for (StatementTreeViewInfo statement : statements) {
88  	                stripStatementIds(statement); // inside set the children of this statementTreeViewInfo
89  	            }
90  	        } else if ((reqComponentInfos != null) && (reqComponentInfos.size() > 0)) {
91  	            // If there are requirements, loop over them and set their IDs to null
92  	            for (ReqComponentInfo reqComponent : reqComponentInfos) {
93  	                if ((reqComponent.getId() != null) && (reqComponent.getId().indexOf(NEW_REQ_COMP_ID) >= 0)) {
94  	                    reqComponent.setId(null);
95  	                }
96  	                // Also set the field IDs to null
97  	                for (ReqCompFieldInfo field : reqComponent.getReqCompFields()) {
98  	                    field.setId(null);
99  	                }
100 	                 
101 	            }
102 	        }
103 	    }
104 }