/*******************************************************************************
 * Copyright (c) 2016 Weasis Team and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Nicolas Roduit - initial API and implementation
 *******************************************************************************/
package org.weasis.acquire.explorer;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Hashtable;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.weasis.core.api.explorer.DataExplorerViewFactory;
import org.weasis.core.api.service.BundleTools;
import org.weasis.core.api.util.FileUtil;
import org.weasis.core.api.util.GzipManager;

@Component(immediate = false)
@Service
@Properties(value = { @Property(name = "service.name", value = "Media Dicomizer"),
    @Property(name = "service.description", value = "Import media and dicomize them") })
public class MediaImporterFactory implements DataExplorerViewFactory {
    private static final Logger LOGGER = LoggerFactory.getLogger(MediaImporterFactory.class);

    private AcquisitionView explorer = null;

    @Override
    public AcquisitionView createDataExplorerView(Hashtable<String, Object> properties) {
        if (explorer == null) {
            initGlobalTags();
            explorer = new AcquisitionView();
            explorer.initImageGroupPane();
        }
        return explorer;
    }

    @Activate
    protected void activate(ComponentContext context) {
        // Do nothing
    }

    @Deactivate
    protected void deactivate(ComponentContext context) {
        if (explorer != null) {
            explorer.saveLastPath();
        }
    }

    private void initGlobalTags() {
        String xml = BundleTools.SYSTEM_PREFERENCES.getProperty("weasis.patient.context"); //$NON-NLS-1$

        if (xml == null) {
            // TODO read service
        } else {

            InputStream stream = null;
            try {
                byte[] buf = null;
                if (Boolean
                    .valueOf(BundleTools.SYSTEM_PREFERENCES.getProperty("weasis.patient.context.gzip", "TRUE"))) {
                    buf = GzipManager
                        .gzipUncompressToByte(Base64.getDecoder().decode(xml.getBytes(StandardCharsets.UTF_8)));
                } else {
                    buf = xml.getBytes(StandardCharsets.UTF_8);
                }
                stream = new ByteArrayInputStream(buf);

                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(stream);

                AcquireManager.GLOBAL.init(doc);
            } catch (Exception e) {
                LOGGER.error("Loading gobal tags", e);
            } finally {
                FileUtil.safeClose(stream);
            }
        }
    }

}
@@ -2764,19 +2764,38 @@
     
-if (B
+b
 oolean
+ isPatientContextGzip =
 %0A   
@@ -2811,16 +2811,23 @@
         
+Boolean
 .valueOf
@@ -2909,19 +2909,18 @@
 %22TRUE%22))
-) %7B
+;%0A
 %0A       
@@ -2932,76 +2932,183 @@
     
-    buf = GzipManager%0A                        .gzipUncompressToByte(
+if (isPatientContextGzip) %7B%0A                    // byte%5B%5D byteArray = Base64.getDecoder().decode(xml.getBytes(StandardCharsets.UTF_8));%0A                    byte%5B%5D byteArray = 
 Base
@@ -3105,32 +3105,35 @@
 ray = Base64.get
+Url
 Decoder().decode
@@ -3170,16 +3170,87 @@
 .UTF_8))
+;%0A%0A                    buf = GzipManager.gzipUncompressToByte(byteArray
 );%0A     

use of base64 decoder with URL and Filename safe type for weasis.patient.context parameters