Package de.innovationgate.wga.model

Source Code of de.innovationgate.wga.model.WGADesignConfigurationModel

/*******************************************************************************
* Copyright 2009, 2010 Innovation Gate GmbH
*
* 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 de.innovationgate.wga.model;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.security.GeneralSecurityException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.BrokenBarrierException;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.commons.beanutils.BeanUtils;

import net.java.dev.genesis.annotation.DataProvider;
import net.java.dev.genesis.annotation.NotBound;
import de.innovationgate.utils.DirZipper;
import de.innovationgate.utils.TemporaryFile;
import de.innovationgate.utils.WGUtils;
import de.innovationgate.utils.DESEncrypter.PersistentKeyException;
import de.innovationgate.wga.common.Constants;
import de.innovationgate.wga.common.DesignDirectory;
import de.innovationgate.wga.common.beans.DesignDefinition;
import de.innovationgate.wga.common.beans.csconfig.v1.ACLRole;
import de.innovationgate.wga.common.beans.csconfig.v1.CSConfig;
import de.innovationgate.wga.common.beans.csconfig.v1.ElementMapping;
import de.innovationgate.wga.common.beans.csconfig.v1.EncoderMapping;
import de.innovationgate.wga.common.beans.csconfig.v1.InvalidCSConfigVersionException;
import de.innovationgate.wga.common.beans.csconfig.v1.JobDefinition;
import de.innovationgate.wga.common.beans.csconfig.v1.MediaKey;
import de.innovationgate.wga.common.beans.csconfig.v1.PluginConfig;
import de.innovationgate.wga.common.beans.csconfig.v1.PluginID;
import de.innovationgate.wga.common.beans.csconfig.v1.PublisherOption;
import de.innovationgate.wga.common.beans.csconfig.v1.RemoteAction;
import de.innovationgate.wga.common.beans.csconfig.v1.Version;
import de.innovationgate.wga.common.beans.csconfig.v2.Shortcut;

public class WGADesignConfigurationModel extends AbstractModel {
 
  public static final String BUILD_PROP_SIGNATURE = "signature";
  public static final String BUILD_PROP_COMMENT = "comment";

    public static final int INTEGER_NOT_SET = -1;

    public static final String STRING_NOT_SET = "$notset";

    public static final AuthPlugin EMPTY_AUTH_PLUGIN = new AuthPlugin(STRING_NOT_SET, "None");

    public static final Map<Integer, AccessLevel> ACCESSLEVELS = new HashMap<Integer, AccessLevel>();

    static {
        ACCESSLEVELS.put(AccessLevel.LEVEL_NO_ACCESS, new AccessLevel(AccessLevel.LEVEL_NO_ACCESS, "NO ACCESS"));
        ACCESSLEVELS.put(AccessLevel.LEVEL_READER, new AccessLevel(AccessLevel.LEVEL_READER, "READER"));
        ACCESSLEVELS.put(AccessLevel.LEVEL_READER_DESIGNER, new AccessLevel(AccessLevel.LEVEL_READER_DESIGNER, "READER/DESIGNER"));
        ACCESSLEVELS.put(AccessLevel.LEVEL_AUTHOR, new AccessLevel(AccessLevel.LEVEL_AUTHOR, "AUTHOR"));
        ACCESSLEVELS.put(AccessLevel.LEVEL_AUTHOR_DESIGNER, new AccessLevel(AccessLevel.LEVEL_AUTHOR_DESIGNER, "AUTHOR/DESIGNER"));
        ACCESSLEVELS.put(AccessLevel.LEVEL_EDITOR, new AccessLevel(AccessLevel.LEVEL_EDITOR, "EDITOR"));
        ACCESSLEVELS.put(AccessLevel.LEVEL_EDITOR_DESIGNER, new AccessLevel(AccessLevel.LEVEL_EDITOR_DESIGNER, "EDITOR/DESIGNER"));
        ACCESSLEVELS.put(AccessLevel.LEVEL_MANAGER, new AccessLevel(AccessLevel.LEVEL_MANAGER, "MANAGER"));
        ACCESSLEVELS.put(INTEGER_NOT_SET, new AccessLevel(INTEGER_NOT_SET, "Do not set"));
    }

    public static final Map<String, ExpressionLanguage> EXPRESSION_LANGUAGES = new HashMap<String, ExpressionLanguage>();

    static {
        EXPRESSION_LANGUAGES.put(STRING_NOT_SET, new ExpressionLanguage(STRING_NOT_SET, "Not set"));
        EXPRESSION_LANGUAGES.put(ExpressionLanguage.LANGUAGE_TMLSCRIPT, new ExpressionLanguage(ExpressionLanguage.LANGUAGE_TMLSCRIPT, "TMLScript"));
        EXPRESSION_LANGUAGES.put(ExpressionLanguage.LANGUAGE_NATIVE, new ExpressionLanguage(ExpressionLanguage.LANGUAGE_NATIVE, "Native database language"));
    }

    public static final Map<String, VersionCompliance> VERSIONCOMPLIANCES = new LinkedHashMap<String, VersionCompliance>();

    static {
        VERSIONCOMPLIANCES.put(VersionCompliance.VERSIONCOMPLIANCE_WGA3, new VersionCompliance(VersionCompliance.VERSIONCOMPLIANCE_WGA3, "WGA 3.x"));
        VERSIONCOMPLIANCES.put(VersionCompliance.VERSIONCOMPLIANCE_WGA4, new VersionCompliance(VersionCompliance.VERSIONCOMPLIANCE_WGA4, "WGA 4.0"));
        VERSIONCOMPLIANCES.put(VersionCompliance.VERSIONCOMPLIANCE_WGA41, new VersionCompliance(VersionCompliance.VERSIONCOMPLIANCE_WGA41, "WGA 4.1"));
        VERSIONCOMPLIANCES.put(VersionCompliance.VERSIONCOMPLIANCE_WGA50, new VersionCompliance(VersionCompliance.VERSIONCOMPLIANCE_WGA50, "WGA 5.0"));
        VERSIONCOMPLIANCES.put(VersionCompliance.VERSIONCOMPLIANCE_WGA51, new VersionCompliance(VersionCompliance.VERSIONCOMPLIANCE_WGA51, "WGA 5.1"));
        VERSIONCOMPLIANCES.put(VersionCompliance.VERSIONCOMPLIANCE_WGA52, new VersionCompliance(VersionCompliance.VERSIONCOMPLIANCE_WGA52, "WGA 5.2"));
        VERSIONCOMPLIANCES.put(VersionCompliance.VERSIONCOMPLIANCE_WGA53, new VersionCompliance(VersionCompliance.VERSIONCOMPLIANCE_WGA53, "WGA 5.3"));
        VERSIONCOMPLIANCES.put(VersionCompliance.VERSIONCOMPLIANCE_WGA54, new VersionCompliance(VersionCompliance.VERSIONCOMPLIANCE_WGA54, "WGA 5.4 and upwards"));
    }

    public static final Map<String, Version> VERSIONCOMPLIANCE_TO_WGA_VERSION = new LinkedHashMap<String, Version>();
    static {
        VERSIONCOMPLIANCE_TO_WGA_VERSION.put(VersionCompliance.VERSIONCOMPLIANCE_WGA3, new Version("3.3.0"));
        VERSIONCOMPLIANCE_TO_WGA_VERSION.put(VersionCompliance.VERSIONCOMPLIANCE_WGA4, new Version("4.0.0"));
        VERSIONCOMPLIANCE_TO_WGA_VERSION.put(VersionCompliance.VERSIONCOMPLIANCE_WGA41, new Version("4.1.0"));
        VERSIONCOMPLIANCE_TO_WGA_VERSION.put(VersionCompliance.VERSIONCOMPLIANCE_WGA50, new Version("5.0.0"));
        VERSIONCOMPLIANCE_TO_WGA_VERSION.put(VersionCompliance.VERSIONCOMPLIANCE_WGA51, new Version("5.1.0"));
        VERSIONCOMPLIANCE_TO_WGA_VERSION.put(VersionCompliance.VERSIONCOMPLIANCE_WGA52, new Version("5.2.0"));
        VERSIONCOMPLIANCE_TO_WGA_VERSION.put(VersionCompliance.VERSIONCOMPLIANCE_WGA53, new Version("5.3.0"));
        VERSIONCOMPLIANCE_TO_WGA_VERSION.put(VersionCompliance.VERSIONCOMPLIANCE_WGA54, new Version("5.4.0"));
    }

    public static final Map<String, Encoding> ITEMENCODINGS = new HashMap<String, Encoding>();

    static {
        ITEMENCODINGS.put(Encoding.ENCODING_HTML, new Encoding(Encoding.ENCODING_HTML, "html"));
        ITEMENCODINGS.put(Encoding.ENCODING_NONE, new Encoding(Encoding.ENCODING_NONE, "none"));
        ITEMENCODINGS.put(STRING_NOT_SET, new Encoding(STRING_NOT_SET, "Not set"));
    }

    public static final Map<String, Encoding> DESIGNENCODINGS = new HashMap<String, Encoding>();

    static {
        DESIGNENCODINGS.put(Encoding.ENCODING_UTF_8, new Encoding(Encoding.ENCODING_UTF_8, "Unicode (UTF-8)"));
        DESIGNENCODINGS.put(Encoding.ENCODING_ISO_8859_1, new Encoding(Encoding.ENCODING_ISO_8859_1, "Latin-1 (ISO-8859-1)"));
        DESIGNENCODINGS.put(STRING_NOT_SET, new Encoding(STRING_NOT_SET, "Not set (Platform encoding)"));
    }

    public static final Map<Integer, PersonalisationMode> PERSONALISATION_MODES = new HashMap<Integer, PersonalisationMode>();
    static {
        PERSONALISATION_MODES.put(Constants.PERSMODE_AUTO, new PersonalisationMode(Constants.PERSMODE_AUTO, "Automatic (per browser / cookie-based)"));
        PERSONALISATION_MODES.put(Constants.PERSMODE_LOGIN, new PersonalisationMode(Constants.PERSMODE_LOGIN, "Login (per database login)"));
        PERSONALISATION_MODES.put(Constants.PERSMODE_CUSTOM, new PersonalisationMode(Constants.PERSMODE_CUSTOM, "Custom (assignment via TMLScript)"));
    }

    public static final Map<Integer, AccessLevel> ACCESSLEVELS_REMOTE_ACTIONS = new HashMap<Integer, AccessLevel>();

    static {
        ACCESSLEVELS_REMOTE_ACTIONS.put(AccessLevel.LEVEL_READER, new AccessLevel(AccessLevel.LEVEL_READER, "READER"));
        ACCESSLEVELS_REMOTE_ACTIONS.put(AccessLevel.LEVEL_READER_DESIGNER, new AccessLevel(AccessLevel.LEVEL_READER_DESIGNER, "READER/DESIGNER"));
        ACCESSLEVELS_REMOTE_ACTIONS.put(AccessLevel.LEVEL_AUTHOR, new AccessLevel(AccessLevel.LEVEL_AUTHOR, "AUTHOR"));
        ACCESSLEVELS_REMOTE_ACTIONS.put(AccessLevel.LEVEL_AUTHOR_DESIGNER, new AccessLevel(AccessLevel.LEVEL_AUTHOR_DESIGNER, "AUTHOR/DESIGNER"));
        ACCESSLEVELS_REMOTE_ACTIONS.put(AccessLevel.LEVEL_EDITOR, new AccessLevel(AccessLevel.LEVEL_EDITOR, "EDITOR"));
        ACCESSLEVELS_REMOTE_ACTIONS.put(AccessLevel.LEVEL_EDITOR_DESIGNER, new AccessLevel(AccessLevel.LEVEL_EDITOR_DESIGNER, "EDITOR/DESIGNER"));
        ACCESSLEVELS_REMOTE_ACTIONS.put(AccessLevel.LEVEL_MANAGER, new AccessLevel(AccessLevel.LEVEL_MANAGER, "MANAGER"));
        ACCESSLEVELS_REMOTE_ACTIONS.put(10000, new AccessLevel(10000, "Specified callers only"));
    }

    public static final Map<Integer, JobType> JOBTYPES = new HashMap<Integer, JobType>();

    static {
        JOBTYPES.put(JobType.TYPE_JAVA, new JobType(JobType.TYPE_JAVA, "Java Task Implementation"));
        JOBTYPES.put(JobType.TYPE_TMLSCRIPTMODULE, new JobType(JobType.TYPE_TMLSCRIPTMODULE, "TMLScript Module"));
    }

    public static final String FEATURE_SHORTCUTS = "SHORTCUTS";

    public static final Map<Integer, ShortcutType> SHORTCUTTYPES = new HashMap<Integer, ShortcutType>();

    static {
        SHORTCUTTYPES.put(new Integer(ShortcutType.TYPE_PLUGIN), new ShortcutType(ShortcutType.TYPE_PLUGIN, "WGA plugin shortcut"));
        SHORTCUTTYPES.put(new Integer(ShortcutType.TYPE_TMLSCRIPT_GLOBAL), new ShortcutType(ShortcutType.TYPE_TMLSCRIPT_GLOBAL, "TMLScript global"));
        SHORTCUTTYPES.put(new Integer(ShortcutType.TYPE_ITEM_MAPPING), new ShortcutType(ShortcutType.TYPE_ITEM_MAPPING, "Item mapping"));
        SHORTCUTTYPES.put(new Integer(ShortcutType.TYPE_META_MAPPING), new ShortcutType(ShortcutType.TYPE_META_MAPPING, "Meta mapping"));
    }
   
    public static final Map<Integer, BrowsingSecurity> BROWSINGSECURITY_VALUES = new LinkedHashMap<Integer, BrowsingSecurity>();

    static {
        BROWSINGSECURITY_VALUES.put(BrowsingSecurity.FULL_ACCESS, new BrowsingSecurity(BrowsingSecurity.FULL_ACCESS, "Normal access"));
        BROWSINGSECURITY_VALUES.put(BrowsingSecurity.NO_AUTHORING, new BrowsingSecurity(BrowsingSecurity.NO_AUTHORING, "No authoring"));
        BROWSINGSECURITY_VALUES.put(BrowsingSecurity.NO_BROWSING, new BrowsingSecurity(BrowsingSecurity.NO_BROWSING, "No authoring and content addressing"));
    }
   
    protected File _syncInfoFile;

    protected DesignDefinition _syncInfo;

    protected File _designDirectory;

    protected File _csConfigFile;

    protected CSConfig _csConfig;

    // a in memory state of the last shortcuts set on this model
    // necessary to restore shortcuts when version compliance is changed
    // multiple times without save
    private List<Shortcut> _cachedShortcuts;

    // special props for versions to avoid parsing errors in setter methods
    private String _pluginVersion;

    private int _pluginBuild;

    private String _pluginWGAVersion;

    private String _pluginJavaVersion;
   
    /**
     * contains options which are direct modifable and should be filtered from custom publisher options list
     */
    public static final List<String> DIRECT_MODIFIABLE_PUBLISHER_OPTIONS = new ArrayList<String>();
    static {
      DIRECT_MODIFIABLE_PUBLISHER_OPTIONS.add(PublisherOption.OPTION_HOME_PAGE);
      DIRECT_MODIFIABLE_PUBLISHER_OPTIONS.add(PublisherOption.OPTION_LOGIN_PAGE);
      DIRECT_MODIFIABLE_PUBLISHER_OPTIONS.add(PublisherOption.OPTION_MULTI_LANGUAGE_CONTENT);
      DIRECT_MODIFIABLE_PUBLISHER_OPTIONS.add(PublisherOption.OPTION_USES_HDB);
      DIRECT_MODIFIABLE_PUBLISHER_OPTIONS.add(PublisherOption.OPTION_DEFAULT_ITEM_ENCODING);
      DIRECT_MODIFIABLE_PUBLISHER_OPTIONS.add(PublisherOption.OPTION_DESIGN_ENCODING);
      DIRECT_MODIFIABLE_PUBLISHER_OPTIONS.add(PublisherOption.OPTION_ADMIN_APP);
      DIRECT_MODIFIABLE_PUBLISHER_OPTIONS.add(PublisherOption.OPTION_BROWSING_SECURITY);
    }


    public WGADesignConfigurationModel(File syncInfoFile) throws IOException {
        _syncInfoFile = syncInfoFile;
        reload();
    }

    @Override
    public void saveChanges() throws IOException {
        if (validate().isEmpty()) {
            _csConfig.write(_csConfigFile);
            writeDesignDefinition();
            _cachedShortcuts = null;
        }
        else {
            throw new IOException("Configuration contains invalid values.");
        }
    }

    private void writeDesignDefinition() throws IOException {
        _syncInfo.setFileEncoding("csconfig");
       
        // Ensure correct file name based on the version compliance
        String correctName = _csConfig.getDesignDefinitionFileName();
        if (!_syncInfoFile.getName().equals(correctName)) {
            File newSyncInfoFile  = new File(_syncInfoFile.getParentFile(), correctName);
            if (_syncInfoFile.exists()) {
                _syncInfoFile.delete();
            }
            _syncInfoFile = newSyncInfoFile;
        }
       
        _syncInfo.write(_syncInfoFile);
    }
   
    public void setPublisherOption(String name, String value, boolean fireModelChanged) {
        if (value != null && value.equals(STRING_NOT_SET)) {
            removePublisherOption(name, fireModelChanged);
        }
        else {
            PublisherOption option = getPublisherOption(name);
            if (option != null) {
                option.setValue(value);
            }
            else {
                _csConfig.getPublisherOptions().add(new PublisherOption(name, value));
            }
            if (fireModelChanged) {
                fireModelChanged();
            }
        }
    }

    public void setPublisherOption(String name, String value) {
        setPublisherOption(name, value, true);
    }


    public void setPublisherOption(String name, boolean value) {
        setPublisherOption(name, String.valueOf(value));
    }
   
    public void setPublisherOption(String name, int value) {
        setPublisherOption(name, String.valueOf(value));
    }

    public PublisherOption getPublisherOption(String name) {
        Iterator options = _csConfig.getPublisherOptions().iterator();
        while (options.hasNext()) {
            PublisherOption option = (PublisherOption) options.next();
            if (option.getName().equals(name)) {
                return option;
            }
        }
        return null;
    }
   
    @SuppressWarnings("unchecked")
    @NotBound
    public List<PublisherOption> getPublisherOptions() {
      return _csConfig.getPublisherOptions();
    }

    private void removePublisherOption(String name, boolean fireModelChanged) {
        Iterator options = _csConfig.getPublisherOptions().iterator();
        boolean removed = false;
        while (options.hasNext()) {
            PublisherOption option = (PublisherOption) options.next();
            if (option.getName().equals(name)) {
                options.remove();
                removed = true;
            }
        }
        if (fireModelChanged && removed) {
            fireModelChanged();
        }
    }
   
    private void removePublisherOption(String name) {
        removePublisherOption(name, true);
    }

    private String getPublisherOptionValue(String name) {
        PublisherOption option = findOption(name, false);
        if (option != null) {
            return option.getValue();
        }
        else {
            return getDefaultValue(name);
        }
    }

    /**
     * checks value != null && value != ''
     */
    protected boolean isEmpty(String value) {
        if (value != null && !value.trim().equalsIgnoreCase("")) {
            return false;
        }
        else {
            return true;
        }
    }

    public String getInitScript() {
        return _csConfig.getInitScript();
    }

    public String getConnectionScript() {
        return _csConfig.getConnectionScript();
    }

    public void setInitScript(String script) {
        _csConfig.setInitScript(script);
        fireModelChanged();
    }

    public void setConnectionScript(String script) {
        _csConfig.setConnectionScript(script);
        fireModelChanged();
    }
    public void setDisconnectionScript(String script) {
      //_disconnectionScript = script;
        if (_csConfig instanceof de.innovationgate.wga.common.beans.csconfig.v3.CSConfig) {
            ((de.innovationgate.wga.common.beans.csconfig.v3.CSConfig) _csConfig).setDisconnectionScript(script);           
        }
        fireModelChanged();
    }
   
    public void setPluginClearDatabaseOnUpdate(boolean value) {
        if (_csConfig.getPluginConfig() != null && _csConfig.getPluginConfig() instanceof de.innovationgate.wga.common.beans.csconfig.v3.PluginConfig) {
            ((de.innovationgate.wga.common.beans.csconfig.v3.PluginConfig) _csConfig.getPluginConfig()).setClearDatabaseOnUpdate(value);
        }
        fireModelChanged();
    }
   
    public void setPluginDisableInit(boolean value) {
        if (_csConfig.getPluginConfig() != null && _csConfig.getPluginConfig() instanceof de.innovationgate.wga.common.beans.csconfig.v3.PluginConfig) {
            ((de.innovationgate.wga.common.beans.csconfig.v3.PluginConfig) _csConfig.getPluginConfig()).setDisablePluginInit(value);           
        }
        fireModelChanged();
    }

    @SuppressWarnings("unchecked")
    @DataProvider(objectField = "anonymousAccessLevel")
    public List<AccessLevel> provideAnonymousAccessLevels() {
        List<AccessLevel> list = new ArrayList<AccessLevel>(ACCESSLEVELS.values());
        return WGUtils.sortByProperty(list, "key");
    }

    @DataProvider(objectField = "defaultAccessLevel")
    public List<AccessLevel> provideDefaultAccessLevels() {
        List<AccessLevel> list = new ArrayList<AccessLevel>(ACCESSLEVELS.values());
        return WGUtils.sortByProperty(list, "key");
    }

    @DataProvider(objectField = "versionCompliance")
    public List<VersionCompliance> provideVersionCompliances() {
        List<VersionCompliance> list = new ArrayList<VersionCompliance>(VERSIONCOMPLIANCES.values());
        return list;
    }

