View Javadoc

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