PreviewManager.java :  » Content-Management-System » contelligent » de » finix » contelligent » client » base » Java Open Source

Java Open Source » Content Management System » contelligent 
contelligent » de » finix » contelligent » client » base » PreviewManager.java
/*
 * Copyright 2001-2006 C:1 Financial Services GmbH
 *
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License Version 2.1, as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
 */

package de.finix.contelligent.client.base;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.xml.sax.helpers.AttributesImpl;

import de.finix.contelligent.client.event.ConfigurationEvent;
import de.finix.contelligent.client.event.ConfigurationEventListener;
import de.finix.contelligent.client.remote.ActionResult;
import de.finix.contelligent.client.remote.Actions;
import de.finix.contelligent.client.remote.RemoteActionException;
import de.zeigermann.xml.simpleImporter.SimpleImportHandler;
import de.zeigermann.xml.simpleImporter.SimplePath;

public final class PreviewManager implements ConfigurationEventListener, SessionListener {
    private static Logger logger = Logger.getLogger(PreviewManager.class.getName());

    private final static String DOMAIN = "preview";

    private static PreviewManager previewManager = null;

    private List<Preview> previews;

    private PreviewManager() {
        ConfigurationManager.getInstance().addConfigurationEventListener(this, false);
    }

    public static PreviewManager getInstance() {
        if (previewManager == null) {
            previewManager = new PreviewManager();
            try {
                previewManager.load();
            } catch (RemoteActionException rae) {
                logger.log(Level.SEVERE, "Could not load PreviewManager", rae);
            }
            Session.getInstance().addSessionListener(previewManager);
        }
        return previewManager;
    }

    public void sessionChanged(Session session) {
        try {
            load();
        } catch (RemoteActionException rae) {
            logger.log(Level.SEVERE, "Could not update PreviewManager", rae);
        }
    }

    public synchronized void load() throws RemoteActionException {
        previews = new ArrayList<Preview>();
        PreviewHandler handler = new PreviewHandler(previews);
        ActionResult actionResult = Actions.loadConfiguration(DOMAIN, handler);
    }

    public synchronized List getPreviews() {
        return Collections.unmodifiableList(previews);
    }

    public void onConfigurationAdded(ConfigurationEvent event) {
    }

    public void onConfigurationRemoved(ConfigurationEvent event) {
    }

    public void onConfigurationChanged(ConfigurationEvent event) {
        if (event.getDomain().equals(DOMAIN)) {
            try {
                load();
            } catch (RemoteActionException rae) {
                logger.log(Level.SEVERE, "Could not load PreviewManager", rae);
            }
        }
    }

    public final static class Preview {
        public String path;

        public String display;

        public String role;

        public String date;

        public Map<String,String> categoryMap = new HashMap<String,String>();

        public Preview(String path) {
            this.path = path;
        }

        public String getPath() {
            return path;
        }

        public String getDisplay() {
            return display;
        }

        public void setDisplay(String display) {
            this.display = display;
        }

        public String getRole() {
            return role;
        }

        public void setRole(String role) {
            this.role = role;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public Map getCategoryMap() {
            return categoryMap;
        }

        public void addCategorySetting(String name, String value) {
            categoryMap.put(name, value);
        }
    }

    public final class PreviewHandler implements SimpleImportHandler {
        private Preview preview;

        private List<Preview> previews;

        public PreviewHandler(List<Preview> previews) {
            this.previews = previews;
        }

        public void startDocument() {
        }

        public void endDocument() {
        }

        public void cData(SimplePath path, String cdata) {
        }

        public void startElement(SimplePath path, String name, AttributesImpl attrs, String leadingCData) {
            try {
                if (path.matches("config")) {
                    preview = new Preview(attrs.getValue("path"));
                } else if (path.matches("preview/display")) {
                    preview.setDisplay(leadingCData);
                } else if (path.matches("preview/category")) {
                    preview.addCategorySetting(attrs.getValue("name"), attrs.getValue("value"));
                } else if (path.matches("preview/role")) {
                    preview.setRole(leadingCData);
                } else if (path.matches("preview/date")) {
                    preview.setDate(leadingCData);
                }
            } catch (Throwable t) {
                logger.log(Level.SEVERE, "Caught exception while preview import", t);
            }
        }

        public void endElement(SimplePath path, String name) {
            if (path.matches("preview") && preview != null) {
                previews.add(preview);
            }
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.