public AccessLevel getAnonymousAccessLevel() {
        return ACCESSLEVELS.get(_csConfig.getAnonymousAccessLevel());
    }

    public void setAnonymousAccessLevel(AccessLevel level) {
        _csConfig.setAnonymousAccessLevel(level.getKey());
        fireModelChanged();
    }

    public AccessLevel getDefaultAccessLevel() {
        AccessLevel level = ACCESSLEVELS.get(_csConfig.getDefaultAccessLevel());
        return level;
    }

    public VersionCompliance getVersionCompliance() {
        return VERSIONCOMPLIANCES.get(_csConfig.getVersionCompliance());
    }

    public void setDefaultAccessLevel(AccessLevel level) {
        _csConfig.setDefaultAccessLevel(level.getKey());
        fireModelChanged();
    }

    public void setVersionCompliance(VersionCompliance vc) throws IllegalAccessException, InvocationTargetException {
        _csConfig.setVersionCompliance(vc.getKey());
        ensureCorrectCSConfigVersion();
        fireModelChanged();
    }

    @NotBound
    public File getTMLScriptDirectory() {
        return new File(_designDirectory, "scripts/tmlscript");
    }

    public String getDesignKey() {
        return _syncInfo.getDesignKey();
    }

    public String getDesignDirectory() {
        return _designDirectory.getAbsolutePath();
    }

    @NotBound
    public String[] getLibraryNames() {
        File systemDir = new File(_designDirectory, "files/system");
        String[] names = systemDir.list(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                return name.endsWith(".jar");
            }

        });
        return names;
    }

    public void addLibrary(File file) throws IOException {
        File systemDir = new File(_designDirectory, "files/system");
        File target = new File(systemDir, file.getName());
        FileInputStream in = new FileInputStream(file);
        FileOutputStream out = new FileOutputStream(target);
        WGUtils.inToOut(in, out, 2048);
        in.close();
        out.close();
    }

    public boolean hasLibrary(String name) {
        return Arrays.asList(getLibraryNames()).contains(name);
    }

    public boolean deleteLibrary(String filename) {
        File systemDir = new File(_designDirectory, "files/system");
        return new File(systemDir, filename).delete();
    }

    public String getHomePage() {
        return getPublisherOptionValue(PublisherOption.OPTION_HOME_PAGE);
    }

    public void setHomePage(String page) {
        setPublisherOption(PublisherOption.OPTION_HOME_PAGE, page);
    }

    public String getLoginPage() {
        return getPublisherOptionValue(PublisherOption.OPTION_LOGIN_PAGE);
    }

    public void setLoginPage(String page) {
        setPublisherOption(PublisherOption.OPTION_LOGIN_PAGE, page);
    }

    public boolean isMultiLanguageContent() {
        return Boolean.parseBoolean(getPublisherOptionValue(PublisherOption.OPTION_MULTI_LANGUAGE_CONTENT));
    }

    public void setMultiLanguageContent(boolean enabled) {
        setPublisherOption(PublisherOption.OPTION_MULTI_LANGUAGE_CONTENT, enabled);
    }

    @SuppressWarnings("unchecked")
    @DataProvider(objectField = "defaultItemEncoding")
    public List<Encoding> populateItemEncodings() {

        List<Encoding> encodings = new ArrayList<Encoding>(ITEMENCODINGS.values());

        Iterator<EncoderMapping> customEncoders = _csConfig.getEncoderMappings().iterator();
        while (customEncoders.hasNext()) {
            EncoderMapping element = customEncoders.next();
            encodings.add(new Encoding(element.getName(), element.getName()));
        }

        return encodings;
    }

    public Encoding getDefaultItemEncoding() {
        String encValue = getPublisherOptionValue(PublisherOption.OPTION_DEFAULT_ITEM_ENCODING);
        Iterator<Encoding> encodings = populateItemEncodings().iterator();
        while (encodings.hasNext()) {
            Encoding encoding = encodings.next();
            if (encoding.getKey().equals(encValue)) {
                return encoding;
            }
        }
        return null;
    }

    public void setDefaultItemEncoding(Encoding encoding) {
        setPublisherOption(PublisherOption.OPTION_DEFAULT_ITEM_ENCODING, encoding.getKey());
    }

    @DataProvider(objectField = "designEncoding")
    public List<Encoding> populateDesignEncodings() {
        List<Encoding> encodings = new ArrayList<Encoding>(DESIGNENCODINGS.values());

        String currentEncodingValue = retrieveDesignEncodingValue();
        if (currentEncodingValue != null) {
            Encoding currentEncoding = new Encoding(currentEncodingValue, currentEncodingValue);
            if (!DESIGNENCODINGS.containsKey(currentEncodingValue)) {
                encodings.add(currentEncoding);
            }
        }

        return encodings;
    }

    public Encoding getDesignEncoding() {
        String encValue = retrieveDesignEncodingValue();

        Iterator<Encoding> encodings = populateDesignEncodings().iterator();
        while (encodings.hasNext()) {
            Encoding encoding = encodings.next();
            if (encoding.getKey().equals(encValue)) {
                return encoding;
            }
        }
        return null;
    }

    @NotBound
    private String retrieveDesignEncodingValue() {
        String encValue = getPublisherOptionValue(PublisherOption.OPTION_DESIGN_ENCODING);
        if (encValue == null || encValue.trim().equals("")) {
            encValue = STRING_NOT_SET;
        }
        return encValue;
    }

    public void setDesignEncoding(Encoding encoding) {
        setPublisherOption(PublisherOption.OPTION_DESIGN_ENCODING, encoding.getKey());
    }

