<commit_before>import fs = require('fs');
import http = require('http');
import express = require('express');
import request = require('request');
import pg = require('pg');
import proj4 = require('proj4');
import MessageBus = require('../bus/MessageBus');
import ConfigurationService = require('../configuration/ConfigurationService');
import Location = require('../database/Location');
import BagDatabase = require('../database/BagDatabase');
import LocalBag = require('../database/LocalBag');
import IBagOptions = require('../database/IBagOptions');
import IGeoJsonFeature = require('./IGeoJsonFeature');
import Api = require('../api/ApiManager');
import Utils = require('../helpers/Utils');
import async = require('async');
import winston = require('winston');
import path = require('path');
import IProperty = Api.IProperty;

export interface ILayerDefinition {
    projectTitle: string;
    reference: string;
    group: string;
    layerTitle: string;
    description: string;
    featureType: string;
    geometryType: string;
    parameter1: string;
    parameter2: string;
    parameter3: string;
    parameter4: string;
    iconUri: string;
    iconSize: number;
    drawingMode: string;
    fillColor: string;
    strokeColor: string;
    selectedStrokeColor: string;
    strokeWidth: number;
    isEnabled: boolean;
    clusterLevel: number;
    useClustering: boolean;
    opacity: number;
    nameLabel: string;
    includeOriginalProperties: boolean;
    defaultFeatureType: string;
    geometryFile: string;
    geometryKey: string;
}

export interface IPropertyType {
    label?: string;
    title?: string;
    description?: string;
    type?: string;
    section?: string;
    stringFormat?: string;
    visibleInCallOut?: boolean;
    canEdit?: boolean;
    filterType?: string;
    isSearchable?: boolean;
    minValue?: number;
    maxValue?: number;
    defaultValue?: number;
    count?: number;
    calculation?: string;
    subject?: string;
    target?: string;
    targetlayers?: string[];
    targetproperty?: string;
    options?: string[];
    activation?: string;
    targetid?: string;
}

export interface ILayerTemplate {
    layerDefinition: ILayerDefinition[];
    propertyTypes: IPropertyType[];
    properties: IProperty[];
    sensors?: IProperty[];
    projectId?: string;
    iconBase64?: string;
}

export interface IBagContourRequest {
    bounds: string;
    layer: any;
}

export interface IBagSearchRequest {
    query: string;
    nrItems: number;
}

/** A factory class to create new map layers based on input, e.g. from Excel */
export class MapLayerFactory {
    templateFiles: IProperty[];
    featuresNotFound: any;
    apiManager: Api.ApiManager;

    // constructor(private bag: LocalBag, private messageBus: MessageBus.MessageBusService) {
    constructor(private bag: BagDatabase.BagDatabase, private messageBus: MessageBus.MessageBusService, apiManager: Api.ApiManager) {
        if (bag != null) {
            bag.init();
        }
        var fileList: IProperty[] = [];
        fs.readdir('public/data/templates', function(err, files) {
            if (err) {
                console.log('Error while looking for templates');
            } else {
                files.forEach((f) => {
                    fileList[f.replace(/\.[^/.]+$/, '')] = ('public/data/templates/' + f); // Filter extension from key and store in dictionary
                });
            }
        });
        this.templateFiles = fileList;
        this.featuresNotFound = {};
        this.apiManager = apiManager;
    }

