001    package org.kuali.student.lum.lu.ui.dependency.server;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import org.kuali.student.r2.common.dto.ContextInfo;
007    import org.kuali.student.r2.common.util.ContextUtils;
008    import org.kuali.student.r1.core.statement.dto.ReqComponentInfo;
009    import org.kuali.student.r1.core.statement.dto.StatementTreeViewInfo;
010    import org.kuali.student.r1.core.statement.service.StatementService;
011    import org.kuali.student.core.statement.ui.client.widgets.rules.ReqComponentInfoUi;
012    import org.kuali.student.core.statement.ui.client.widgets.rules.RulesUtil;
013    import org.kuali.student.lum.lu.ui.dependency.client.service.DependencyAnalysisRpcService;
014    import org.kuali.student.r2.lum.program.dto.ProgramRequirementInfo;
015    import org.kuali.student.r2.lum.program.service.ProgramService;
016    
017    import com.google.gwt.user.server.rpc.RemoteServiceServlet;
018    
019    public class DependencyAnalysisRpcServlet extends RemoteServiceServlet implements DependencyAnalysisRpcService {
020    
021        private static final long serialVersionUID = 1L;
022    
023        StatementService statementService;
024        ProgramService programService;
025    
026        @Override
027        public String getRequirementComponentNL(String reqComponentId) {
028            return null;
029        }
030    
031        @Override
032        public List<String> getRequirementComponentNL(List<String> reqComponentIds) throws Exception {
033            List<String> reqComponents = new ArrayList<String>();
034            try
035            {
036            for (String reqComponentId : reqComponentIds) {
037                String reqCompNL = statementService.getNaturalLanguageForReqComponent(reqComponentId, "KUALI.RULE", "en");
038                reqComponents.add(reqCompNL);
039            }
040    
041            return reqComponents;
042            }
043            catch(Exception ex){
044                // Log exception 
045                ex.printStackTrace();
046                throw new RuntimeException(ex);
047            }
048        }
049    
050        @Override
051        public ProgramRequirementInfo getProgramRequirement(String reqId) throws Exception {
052            //TODO fix null parameters?
053            try
054            {
055                ProgramRequirementInfo programRequirementInfo = programService.getProgramRequirement(reqId,
056                        ContextUtils.getContextInfo());
057    
058                setProgReqNL(programRequirementInfo);
059                return programRequirementInfo;
060            } catch (Exception ex) {
061                // Log exception 
062                ex.printStackTrace();
063                throw new RuntimeException(ex);
064            }
065        }
066    
067        public StatementService getStatementService() {
068            return statementService;
069        }
070    
071        public void setStatementService(StatementService statementService) {
072            this.statementService = statementService;
073        }
074    
075        public ProgramService getProgramService() {
076            return programService;
077        }
078    
079        public void setProgramService(ProgramService programService) {
080            this.programService = programService;
081        }
082    
083        private void setProgReqNL(ProgramRequirementInfo programRequirementInfo) throws Exception {
084            setReqCompNL(programRequirementInfo.getStatement());
085        }
086    
087        private void setReqCompNL(StatementTreeViewInfo tree) throws Exception {
088            List<StatementTreeViewInfo> statements = tree.getStatements();
089            List<ReqComponentInfo> reqComponentInfos = tree.getReqComponents();
090    
091            if ((statements != null) && (statements.size() > 0)) {
092                // retrieve all statements
093                for (StatementTreeViewInfo statement : statements) {
094                    setReqCompNL(statement); // inside set the children of this statementTreeViewInfo
095                }
096            } else if ((reqComponentInfos != null) && (reqComponentInfos.size() > 0)) {
097                // retrieve all req. component LEAFS
098                for (int i = 0; i < reqComponentInfos.size(); i++) {
099                    ReqComponentInfoUi reqUi = RulesUtil.clone(reqComponentInfos.get(i));
100                    reqUi.setNaturalLanguageTranslation(statementService.translateReqComponentToNL(reqUi, "KUALI.RULE",
101                            "en"));
102                    reqUi.setPreviewNaturalLanguageTranslation(statementService.translateReqComponentToNL(reqUi,
103                            "KUALI.RULE.PREVIEW", "en"));
104                    reqComponentInfos.set(i, reqUi);
105                }
106            }
107        }
108    
109    }