//    @DataProvider(objectField = "expressionDefault")
//    public List<ExpressionLanguage> populateExpressionLanguages() {
//        return new ArrayList<ExpressionLanguage>(EXPRESSION_LANGUAGES.values());
//    }

//    public ExpressionLanguage getExpressionDefault() {
//        String xpCode = getPublisherOptionValue(PublisherOption.OPTION_EXPRESSION_DEFAULT);
//        return (ExpressionLanguage) findBeanByKey(xpCode, populateExpressionLanguages());
//    }

//    public void setExpressionDefault(ExpressionLanguage language) {
//        setPublisherOption(PublisherOption.OPTION_EXPRESSION_DEFAULT, language.getKey());
//    }

    private String getDefaultValue(String name) {

        if (name.equals(PublisherOption.OPTION_DEFAULT_ITEM_ENCODING)) {
            return STRING_NOT_SET;
        }
        else if (name.equals(PublisherOption.OPTION_DEFAULT_MEDIA_KEY)) {
            return Encoding.ENCODING_HTML;
        }
        else if (name.equals(PublisherOption.OPTION_EXPRESSION_DEFAULT)) {
            return STRING_NOT_SET;
        }
        else if (name.equals(PublisherOption.OPTION_MULTI_LANGUAGE_CONTENT)) {
            return "true";
        }
        else if (name.equals(PublisherOption.OPTION_DESIGN_ENCODING)) {
            return STRING_NOT_SET;
        }
        else if (name.equals(PublisherOption.OPTION_ADMIN_APP)) {
            return "false";
        }
        else if (name.equals(PublisherOption.OPTION_BROWSING_SECURITY)) {
            return "90";
        }
        else {
            return "";
        }
    }

    private PublisherOption findOption(String name, boolean create) {
        Iterator<PublisherOption> optionIt = _csConfig.getPublisherOptions().iterator();
        while (optionIt.hasNext()) {
            PublisherOption option = optionIt.next();
            if (option.getName().equals(name)) {
                return option;
            }
        }

        if (create) {
            PublisherOption option = new PublisherOption();
            option.setName(name);
            _csConfig.getPublisherOptions().add(option);
            return option;
        }
        else {
            return null;
        }

    }

    private Object findBeanByKey(String key, List list) {
        Iterator beans = list.iterator();
        while (beans.hasNext()) {
            KeyValueBean<String, Object> bean = (KeyValueBean<String, Object>) beans.next();
            if (bean.getKey().equals(key)) {
                return bean;
            }
        }
        return null;
    }