    public process(req: express.Request, res: express.Response) {
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.end('');
        console.log('Received project template. Processing...');
        this.featuresNotFound = {};
        var template: ILayerTemplate = req.body;
        var ld = template.layerDefinition[0];
        this.createMapLayer(template, (geojson) => {

            //if (!fs.existsSync("public/data/projects/DynamicExample")) fs.mkdirSync("public/data/projects/DynamicExample");
            //if (!fs.existsSync("public/data/projects/DynamicExample/" + ld.group)) fs.mkdirSync("public/data/projects/DynamicExample/" + ld.group);
            //fs.writeFileSync("public/data/projects/DynamicExample/" + ld.group + "/" + ld.layerTitle + ".json", JSON.stringify(geojson));

            if (!template.projectId || !ld.reference) {
                console.log('Error: No project or layer ID');
                return;
            }
            var layerId = template.projectId + ld.reference.toLowerCase();<commit_msg>Fix: MapLayerFactory templates were not loaded correctly from global install<commit_after>import fs = require('fs');
import http = require('http');
import express = require('express');
import request = require('request');
import pg = require('pg');
import proj4 = require('proj4');
import MessageBus = require('../bus/MessageBus');
import ConfigurationService = require('../configuration/ConfigurationService');
import Location = require('../database/Location');
import BagDatabase = require('../database/BagDatabase');
import LocalBag = require('../database/LocalBag');
import IBagOptions = require('../database/IBagOptions');
import IGeoJsonFeature = require('./IGeoJsonFeature');
import Api = require('../api/ApiManager');
import Utils = require('../helpers/Utils');
import async = require('async');
import winston = require('winston');
import path = require('path');
import IProperty = Api.IProperty;

export interface ILayerDefinition {
    projectTitle: string;
    reference: string;
    group: string;
    layerTitle: string;
    description: string;
    featureType: string;
    geometryType: string;
    parameter1: string;
    parameter2: string;
    parameter3: string;
    parameter4: string;
    iconUri: string;
    iconSize: number;
    drawingMode: string;
    fillColor: string;
    strokeColor: string;
    selectedStrokeColor: string;
    strokeWidth: number;
    isEnabled: boolean;
    clusterLevel: number;
    useClustering: boolean;
    opacity: number;
    nameLabel: string;
    includeOriginalProperties: boolean;
    defaultFeatureType: string;
    geometryFile: string;
    geometryKey: string;
}

export interface IPropertyType {
    label?: string;
    title?: string;
    description?: string;
    type?: string;
    section?: string;
    stringFormat?: string;
    visibleInCallOut?: boolean;
    canEdit?: boolean;
    filterType?: string;
    isSearchable?: boolean;
    minValue?: number;
    maxValue?: number;
    defaultValue?: number;
    count?: number;
    calculation?: string;
    subject?: string;
    target?: string;
    targetlayers?: string[];
    targetproperty?: string;
    options?: string[];
    activation?: string;
    targetid?: string;
}

export interface ILayerTemplate {
    layerDefinition: ILayerDefinition[];
    propertyTypes: IPropertyType[];
    properties: IProperty[];
    sensors?: IProperty[];
    projectId?: string;
    iconBase64?: string;
}

export interface IBagContourRequest {
    bounds: string;
    layer: any;
}

export interface IBagSearchRequest {
    query: string;
    nrItems: number;
}

/** A factory class to create new map layers based on input, e.g. from Excel */
export class MapLayerFactory {
    templateFiles: IProperty[];
    featuresNotFound: any;
    apiManager: Api.ApiManager;

    // constructor(private bag: LocalBag, private messageBus: MessageBus.MessageBusService) {
    constructor(private bag: BagDatabase.BagDatabase, private messageBus: MessageBus.MessageBusService, apiManager: Api.ApiManager, private workingDir: string = '') {
        if (bag != null) {
            bag.init();
        }
        var fileList: IProperty[] = [];
        var templateFolder: string = path.join(workingDir, 'public', 'data', 'templates');
        fs.readdir(templateFolder, function(err, files) {
            if (err) {
                console.log('Error while looking for templates in ' + templateFolder);
            } else {
                files.forEach((f) => {
                    fileList[f.replace(/\.[^/.]+$/, '')] = path.join(templateFolder, f); // Filter extension from key and store in dictionary
                });
                console.log(`Loaded ${files.length} templates from ${templateFolder}.` );
            }
        });
        this.templateFiles = fileList;
        this.featuresNotFound = {};
        this.apiManager = apiManager;
    }

    public process(req: express.Request, res: express.Response) {
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.end('');
        console.log('Received project template. Processing...');
        this.featuresNotFound = {};
        var template: ILayerTemplate = req.body;
        var ld = template.layerDefinition[0];
        this.createMapLayer(template, (geojson) => {

            //if (!fs.existsSync("public/data/projects/DynamicExample")) fs.mkdirSync("public/data/projects/DynamicExample");
            //if (!fs.existsSync("public/data/projects/DynamicExample/" + ld.group)) fs.mkdirSync("public/data/projects/DynamicExample/" + ld.group);
            //fs.writeFileSync("public/data/projects/DynamicExample/" + ld.group + "/" + ld.layerTitle + ".json", JSON.stringify(geojson));

            if (!template.projectId || !ld.reference) {
                console.log('Error: No project or layer ID');
                return;
            }
            var layerId = template.projectId + ld.reference.toLowerCase();
{'fraction_non_alphanumeric': 0.13137151865475566, 'fraction_numerical': 0.0025223331581713084, 'mean_word_length': 3.115916955017301, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5523728', 'n_tokens_mistral': 2562, 'n_tokens_neox': 2538, 'n_words': 649}