/**
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2015 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.report;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

import org.eclipse.birt.report.model.api.IResourceLocator;
import org.eclipse.birt.report.model.api.ModuleHandle;
import org.eclipse.datatools.connectivity.oda.flatfile.ResourceLocator;

import com.axelor.app.AppSettings;
import com.axelor.common.ClassUtils;

/**
 * This is a {@link ResourceLocator} that first searches external reports
 * directory for the resources and fallbacks to module resources if not found.
 *
 */
public class ReportResourceLocator implements IResourceLocator {

	public static final String CONFIG_REPORT_DIR = "axelor.report.dir";
	public static final String DEFAULT_REPORT_DIR = "{user.home}/axelor/reports";

	private Path searchPath;

	public ReportResourceLocator() {
		final String dir = AppSettings.get().getPath(CONFIG_REPORT_DIR,
				DEFAULT_REPORT_DIR);
		this.searchPath = Paths.get(dir);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public URL findResource(ModuleHandle moduleHandle, String fileName,
			int type, Map appContext) {
		return findResource(moduleHandle, fileName, type);
	}

	@Override
	public URL findResource(ModuleHandle moduleHandle, String fileName, int type) {

		Path path = searchPath;
		String sub = ".";

		switch (type) {
		case IResourceLocator.LIBRARY:
			sub = "lib";
			break;
		case IResourceLocator.IMAGE:
			sub = "img";
			break;
		case IResourceLocator.CASCADING_STYLE_SHEET:
			sub = "css";
			break;
		}

		path = path.resolve(sub).normalize();

		// first search in the external repository
		File found = path.resolve(fileName).toFile();

		if (!found.exists()) {
			found = searchPath.resolve(fileName).toFile();
		}

		if (found.exists()) {
			try {
				return found.toURI().toURL();
			} catch (MalformedURLException e) {
			}
		}

		// otherwise locate from the modules
		path = Paths.get("reports", sub, fileName).normalize();

		return ClassUtils.getResource(path.toString());
	}

}

@@ -2012,34 +2012,8 @@
  %7B%0A%0A
-%09%09Path path = searchPath;%0A
 %09%09St
@@ -2245,49 +2245,8 @@
 %09%7D%0A%0A
-%09%09path = path.resolve(sub).normalize();%0A%0A
 %09%09//
@@ -2270,23 +2270,17 @@
 the 
-external reposi
+top direc
 tory
@@ -2295,17 +2295,23 @@
 found = 
-p
+searchP
 ath.reso
@@ -2335,16 +2335,50 @@
 ile();%0A%0A
+%09%09// else search in sub directory%0A
 %09%09if (!f
@@ -2416,32 +2416,57 @@
 rchPath.resolve(
+sub).normalize().resolve(
 fileName).toFile
@@ -2635,15 +2635,41 @@
 s%0A%09%09
-path = 
+URL url = ClassUtils.getResource(
 Path
@@ -2688,13 +2688,8 @@
 ts%22,
- sub,
  fil
@@ -2710,19 +2710,51 @@
 ze()
-;%0A%0A%09%09return
+.toString());%0A%09%09if (url == null) %7B%0A%09%09%09url =
  Cla
@@ -2777,24 +2777,86 @@
 rce(
-path.toString())
+Paths.get(%22reports%22, sub, fileName).normalize().toString());%0A%09%09%7D%0A%0A%09%09return url
 ;%0A%09%7D

Fix birt report resource locator