//    public String getDefaultMediaKey() {
//        return getPublisherOptionValue(PublisherOption.OPTION_DEFAULT_MEDIA_KEY);
//    }
//
//    public void setDefaultMediaKey(String key) {
//        setPublisherOption(PublisherOption.OPTION_DEFAULT_MEDIA_KEY, key);
//    }

    public boolean isDirectAccessDefault() {
        return Boolean.parseBoolean(getPublisherOptionValue(PublisherOption.OPTION_DIRECT_ACCESS_DEFAULT));
    }
//
//    public void setDirectAccessDefault(boolean value) {
//        setPublisherOption(PublisherOption.OPTION_DIRECT_ACCESS_DEFAULT, value);
//    }

    public boolean isUsesHDB() {
      if (_csConfig instanceof de.innovationgate.wga.common.beans.csconfig.v3.CSConfig) {
        return true;
      } else if (_csConfig.getVersionCompliance().equals(CSConfig.VERSIONCOMPLIANCE_WGA3)) {
        return false;
      }
        return Boolean.parseBoolean(getPublisherOptionValue(PublisherOption.OPTION_USES_HDB));
    }

    public void setUsesHDB(boolean value) {     
        setPublisherOption(PublisherOption.OPTION_USES_HDB, value);
    }

    @NotBound
    public boolean hasPluginConfig() {
        return _csConfig.getPluginConfig() != null;
    }

    public void createPluginConfig() {
        PluginConfig config =  PluginConfig.instantiatePluginConfigForCompliance(_csConfig.getVersionCompliance());
        config.getId().getVersion().setBuildVersion(1);
        if (getVersionCompliance() != null) {
            Version minWGAVersion = VERSIONCOMPLIANCE_TO_WGA_VERSION.get(getVersionCompliance().getKey());
            if (minWGAVersion != null) {
                config.setMinimumWGAVersion(minWGAVersion);
            }
           
            // OpenWGA 5.0 requires java 1.5
            if (minWGAVersion.isAtLeast(5, 0)) {
                config.setMinimumJavaVersion(new Version("1.5.0"));
            }
        }
       
        config.setPersonalisationMode(Constants.PERSMODE_AUTO);
       
        _csConfig.setPluginConfig(config);

        loadPluginVersionProperties();

        fireModelChanged();
    }

    private void loadPluginVersionProperties() {
        if (_csConfig.getPluginConfig() != null) {
            _pluginVersion = _csConfig.getPluginConfig().getId().getVersion().getMainVersionString();
            _pluginBuild = _csConfig.getPluginConfig().getId().getVersion().getBuildVersion();
            _pluginWGAVersion = _csConfig.getPluginConfig().getMinimumWGAVersion().getMainVersionString();
            _pluginJavaVersion = _csConfig.getPluginConfig().getMinimumJavaVersion().toString();
        }
    }

    public void removePluginConfig() {
        _csConfig.setPluginConfig(null);
        _pluginBuild = 0;
        _pluginJavaVersion = null;
        _pluginVersion = null;
        _pluginWGAVersion = null;
        fireModelChanged();
    }

    @SuppressWarnings("unchecked")
    @NotBound
    public List<PluginID> getPluginDependencies() {
        if (hasPluginConfig()) {
            return _csConfig.getPluginConfig().getDependencies();
        }
        else {
            return Collections.EMPTY_LIST;
        }
    }

    public String getPluginUniqueName() {
        if (hasPluginConfig()) {
            return _csConfig.getPluginConfig().getId().getUniqueName();
        }
        else {
            return null;
        }
    }

    public void setPluginUniqueName(String uname) {
        if (uname != null) {
            uname = uname.trim();
        }
        _csConfig.getPluginConfig().getId().setUniqueName(uname);
        fireModelChanged();
    }

    public String getPluginJavaVersion() {
        if (hasPluginConfig()) {
            return _pluginJavaVersion;
        }
        else {
            return null;
        }
    }

    public void setPluginJavaVersion(String version) {
        _pluginJavaVersion = version;
        fireModelChanged();
    }

    public String getPluginVersion() {
        if (hasPluginConfig()) {
            return _pluginVersion;
        }
        else {
            return null;
        }
    }

    public void setPluginVersion(String version) {
        _pluginVersion = version;
        fireModelChanged();
    }

    public int getPluginBuild() {
        if (hasPluginConfig()) {
            return _csConfig.getPluginConfig().getId().getVersion().getBuildVersion();
        }
        else {
            return -1;
        }
    }

    public void setPluginBuild(int build) {
        _pluginBuild = build;
        fireModelChanged();
    }

    public String getPluginWGAVersion() {
        if (hasPluginConfig()) {
            return _pluginWGAVersion;
        }
        else {
            return null;
        }
    }

    public void setPluginWGAVersion(String version) {
        _pluginWGAVersion = version;
        fireModelChanged();
    }

    public String getPluginTitle() {
        if (hasPluginConfig()) {
            return _csConfig.getPluginConfig().getTitle();
        }
        else {
            return null;
        }
    }

    public void setPluginTitle(String title) {
        _csConfig.getPluginConfig().setTitle(title);
        fireModelChanged();
    }

    public String getPluginVendor() {
        if (hasPluginConfig()) {
            return _csConfig.getPluginConfig().getVendor();
        }
        else {
            return null;
        }
    }

    public void setPluginVendor(String vendor) {
        _csConfig.getPluginConfig().setVendor(vendor);
        fireModelChanged();
    }

    public String getPluginVendorHomepage() {
        if (hasPluginConfig()) {
            return _csConfig.getPluginConfig().getWebHomepage();
        }
        else {
            return null;
        }
    }

    public void setPluginVendorHomepage(String homepage) {
        _csConfig.getPluginConfig().setWebHomepage(homepage);
        fireModelChanged();
    }

    public String getPluginDescription() {
        if (hasPluginConfig()) {
            return _csConfig.getPluginConfig().getDescription();
        }
        else {
            return null;
        }
    }

    public void setPluginDescription(String description) {
        _csConfig.getPluginConfig().setDescription(description);
        fireModelChanged();
    }

    public String getPluginHomepage() {
        if (hasPluginConfig()) {
            return _csConfig.getPluginConfig().getPluginHomepage();
        }
        else {
            return null;
        }
    }

    public void setPluginHomepage(String page) {
        _csConfig.getPluginConfig().setPluginHomepage(page);
        fireModelChanged();
    }

    @DataProvider(objectField = "pluginAuthenticationSource")
    public List<AuthPlugin> populatePluginAuthenticationSources() {

        List<AuthPlugin> list = new ArrayList<AuthPlugin>();
        list.add(EMPTY_AUTH_PLUGIN);
        list.add(new AuthPlugin(PluginConfig.AUTHSOURCE_DEFAULT_DOMAIN, "Default Domain"));

        if (isPluginUsageAsAuthenticationSource()) {
            list.add(new AuthPlugin(_csConfig.getPluginConfig().getId().getUniqueName(), "Myself"));
        }

        Iterator<PluginID> dependencies = getPluginDependencies().iterator();
        while (dependencies.hasNext()) {
            PluginID id = dependencies.next();
            list.add(new AuthPlugin(id.getUniqueName(), id.getUniqueName()));
        }
        return list;
    }

    public AuthPlugin getPluginAuthenticationSource() {
        if (hasPluginConfig()) {
            String authString = _csConfig.getPluginConfig().getAuthentication();
            if (authString == null || authString.trim().equalsIgnoreCase("")) {
                authString = STRING_NOT_SET;
            }
            Iterator<AuthPlugin> authPlugins = populatePluginAuthenticationSources().iterator();
            while (authPlugins.hasNext()) {
                AuthPlugin plugin = authPlugins.next();
                if (plugin.getKey().equals(authString)) {
                    return plugin;
                }
            }
            return EMPTY_AUTH_PLUGIN;
        }
        else {
            return null;
        }
    }

    public void setPluginAuthenticationSource(AuthPlugin auth) {
        if (auth != null && !EMPTY_AUTH_PLUGIN.equals(auth)) {
            _csConfig.getPluginConfig().setAuthentication(auth.getKey());
            fireModelChanged();
        }
        else if (EMPTY_AUTH_PLUGIN.equals(auth)) {
            _csConfig.getPluginConfig().setAuthentication(null);
            fireModelChanged();
        }
    }

    public boolean isPluginUsageAsAuthenticationSource() {
        if (hasPluginConfig()) {
            return _csConfig.getPluginConfig().isUsageAsAuthSource();
        }
        else {
            return false;
        }
    }
   
    public boolean isStaticClasspath() {
        if (_csConfig instanceof de.innovationgate.wga.common.beans.csconfig.v3.CSConfig) {
            return ((de.innovationgate.wga.common.beans.csconfig.v3.CSConfig) _csConfig).isStaticClasspath();
        } else {
          return false;
        }
       
    }
   
    public void setStaticClasspath(boolean value) {
        if (_csConfig instanceof de.innovationgate.wga.common.beans.csconfig.v3.CSConfig) {
            ((de.innovationgate.wga.common.beans.csconfig.v3.CSConfig) _csConfig).setStaticClasspath(value);
        }
        fireModelChanged();
    }

    public void setPluginUsageAsAuthenticationSource(boolean enabled) {
        _csConfig.getPluginConfig().setUsageAsAuthSource(enabled);
        fireModelChanged();
    }

    public boolean isPluginUsageAsContentStore() {
        if (hasPluginConfig()) {
            return _csConfig.getPluginConfig().isUsageAsContentStore();
        }
        else {
            return false;
        }
    }

    public void setPluginUsageAsContentStore(boolean enabled) {
        _csConfig.getPluginConfig().setUsageAsContentStore(enabled);
        fireModelChanged();
    }

    public boolean isPluginUsageAsDesignProvider() {
        if (hasPluginConfig()) {
            return _csConfig.getPluginConfig().isUsageAsDesignProvider();
        }
        else {
            return false;
        }
    }

    public void setPluginUsageAsDesignProvider(boolean enabled) {
        _csConfig.getPluginConfig().setUsageAsDesignProvider(enabled);
        fireModelChanged();
    }

    @DataProvider(objectField = "pluginPersonalisationMode")
    public List<PersonalisationMode> populatePluginPersonalisationModes() {
        List<PersonalisationMode> modes = new ArrayList<PersonalisationMode>(PERSONALISATION_MODES.values());
        return modes;
    }

    public PersonalisationMode getPluginPersonalisationMode() {
        if (hasPluginConfig()) {
            int mode = _csConfig.getPluginConfig().getPersonalisationMode();
            Iterator<PersonalisationMode> encodings = populatePluginPersonalisationModes().iterator();
            while (encodings.hasNext()) {
                PersonalisationMode persMode = encodings.next();
                if (persMode.getKey().equals(mode)) {
                    return persMode;
                }
            }
            return null;
        }
        else {
            return null;
        }
    }

    public void setPluginPersonalisationMode(PersonalisationMode mode) {
        if (mode != null) {
            _csConfig.getPluginConfig().setPersonalisationMode(mode.getKey());
            fireModelChanged();
        }
    }

    public boolean isAllowContentAuthoring() {
        if (hasPluginConfig()) {
            return _csConfig.getPluginConfig().isShowOnStartPage();
        }
        else {
            return false;
        }
    }

    public void setAllowContentAuthoring(boolean enabled) {
        _csConfig.getPluginConfig().setShowOnStartPage(enabled);
        fireModelChanged();
    }

    @SuppressWarnings("unchecked")
    @NotBound
    public List<EncoderMapping> getEncoderMappings() {
        return _csConfig.getEncoderMappings();
    }

    @SuppressWarnings("unchecked")
    @NotBound
    public List<ElementMapping> getElementMappings() {
        return _csConfig.getElementMappings();
    }

    @SuppressWarnings("unchecked")
    @NotBound
    public List<MediaKey> getMediaKeys() {
        return _csConfig.getMediaKeys();
    }

    @SuppressWarnings("unchecked")
    @NotBound
    public List<RemoteAction> getRemoteActions() {
        return _csConfig.getRemoteActions();
    }

    @SuppressWarnings("unchecked")
    @NotBound
    public List<ACLRole> getACLRoles() {
        return _csConfig.getRoles();
    }

    @SuppressWarnings("unchecked")
    @NotBound
    public List<JobDefinition> getJobDefinitions() {
        return _csConfig.getJobDefinitions();
    }

    @SuppressWarnings("unchecked")
    @NotBound
    public List<Shortcut> getShortcuts() {
        if (_csConfig instanceof de.innovationgate.wga.common.beans.csconfig.v2.CSConfig) {
            return ((de.innovationgate.wga.common.beans.csconfig.v2.CSConfig) _csConfig).getShortcuts();
        }
        return null;
    }
   
    public String getDisconnectionScript() {
        if (_csConfig instanceof de.innovationgate.wga.common.beans.csconfig.v3.CSConfig) {
            return ((de.innovationgate.wga.common.beans.csconfig.v3.CSConfig) _csConfig).getDisconnectionScript();
        }
        return null;
    }
   
    public boolean isPluginClearDatabaseOnUpdate() {     
        if (_csConfig.getPluginConfig() != null && _csConfig.getPluginConfig() instanceof de.innovationgate.wga.common.beans.csconfig.v3.PluginConfig) {
          return ((de.innovationgate.wga.common.beans.csconfig.v3.PluginConfig) _csConfig.getPluginConfig()).isClearDatabaseOnUpdate();
        }
        return false;
    }
   
    public boolean isPluginDisableInit() {
        if (_csConfig.getPluginConfig() != null && _csConfig.getPluginConfig() instanceof de.innovationgate.wga.common.beans.csconfig.v3.PluginConfig) {
            return ((de.innovationgate.wga.common.beans.csconfig.v3.PluginConfig) _csConfig.getPluginConfig()).isDisablePluginInit();
        }
        return false;
    }

    public boolean isFeatureSupported(String featureID) {
        if (featureID.equals(FEATURE_SHORTCUTS)) {
            return (_csConfig instanceof de.innovationgate.wga.common.beans.csconfig.v2.CSConfig);
        }
        else {
            return false;
        }
    }

    public File getPluginExportFile(File targetDir, boolean shortFileName) {
        String fileName;
        if (shortFileName) {
            fileName = _csConfig.getPluginConfig().getId().buildShortFileName();
        }
        else {
            fileName = _csConfig.getPluginConfig().getId().buildQualifiedFileName();
        }
        return new File(targetDir, fileName);
    }

    public void exportPlugin(File pluginFile, int buildnumber, boolean obfuscate, boolean increaseBuild, String javaSourceFolder) throws IOException, PersistentKeyException, GeneralSecurityException {                               
            // Update build number
            if (_pluginBuild != buildnumber) {
                setPluginBuild(buildnumber);
                saveChanges();
            }

            ZipOutputStream stream = createZIP(pluginFile, obfuscate, javaSourceFolder);
            stream.close();
           
            // Increment build version
            if (increaseBuild) {
              setPluginBuild(getPluginBuild() + 1);
              saveChanges();  
            }
                          
    }
   
    public void exportPlugin(File pluginFile, int buildnumber, boolean obfuscate, boolean increaseBuild) throws IOException, PersistentKeyException, GeneralSecurityException {
        exportPlugin(pluginFile, buildnumber, obfuscate, increaseBuild, null);
    }
   
    public File getDesignExportFile(File targetDir) {
      return new File(targetDir, _syncInfo.getDesignKey() + ".wgadesign");
    }
   
    public void exportDesign(File file, Properties buildProperties) throws FileNotFoundException, PersistentKeyException, GeneralSecurityException, IOException {
      ZipOutputStream stream = createZIP(file, false, null);
      ZipEntry buildPropertiesEntry = new ZipEntry("files/system/build.properties");
      stream.putNextEntry(buildPropertiesEntry);
      ByteArrayOutputStream propertiesContent = new ByteArrayOutputStream();
      buildProperties.store(propertiesContent, null);
      stream.write(propertiesContent.toByteArray());
      stream.close();
    }
   
    public Properties createBuildProperties() {
      Properties properties = new Properties();
      properties.setProperty(BUILD_PROP_SIGNATURE,  System.getProperty("user.name") + "_" + new SimpleDateFormat("yyyyMMdd_HHmm").format(new Date()));
      return properties;
    }

  private ZipOutputStream createZIP(File zipFile, boolean obfuscate, String javaSourceFolder) throws FileNotFoundException, PersistentKeyException, GeneralSecurityException, IOException {
      if (!zipFile.exists()) {
        zipFile.createNewFile();
      }

    ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile));
    zipOutputStream.setLevel(0);
   
    DirZipper zipper = new DirZipper();
    zipper.setInputStreamProvider(new PluginISProvider(_designDirectory, obfuscate));
    zipper.addFilePatternToIgnore("^\\..*$");
   
    // Zip all folders and files that belong into an exported plugin
    List<File> filesToZip = new ArrayList<File>();
    filesToZip.add(new File(_designDirectory, DesignDirectory.FOLDERNAME_TML));
    filesToZip.add(new File(_designDirectory, DesignDirectory.FOLDERNAME_SCRIPT));
    filesToZip.add(new File(_designDirectory, DesignDirectory.FOLDERNAME_FILES));
    filesToZip.add(new File(_designDirectory, DesignDirectory.SYNCINFO_FILE));
    filesToZip.add(new File(_designDirectory, DesignDirectory.DESIGN_DEFINITION_FILE));
    Iterator<File> files = filesToZip.iterator();
    while (files.hasNext()) {
        File file = (File) files.next();
        if (file.exists()) {
            if (file.isDirectory()) {
                zipper.zipDirectory(file, zipOutputStream, file.getName() + "/");
            }
            else {
                zipper.zipFile(file, zipOutputStream);
            }
           
        }
    }
   
    // If the design directory has a java classes folder we put those into a jar and zip them to "files/system"
    File javaClassesDir = new File(_designDirectory, DesignDirectory.FOLDERNAME_JAVA);
    if (javaClassesDir.exists() && javaClassesDir.isDirectory()) {
        TemporaryFile jar = new TemporaryFile("plugin-classes.jar", null, null);
        ZipOutputStream jarStream = new ZipOutputStream(new FileOutputStream(jar.getFile()));
        jarStream.setLevel(0);
        DirZipper jarZipper = new DirZipper();
        jarZipper.addFilePatternToIgnore("^\\..*$");
        jarZipper.zipDirectory(javaClassesDir, jarStream);
       
        jarStream.close();
       
        zipper.zipNormalFile(jar.getFile(), zipOutputStream, "files/system/");
       
        jar.delete();
    }
   
    // If we were given a java source folder we put its contents into plugin-sources.zip and put that into "files/system" (for OSS plugins)
        if (javaSourceFolder != null) {
            File javaSourceDir = new File(javaSourceFolder);
            TemporaryFile zip = new TemporaryFile("plugin-sources.zip", null, null);
            ZipOutputStream zipStream = new ZipOutputStream(new FileOutputStream(zip.getFile()));
            zipStream.setLevel(0);
            DirZipper jarZipper = new DirZipper();
            jarZipper.addFilePatternToIgnore("^\\..*$");
            jarZipper.zipDirectory(javaSourceDir, zipStream);
           
            zipStream.close();
           
            zipper.zipNormalFile(zip.getFile(), zipOutputStream, "files/system/");
           
            zip.delete();
        }
   

   

    if (obfuscate) {
        ZipEntry anEntry = new ZipEntry(DesignDirectory.OBFUSCATE_FLAGFILE);
        zipOutputStream.putNextEntry(anEntry);
        zipOutputStream.write("Obfuscation flagfile".getBytes());
    }
   
    return zipOutputStream;
  }

    @Override
    public void reload() throws IOException {
        _syncInfo = DesignDefinition.load(_syncInfoFile);
        _designDirectory = _syncInfoFile.getParentFile();
        _csConfigFile = new File(_designDirectory, "files/system/csconfig.xml");
        if (!_csConfigFile.exists()) {
            File files = new File(_designDirectory, "files");
            if (!files.exists()) {
                files.mkdir();
            }
            File system = new File(files, "system");
            if (!system.exists()) {
                system.mkdir();
            }
            _csConfig = new CSConfig();
            _csConfig.write(_csConfigFile);
        }
        else {
            try {
                _csConfig = CSConfig.load(_csConfigFile);
            }
            catch (InvalidCSConfigVersionException e) {
                IOException io = new IOException("Invalid cs config version: " + e.getMessage());
                io.setStackTrace(e.getStackTrace());
                throw io;
            }
        }

        loadPluginVersionProperties();

        try {
            ensureCorrectCSConfigVersion();
        }
        catch (Exception e) {
            IOException ioe = new IOException(e.getMessage());
            ioe.setStackTrace(e.getStackTrace());
            throw ioe;
        }

        // migrate design encoding to csConfig
        String encValue = _syncInfo.getFileEncoding();
        if (encValue != null && !encValue.trim().equalsIgnoreCase("csconfig")) {
            _syncInfo.setFileEncoding(null);
            if (encValue.trim().equals("")) {
                removePublisherOption(PublisherOption.OPTION_DESIGN_ENCODING);
            }
            else {
                setPublisherOption(PublisherOption.OPTION_DESIGN_ENCODING, encValue, false);
            }
        } else if (encValue == null) {
          // design encoding is not defined in _syncinfo -> syncinfo has priority so change model to reflect correct encoding
          setPublisherOption(PublisherOption.OPTION_DESIGN_ENCODING, STRING_NOT_SET, false);
        }
    }

    @Override
    public List<ValidationError> validate() {
        List<ValidationError> errors = new ArrayList<ValidationError>();

        if (hasPluginConfig()) {
            try {
                Version newV = new Version(_pluginVersion);
                newV.setBuildVersion(_pluginBuild);
                _csConfig.getPluginConfig().getId().setVersion(newV);
            }
            catch (RuntimeException e) {
                // unparsable version
                errors.add(new ValidationError("Invalid version format.", new String[] { "pluginVersion" }));
            }

            try {
                Version newV = new Version(_pluginWGAVersion);
                _csConfig.getPluginConfig().setMinimumWGAVersion(newV);
            }
            catch (RuntimeException e) {
                // unparsable version
                errors.add(new ValidationError("Invalid version format.", new String[] { "pluginWGAVersion" }));
            }

            try {
                Version newV = new Version(_pluginJavaVersion);
                _csConfig.getPluginConfig().setMinimumJavaVersion(newV);
            }
            catch (RuntimeException e) {
                // unparsable version
                errors.add(new ValidationError("Invalid version format.", new String[] { "pluginJavaVersion" }));
            }
           
            if (isPluginDisableInit()) {
              if (!(_csConfig.getPluginConfig() instanceof de.innovationgate.wga.common.beans.csconfig.v3.PluginConfig)) {
                errors.add(new ValidationError("Plugin initialisation functionalities can only be disabled with WGA5 compatibility or higher.", new String[] { "versionCompliance", "pluginDisableInit"}));
              }
            }
           
            if (isPluginClearDatabaseOnUpdate()) {
              if (!(_csConfig.getPluginConfig() instanceof de.innovationgate.wga.common.beans.csconfig.v3.PluginConfig)) {
                errors.add(new ValidationError("Clear plugin database on plugin update can only be enabled with WGA5 compatibility or higher.", new String[] { "versionCompliance", "pluginClearDatabaseOnUpdate"}));
              }
            }

        }

        // Prevent HDB enabling with Non-WGA4-Compatibility
        PublisherOption option = _csConfig.findPublisherOption("isHDB");
        boolean hdbEnabled = (option != null && option.getValue().equals("true"));
        if (hdbEnabled) {
            if (_csConfig.getVersionCompliance().equals(CSConfig.VERSIONCOMPLIANCE_WGA3)) {
                errors.add(new ValidationError("HDB interface can only be enabled with WGA4 compatibility or higher.", new String[] { "versionCompliance", "usesHDB" }));
            }
        }
       
        // Prevent Disconnection Script with Non-WGA5-Compatibility
        if (getDisconnectionScript() != null && getDisconnectionScript().trim().length() > 0) {
          if (!(_csConfig instanceof de.innovationgate.wga.common.beans.csconfig.v3.CSConfig)) {
            errors.add(new ValidationError("Disconnection script can only be enabled with WGA5 compatibility or higher.", new String[] { "versionCompliance", "disconnectionScript"}));
          }
        }

        if (isStaticClasspath()) {
          if (!(_csConfig instanceof de.innovationgate.wga.common.beans.csconfig.v3.CSConfig)) {
            errors.add(new ValidationError("Prevent reloading of java libraries can only be enabled with WGA5 compatibility or higher.", new String[] { "versionCompliance", "staticClasspath"}));
          }
        }
       
        // Prevent Admin App enabling with Non-WGA5-Compat.
        if (isAdminApp()) {
            if (_csConfig.getVersionCompliance().equals(CSConfig.VERSIONCOMPLIANCE_WGA3) ||
                    _csConfig.getVersionCompliance().equals(CSConfig.VERSIONCOMPLIANCE_WGA4) ||
                    _csConfig.getVersionCompliance().equals(CSConfig.VERSIONCOMPLIANCE_WGA41)) {
                errors.add(new ValidationError("Admin application can only be enabled for WGA5 compatibility or higher.", new String[] { "versionCompliance", "adminApp" }));
            }
        }
       
        // Validate role names
        Iterator<ACLRole> roles = getACLRoles().iterator();
        while (roles.hasNext()) {
            ACLRole role = (ACLRole) roles.next();
            if (!Constants.PATTERN_ROLENAMES.matcher(role.getName()).matches()) {
                errors.add(new ValidationError("Role name '" + role.getName() + "' is invalid. Use only international alphanumeric characters and the symbols -, _, $, #, [ and ]",
                        new String[] { "aclRoles" }));
            }
        }

        if (_csConfig instanceof de.innovationgate.wga.common.beans.csconfig.v2.CSConfig) {
            de.innovationgate.wga.common.beans.csconfig.v2.CSConfig v2 = (de.innovationgate.wga.common.beans.csconfig.v2.CSConfig) _csConfig;
            Iterator<Shortcut> shortcuts = v2.getShortcuts().iterator();
            while (shortcuts.hasNext()) {
                Shortcut shortcut = shortcuts.next();
                if (WGUtils.isEmpty(shortcut.getShortcut())) {
                    errors.add(new ValidationError("A shortcut has no name", new String[] { "shortcuts" }));
                }
                else if (WGUtils.isEmpty(shortcut.getReference())) {
                    errors.add(new ValidationError("The shortcut '" + shortcut.getShortcut() + "' has no reference", new String[] { "shortcuts" }));
                }
                else if (shortcut.getType() == Shortcut.TYPE_TMLSCRIPT_GLOBAL) {
                    if (!Character.isUpperCase(shortcut.getShortcut().charAt(0))) {
                        errors.add(new ValidationError("The TMLScript global shortcut '" + shortcut.getShortcut() + "' must start with an uppercase letter.", new String[] { "shortcuts" }));
                    }
                }

            }
        }

        if (hasPluginConfig()) {
            // Validate plugin id
            PluginConfig pc = _csConfig.getPluginConfig();
            PluginID id = pc.getId();

            errors.addAll(validatePluginUniqueName(getPluginUniqueName()));

            // Validate plugin dependencies
            Iterator<PluginID> dependencies = pc.getDependencies().iterator();
            while (dependencies.hasNext()) {
                PluginID did = (PluginID) dependencies.next();
                if (did.getUniqueName() == null || did.getUniqueName().trim().equals("")) {
                    errors.add(new ValidationError("Unique name of plugin dependency should not be empty.", new String[] { "pluginDependencies" }));
                }
                else if (did.getUniqueName().contains(" ")) {
                    errors.add(new ValidationError("Unique name of plugin dependency should not contain white spaces.", new String[] { "pluginDependencies" }));
                }
            }

            // Validate relation between minimum WGA version and WGA
            // compatibility version
            float minimumWGAVersion = Float.valueOf(pc.getMinimumWGAVersion().getMajorVersion() + "." + pc.getMinimumWGAVersion().getMinorVersion());
            float compatibilityWGAVersion = Float.valueOf(_csConfig.getVersionCompliance().substring(3));
            if (minimumWGAVersion < compatibilityWGAVersion) {
                errors.add(new ValidationError("The minimum WGA version in plugin configuration is lower than the version chosen in 'Developed for WGA version'", new String[] { "pluginWGAVersion",
                        "versionCompliance" }));
            }
           
            if (_csConfig.getPluginConfig().getMinimumWGAVersion().isAtLeast(5, 0)&& !_csConfig.getPluginConfig().getMinimumJavaVersion().isAtLeast(1, 5)) {
                errors.add(new ValidationError("Invalid Java version. OpenWGA >= 5.0 requires Java 1.5.0 or higher.", new String[] { "pluginJavaVersion" }));
            }

        }
       
        // Prevent BrowsingSecurity other than default for WGA < 5.3.
        if (!getBrowsingSecurity().getKey().equals(BrowsingSecurity.FULL_ACCESS)) {
            if (!getVersionCompliance().toWGAVersion().isAtLeast(5, 3)) {
                errors.add(new ValidationError("Browsing security can only be enabled for WGA 5.3 compatibility or higher.", new String[] { "versionCompliance", "browsingSecurity" }));
            }
        }

        return errors;
    }
   
    public static List<ValidationError> validatePluginUniqueName(String uname) {
      List<ValidationError> errors = new ArrayList<ValidationError>();
      if (uname == null || uname.trim().equals("")) {
            errors.add(new ValidationError("Plugin unique name should not be empty.", new String[] { "pluginUniqueName" }));
        }
        else if (uname.contains(" ")) {
            errors.add(new ValidationError("Plugin unique name should not contain white spaces.", new String[] { "pluginUniqueName" }));
        } else if (uname.endsWith(".")) {
          errors.add(new ValidationError("Plugin unique name should not end with an '.'", new String[] { "pluginUniqueName" }));
        }
      return errors;
    }

    private void ensureCorrectCSConfigVersion() throws IllegalAccessException, InvocationTargetException {     
        // cache current shortcuts in memory - necessary to restore them if
        // vesion compliance is changed multiple times without save
        if (getShortcuts() != null) {
            _cachedShortcuts = getShortcuts();
        }

        // Convert CsConfig and PluginConfig object to proper version for WGA version compliance
        if (!_csConfig.getClass().equals(CSConfig.getCSConfigClassForCompliance(_csConfig.getVersionCompliance()))) {
           
            CSConfig correctVersionCSConfig = CSConfig.instantiateCSConfigForCompliance(_csConfig.getVersionCompliance());
            BeanUtils.copyProperties(correctVersionCSConfig, _csConfig);
           
            if (_csConfig.getPluginConfig() != null) {
                PluginConfig correctVersionPluginConfig = PluginConfig.instantiatePluginConfigForCompliance(_csConfig.getVersionCompliance());
                BeanUtils.copyProperties(correctVersionPluginConfig, _csConfig.getPluginConfig());
                correctVersionCSConfig.setPluginConfig(correctVersionPluginConfig);
            }
                       
            _csConfig = correctVersionCSConfig;

            // reload cached design shortcuts
            if (isFeatureSupported(FEATURE_SHORTCUTS) && _cachedShortcuts != null && _cachedShortcuts.size() > 0 && getShortcuts().size() == 0) {
                getShortcuts().addAll(_cachedShortcuts);
            }

            fireModelChanged();
        }
       
    }

  @Override
  public boolean isEditable(String propertyName) {
    if (propertyName != null) {
      if (propertyName.equals("usesHDB")) {
        if (_csConfig instanceof de.innovationgate.wga.common.beans.csconfig.v3.CSConfig) {
          return false;
        } else if (_csConfig.getVersionCompliance().equals(CSConfig.VERSIONCOMPLIANCE_WGA3)) {
          return false;
        }
         
      } else if (propertyName.equals("staticClasspath")) {
        if (!(_csConfig instanceof de.innovationgate.wga.common.beans.csconfig.v3.CSConfig)) {
          return false;
        }
      } else if (propertyName.equals("pluginDisableInit")) {
        if (!(_csConfig instanceof de.innovationgate.wga.common.beans.csconfig.v3.CSConfig)) {
          return false;
        }
      } else if (propertyName.equals("pluginClearDatabaseOnUpdate")) {
        if (!(_csConfig instanceof de.innovationgate.wga.common.beans.csconfig.v3.CSConfig)) {
          return false;
        }
      } else if (propertyName.equals("disconnectionScript")) {
        if (!(_csConfig instanceof de.innovationgate.wga.common.beans.csconfig.v3.CSConfig)) {
          return false;
        }
      } else if (propertyName.equals("adminApp")) {
          if (_csConfig.getVersionCompliance().equals(CSConfig.VERSIONCOMPLIANCE_WGA3) ||
                  _csConfig.getVersionCompliance().equals(CSConfig.VERSIONCOMPLIANCE_WGA4) ||
                  _csConfig.getVersionCompliance().equals(CSConfig.VERSIONCOMPLIANCE_WGA41)) {
              return false;
          } else {
              return true;
          }
      }
//      } else if (propertyName.equals("browsingSecurity")) {
//          if (getVersionCompliance().toWGAVersion().isAtLeast(5, 3)) {
//              return true;
//          } else {
//              return false;
//          }
//            }
    }
    return true;
  }

    public boolean isAdminApp() {
        return Boolean.parseBoolean(getPublisherOptionValue(PublisherOption.OPTION_ADMIN_APP));
    }

    public void setAdminApp(boolean enabled) {
        setPublisherOption(PublisherOption.OPTION_ADMIN_APP, enabled);
    }
   
    public void setBrowsingSecurity(BrowsingSecurity level) {
        setPublisherOption(PublisherOption.OPTION_BROWSING_SECURITY, level.getKey());
    }
 
    public BrowsingSecurity getBrowsingSecurity() {
        return BROWSINGSECURITY_VALUES.get(Integer.parseInt(getPublisherOptionValue(PublisherOption.OPTION_BROWSING_SECURITY)));
    }
   
    @DataProvider(objectField = "browsingSecurity")
    public List<BrowsingSecurity> provideBrowsingSecurityValues() {
        List<BrowsingSecurity> list = new ArrayList<BrowsingSecurity>(BROWSINGSECURITY_VALUES.values());
        return list;
    }
}
TOP

Related Classes of de.innovationgate.wga.model.WGADesignConfigurationModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.