View Javadoc

1   package com.germinus.merlin.controller.assignments;
2   
3   import java.util.Map;
4   
5   import org.apache.commons.logging.Log;
6   import org.apache.commons.logging.LogFactory;
7   import javax.portlet.ActionRequest;
8   import javax.portlet.ActionResponse;
9   import javax.portlet.RenderRequest;
10  import javax.portlet.RenderResponse;
11  
12  import org.springframework.beans.factory.InitializingBean;
13  import org.springframework.web.portlet.ModelAndView;
14  import org.springframework.web.portlet.mvc.AbstractController;
15  
16  import com.germinus.liferay.util.ILiferayUtil;
17  import com.germinus.merlin.manager.AssignmentManager;
18  import com.germinus.merlin.manager.CourseManager;
19  import com.germinus.merlin.model.Course;
20  import com.germinus.merlin.util.IMerlinUtil;
21  import com.liferay.portal.PortalException;
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.search.SearchContainer;
24  
25  /**
26   * Controller to show the adequate content in the page add assignment.
27   * @author Rui Quintas
28   * @version 1.0
29   * @since 1.0
30   * @author Jesús Rodríguez Martínez Gérminus XXI
31   * @version 2.0
32   * @since 2.0
33   */
34  public class AssignmentAddController extends AbstractController implements
35  		InitializingBean {
36  	
37  	private ILiferayUtil liferayUtil;
38  	private IMerlinUtil merlinUtil;
39  	
40  	private AssignmentManager assignmentManager;
41  	private CourseManager courseManager;
42  	
43  	private static final Log log = LogFactory.getLog(AssignmentAddController.class);
44  
45  	public void afterPropertiesSet() throws Exception {
46  	}
47  	
48  	/**
49  	 * Process the action request. There is nothing to return.
50  	 * @param request
51  	 * @param response
52  	 * @throws Exception
53  	 */
54  	public void handleActionRequestInternal(ActionRequest request,
55  							ActionResponse response) throws Exception {
56  		log.debug("Add Assignment to the model");
57  		//get the course with the groupId of the current community
58  		long courseId = liferayUtil.getComunityGroupId(request, response); 
59  		Course course = courseManager.getCourse(courseId);
60  		
61  //		log.debug("Fecha Inicio: "+request.getParameter("startDate"));
62  //		log.debug("Fecha Final: "+request.getParameter("endDate"));
63  //		log.debug("Fecha Final: "+request.getAttribute("endDate"));
64  		
65  		assignmentManager.addAssignment(request,response,course);
66  		
67  		//handleRenderRequestInternal with this parameter knows if it have to show the add
68  		//assignment form, or the assignmentlist
69  		response.setRenderParameter("fin", "fin");
70  	
71      	log.debug("End Add Assignment to the model");
72  	}
73  
74  	/**
75  	 * Get the type of assignment and save it in the request.
76  	 * Process the render request and return a ModelAndView object which the
77  	 * DispatcherPortlet will render.
78  	 * @param request
79  	 * @param response
80  	 * @throws Exception
81  	 * @return ModelAndView
82  	 */
83  	public ModelAndView handleRenderRequestInternal(RenderRequest request,
84  										RenderResponse response) throws Exception {
85  		log.debug("Add Assignment Form or Assignments List");
86  		
87  		long courseId = liferayUtil.getComunityGroupId(request, response); 
88  		//get the user type: student or teacher (another->error)
89  		String usertype= merlinUtil.getUserType(request, response, courseId);
90  		
91  		if (usertype!="teacher"){ 
92  			//security (an user can write ulr)
93  			return new ModelAndView("errors/notAuthorized", "", null);
94  		}
95  		
96  		if (request.getParameter("fin")!=null && request.getParameter("fin")=="fin"){
97  			log.debug("Going to Assignments List");
98  			//the assignment is added to the system
99  			return goToAssignmentList(request, response, courseId, usertype);
100 			
101 		}
102 		else { 
103 			log.debug("Going to Add Assignments Form");
104 			//select the form to add the assignment:
105 			return goToAddAssignmentForm(request);
106 		}
107 	}
108 
109 	private ModelAndView goToAddAssignmentForm(RenderRequest request) {
110 		//get the type of assignment and save it in the request
111 		String assignmentType = request.getParameter("type");
112 		request.setAttribute("param", assignmentType);
113 
114 		if (assignmentType.compareTo("Online Activity")==0){
115 			return new ModelAndView("assignments/addAssignment/addAssignmentOnline", "", null);
116 		}else
117 			if (assignmentType.compareTo("Upload a Single File")==0){
118 				return new ModelAndView("assignments/addAssignment/addAssignmentUploadSingleFile", "", null);
119 			}else
120 				if (assignmentType.compareTo("Offline Activity")==0){
121 					return new ModelAndView("assignments/addAssignment/addAssignmentOffline", "", null);
122 				} else 
123 					return new ModelAndView("errors/error", "", null);
124 	}
125 
126 	private ModelAndView goToAssignmentList(RenderRequest request,
127 			RenderResponse response, long courseId, String usertype)
128 			throws PortalException, SystemException {
129 		
130 		Course course = courseManager.getCourse(courseId);
131 
132 		
133 		Map<String, SearchContainer> model ;
134 		model= assignmentManager.listAssignment(request, response, course, usertype);
135 		
136 		if (usertype=="teacher"){
137 			return new ModelAndView("assignments/listAssignment/teacherAssignmentsList", "model", model);
138 		}
139 		else
140 			if (usertype=="student"){
141 				return new ModelAndView("assignments/listAssignment/studentAssignmentsList", "model", model);
142 			}
143 			else //guest user, user without teacher or student role
144 					return new ModelAndView("errors/notAuthorized", "", null);
145 	}
146 	
147 	/**
148 	 * @return the liferayUtil
149 	 */
150 	public ILiferayUtil getLiferayUtil() {
151 		return liferayUtil;
152 	}
153 
154 	/**
155 	 * @param liferayUtil the liferayUtil to set
156 	 */
157 	public void setLiferayUtil(ILiferayUtil liferayUtil) {
158 		this.liferayUtil = liferayUtil;
159 	}
160 
161 	/**
162 	 * @return the merlinUtil
163 	 */
164 	public IMerlinUtil getMerlinUtil() {
165 		return merlinUtil;
166 	}
167 
168 	/**
169 	 * @param merlinUtil the merlinUtil to set
170 	 */
171 	public void setMerlinUtil(IMerlinUtil merlinUtil) {
172 		this.merlinUtil = merlinUtil;
173 	}
174 
175 
176 	public AssignmentManager getAssignmentManager() {
177 		return assignmentManager;
178 	}
179 
180 
181 	public void setAssignmentManager(AssignmentManager assignmentManager) {
182 		this.assignmentManager = assignmentManager;
183 	}
184 
185 
186 	public CourseManager getCourseManager() {
187 		return courseManager;
188 	}
189 
190 
191 	public void setCourseManager(CourseManager courseManager) {
192 		this.courseManager = courseManager;
193 	}
194 
195 }