it.scoppelletti.sdk.schemaupdate.RoleCreator.java Source code

Java tutorial

Introduction

Here is the source code for it.scoppelletti.sdk.schemaupdate.RoleCreator.java

Source

/*
 * Copyright (C) 2012 Dario Scoppelletti, <http://www.scoppelletti.it/>.
 * 
 * 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 it.scoppelletti.sdk.schemaupdate;

import java.io.*;
import java.util.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.*;
import it.scoppelletti.programmerpower.*;
import it.scoppelletti.programmerpower.io.*;
import it.scoppelletti.programmerpower.reflect.*;
import it.scoppelletti.programmerpower.security.*;
import it.scoppelletti.programmerpower.types.*;
import it.scoppelletti.programmerpower.ui.*;

/**
 * Crea un ruolo.
 * 
 * @see   it.scoppelletti.programmerpower.security.RoleManager
 * @since 1.0.0
 */
@Final
public class RoleCreator implements Runnable, InitializingBean {

    /**
     * Estensione dei file di propriet&agrave;. Il valore della costante
     * &egrave; <CODE>{@value}</CODE>. 
     */
    public static final String PROPERTIES_EXT = ".properties";

    /**
     * Separatore tra il nome di base di un file ed il codice della
     * localizzazione. Il valore della costante &egrave; <CODE>{@value}</CODE>. 
     */
    public static final String LOCALE_SEP = "_";

    @javax.annotation.Resource(name = UserInterfaceProvider.BEAN_NAME)
    private UserInterfaceProvider myUI;

    private String myCode;
    private String myDesc;
    private Map<String, String> myDescMap;
    private File myBaseDir;

    /**
     * Crea un ruolo.
     */
    public RoleCreator() {
    }

    /**
     * Imposta il codice del ruolo.
     * 
     * @param value Valore.
     */
    @Required
    public void setCode(String value) {
        if (Strings.isNullOrEmpty(value)) {
            throw new ArgumentNullException("value");
        }

        myCode = value;
    }

    /**
     * Imposta la descrizione del ruolo.
     * 
     * @param                       value Valore.
     * @it.scoppelletti.tag.default       {@link #setCode}
     */
    public void setDescription(String value) {
        myDesc = value;
    }

    /**
     * Imposta le descrizioni localizzate.
     * 
     * @param obj Oggetto.
     */
    public void setLocalizedDescriptions(Map<String, String> obj) {
        if (obj == null) {
            throw new ArgumentNullException("obj");
        }

        myDescMap = new HashMap<String, String>(obj);
    }

    /**
     * Inizializzazione.
     */
    public void afterPropertiesSet() throws Exception {
        myBaseDir = SharedDataDirectory.getInstance().initResourceDirectory(DefaultRoleManager.class);
    }

    /**
     * Esegue l&rsquo;operazione.
     */
    public void run() {
        String name;
        StringBuilder buf;
        File file;
        ProgramResources res = new ProgramResources();

        if (Strings.isNullOrEmpty(myCode)) {
            throw new PropertyNotSetException(toString(), "code");
        }

        if (Strings.isNullOrEmpty(myDesc)) {
            myDesc = myCode;
        }

        name = DefaultRoleManager.BASENAME.concat(RoleCreator.PROPERTIES_EXT);
        file = new File(myBaseDir, name);
        createRole(file, myDesc);

        if (myDescMap == null) {
            return;
        }

        for (Map.Entry<String, String> descEntry : myDescMap.entrySet()) {
            if (Strings.isNullOrEmpty(descEntry.getKey())) {
                myUI.display(MessageType.WARNING, res.getEmptyLocaleException(myCode));
                continue;
            }
            if (Strings.isNullOrEmpty(descEntry.getValue())) {
                myUI.display(MessageType.WARNING, res.getEmptyDescriptionException(myCode, descEntry.getKey()));
                continue;
            }

            buf = new StringBuilder(DefaultRoleManager.BASENAME);
            buf.append(RoleCreator.LOCALE_SEP);
            buf.append(descEntry.getKey());
            buf.append(RoleCreator.PROPERTIES_EXT);

            file = new File(myBaseDir, buf.toString());
            createRole(file, descEntry.getValue());
        }
    }

    /**
     * Crea una coppia (ruolo, descrizione).
     * 
     * @param file File di propriet&agrave;.
     * @param desc Descrizione.
     */
    private void createRole(File file, String desc) {
        Properties props;
        OutputStream out = null;
        ProgramResources res = new ProgramResources();

        props = loadProperties(file);
        if (props.containsKey(myCode)) {
            myUI.display(MessageType.WARNING, res.getRoleAlreadyExistException(myCode, file));
            return;
        }

        props.setProperty(myCode, desc);

        try {
            out = new FileOutputStream(file);
            props.store(out, null);
        } catch (IOException ex) {
            throw new IOOperationException(ex);
        } finally {
            if (out != null) {
                IOUtils.close(out);
                out = null;
            }
        }

        myUI.display(MessageType.INFORMATION, res.getRoleSavedMessage(myCode, desc, file));
    }

    /**
     * Legge un file di propriet&agrave;
     * 
     * @param  file File.
     * @return      Collezione.
     */
    private Properties loadProperties(File file) {
        Properties props;
        InputStream in = null;

        props = new Properties();
        if (!file.exists()) {
            return props;
        }

        try {
            in = new FileInputStream(file);
            props.load(in);
        } catch (IOException ex) {
            throw new IOOperationException(ex);
        } finally {
            if (in != null) {
                IOUtils.close(in);
                in = null;
            }
        }

        return props;
    }
}