/* * Created on 11:36:06,28/Fev/2005 * * Author: Goncalo Luiz (goncalo@ist.utl.pt) * */ package net.sourceforge.fenixedu.presentationTier.Action.externalServices; import java.io.IOException; import java.util.Collection; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sourceforge.fenixedu._development.PropertiesManager; import net.sourceforge.fenixedu.applicationTier.IUserView; import net.sourceforge.fenixedu.applicationTier.Filtro.exception.FenixFilterException; import net.sourceforge.fenixedu.applicationTier.Servico.exceptions.FenixServiceException; import net.sourceforge.fenixedu.applicationTier.Servico.externalServices.ReadStudentGroupsExternalInformationByExecutionCourseIDAndStudentUsername; import net.sourceforge.fenixedu.framework.factory.ServiceManagerServiceFactory; import net.sourceforge.fenixedu.presentationTier.Action.BaseAuthenticationAction; import net.sourceforge.fenixedu.presentationTier.Action.base.FenixAction; import net.sourceforge.fenixedu.presentationTier.Action.exceptions.FenixActionException; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.linkare.commons.metainfo.Linkare; import com.thoughtworks.xstream.XStream; /** * @author Goncalo Luiz * * Created at 11:36:06, 28/Fev/2005 */ public class InfoGroupsByExecutionCourse extends FenixAction { private final String SEPARATOR = ","; //$NON-NLS-1$ private String studentUsername; private String studentPassword; private Integer[] executionCourseIds; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws FenixActionException { this.readRequestInformation(request); StringBuilder buffer = new StringBuilder(); try { final String requestURL = request.getRequestURL().toString(); final String remoteHostName = BaseAuthenticationAction.getRemoteHostName(request); IUserView userView = authenticate(this.studentUsername, this.studentPassword, requestURL, remoteHostName); for (int i = 0; i < this.executionCourseIds.length; i++) { buffer.append(this.buildInfo(this.executionCourseIds[i], userView)); } if (buffer.toString().equals("")) buffer = new StringBuilder("-1"); } catch (Exception e) { buffer = new StringBuilder(); buffer = new StringBuilder(); buffer.append(""); buffer.append(e.getMessage()); buffer.append(""); } try { sendAnswer(response, buffer.toString()); } catch (IOException ex) { throw new FenixActionException(); } return null; } private String buildInfo(Integer integer, IUserView userView) throws FenixFilterException, FenixServiceException { Collection info = (Collection) ReadStudentGroupsExternalInformationByExecutionCourseIDAndStudentUsername.run(integer, userView.getUtilizador()); //$NON-NLS-1$ XStream xstream = new XStream(); String data = xstream.toXML(info); return data; } /** * @param response * @param result * @throws IOException */ private void sendAnswer(HttpServletResponse response, String result) throws IOException { ServletOutputStream writer = response.getOutputStream(); response.setContentType("text/plain"); writer.print(result); writer.flush(); response.flushBuffer(); } /** * @param request * */ private void readRequestInformation(HttpServletRequest request) { this.studentUsername = request.getParameter("username"); //$NON-NLS-1$ this.studentPassword = request.getParameter("password"); //$NON-NLS-1$ String idsString = request.getParameter("ids"); //$NON-NLS-1$ this.executionCourseIds = buildIdsArray(idsString); } private IUserView authenticate(String username, String password, String requestURL, String remoteHostName) throws FenixServiceException, FenixFilterException { Object argsAutenticacao[] = { username, password, requestURL, remoteHostName }; IUserView userView = (IUserView) ServiceManagerServiceFactory.executeService(PropertiesManager .getProperty("authenticationService"), argsAutenticacao); return userView; } @Linkare(author = "Paulo Zenida", comments = "Used Integer.valueOf() instead of new Integer()") private Integer[] buildIdsArray(String idsString) { Integer[] coursesIds = { Integer.valueOf(34811), Integer.valueOf(34661), Integer.valueOf(34950) }; if (idsString != null) { String[] ids = idsString.split(this.SEPARATOR); coursesIds = new Integer[ids.length]; for (int i = 0; i < ids.length; i++) coursesIds[i] = Integer.valueOf(ids[i]); } return coursesIds; } }