org.shaf.server.controller.GenericActionController.java Source code

Java tutorial

Introduction

Here is the source code for org.shaf.server.controller.GenericActionController.java

Source

/**
 * Copyright 2014-2015 SHAF-WORK
 * 
 * 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.shaf.server.controller;

import javax.servlet.http.HttpServletRequest;

import org.atteo.evo.inflector.English;
import org.shaf.core.security.Firewall;
import org.shaf.core.security.Role;
import org.shaf.core.util.TextMatrix;
import org.shaf.server.oper.OperationManager;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.collect.ObjectArrays;

/**
 * The generic action controller.
 * 
 * @author Mykola Galushka
 */
public class GenericActionController {

    /**
     * The server request.
     */
    @Autowired(required = true)
    protected HttpServletRequest REQUEST;

    /**
     * The operation manager.
     */
    @Autowired
    protected OperationManager OPER;

    /**
     * Returns the firewall.
     * 
     * @return the firewall.
     */
    protected final Firewall getFirewall() {
        Role[] roles = new Role[0];
        for (Role role : Role.values()) {
            if (REQUEST.isUserInRole(role.getName())) {
                roles = ObjectArrays.concat(roles, role);
            }
        }
        return new Firewall(roles);
    }

    protected final String getUserName() {
        return REQUEST.getUserPrincipal().getName();
    }

    /**
     * Returns a generic list description.
     * 
     * @param content
     *            the list, which needs to be described.
     * @param term
     *            the term, which list consists of.
     * @return the generated description.
     */
    protected final String getListDescription(final TextMatrix list, final String term) {
        if (list.getNumRows() == 0) {
            return "There are no " + English.plural(term) + " detected.";
        } else if (list.getNumRows() == 1) {
            return "There is one " + term + " detected.";
        } else {
            return "There are " + list.getNumRows() + " " + English.plural(term) + " detected.";
        }
    }
}