org.muse.mneme.tool.AssessmentSpecialView.java Source code

Java tutorial

Introduction

Here is the source code for org.muse.mneme.tool.AssessmentSpecialView.java

Source

/**********************************************************************************
 * $URL$
 * $Id$
 ***********************************************************************************
 *
 * Copyright (c) 2007 The Regents of the University of Michigan & Foothill College, ETUDES Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **********************************************************************************/

package org.muse.mneme.tool;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.muse.ambrosia.api.Context;
import org.muse.ambrosia.api.Values;
import org.muse.ambrosia.util.ControllerImpl;
import org.muse.mneme.api.Assessment;
import org.muse.mneme.api.AssessmentAccess;
import org.muse.mneme.api.AssessmentPermissionException;
import org.muse.mneme.api.AssessmentPolicyException;
import org.muse.mneme.api.AssessmentService;
import org.sakaiproject.util.Web;

/**
 * The /asssessment_special view for the mneme tool.
 */
public class AssessmentSpecialView extends ControllerImpl {
    /** Our log. */
    private static Log M_log = LogFactory.getLog(AssessmentSpecialView.class);

    /** Dependency: Assessment service. */
    protected AssessmentService assessmentService = null;

    /**
     * Shutdown.
     */
    public void destroy() {
        M_log.info("destroy()");
    }

    /**
     * {@inheritDoc}
     */
    public void get(HttpServletRequest req, HttpServletResponse res, Context context, String[] params)
            throws IOException {
        if (params.length != 4) {
            throw new IllegalArgumentException();
        }

        // sort parameter for return view
        String sort = params[2];

        // assessment id parameter
        String aid = params[3];
        Assessment assessment = assessmentService.getAssessment(aid);
        if (assessment == null) {
            // redirect to error
            res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.invalid)));
            return;
        }

        // security check
        if (!assessmentService.allowEditAssessment(assessment)) {
            // redirect to error
            res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.unauthorized)));
            return;
        }

        // make sure any undefined user ids or users without permissions are removed
        assessment.getSpecialAccess().assureValidUsers();

        // collect information: the selected assessment
        context.put("assessment", assessment);
        context.put("sort", sort);

        // render
        uiService.render(ui, context);
    }

    /**
     * Final initialization, once all dependencies are set.
     */
    public void init() {
        super.init();
        M_log.info("init()");
    }

    /**
     * {@inheritDoc}
     */
    public void post(HttpServletRequest req, HttpServletResponse res, Context context, String[] params)
            throws IOException {
        if (params.length != 4) {
            throw new IllegalArgumentException();
        }

        // sort parameter for return view
        String sort = params[2];

        // assessment id parameter
        String aid = params[3];
        Assessment assessment = assessmentService.getAssessment(aid);
        if (assessment == null) {
            // redirect to error
            res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.invalid)));
            return;
        }

        // security check
        if (!assessmentService.allowEditAssessment(assessment)) {
            // redirect to error
            res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.unauthorized)));
            return;
        }

        // for the ids selected for delete
        Values values = this.uiService.newValues();
        context.put("ids", values);

        // read form
        String destination = this.uiService.decode(req, context);

        if ("DEL".equals(destination)) {
            // delete the selected ids
            String[] ids = values.getValues();
            if (ids != null && (ids.length > 0)) {
                for (String id : ids) {
                    AssessmentAccess access = assessment.getSpecialAccess().getAccess(id);
                    if (access != null) {
                        assessment.getSpecialAccess().removeAccess(access);
                    }
                }
            }

            // make sure any undefined user ids or users without permissions are removed
            assessment.getSpecialAccess().assureValidUsers();

            // save
            try {
                this.assessmentService.saveAssessment(assessment);
            } catch (AssessmentPermissionException e) {
                // redirect to error
                res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.unauthorized)));
                return;
            } catch (AssessmentPolicyException e) {
                // redirect to error
                res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.policy)));
                return;
            }

            destination = context.getDestination();
        }

        // deal with add
        else if ("ADD".equals(destination)) {
            AssessmentAccess access = assessment.getSpecialAccess().addAccess();

            // save
            try {
                this.assessmentService.saveAssessment(assessment);
            } catch (AssessmentPermissionException e) {
                // redirect to error
                res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.unauthorized)));
                return;
            } catch (AssessmentPolicyException e) {
                // redirect to error
                res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.policy)));
                return;
            }

            // go edit it
            destination = "/assessment_access/" + sort + "/" + aid + "/" + access.getId();
        }

        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, destination)));
    }

    /**
     * Set the AssessmentService.
     * 
     * @param service
     *        The AssessmentService.
     */
    public void setAssessmentService(AssessmentService service) {
        this.assessmentService = service;
    }
}