import {SafeResourceUrl} from "@angular/platform-browser"; import {properties} from "../../../../environments/environment"; export const ChartHelper = { prefix: "((__", suffix: "__))" }; export type StakeholderType = 'funder' | 'ri' | 'project' | 'organization'; export type IndicatorType = 'number' | 'chart'; export type IndicatorSize = 'small' | 'medium' | 'large'; export type IndicatorPathType = 'table' | 'bar' | 'column' | 'pie' | 'line' | 'other'; export type SourceType = 'statistics' | 'search' | 'metrics' | 'stats-tool' | 'old' | 'image'; export type Visibility = 'PUBLIC' | 'PRIVATE' | 'RESTRICTED'; export class Stakeholder { _id: string; type: StakeholderType; name: string; index_id; index_name: string; index_shortName: string; alias: string; defaultId: string; visibility: Visibility; creationDate: Date = null; updateDate: Date; logoUrl: string; isUpload: boolean = false; description: string; topics: any[]; constructor(_id: string, type: StakeholderType, index_id, index_name: string, index_shortName: string, alias: string, visibility: Visibility, logoUrl: string, defaultId: string = null, description: string = null) { this._id = _id; this.type = type; this.index_id = index_id; this.index_name = index_name; this.index_shortName = index_shortName; this.defaultId = defaultId; this.alias = alias; this.visibility = visibility; this.logoUrl = logoUrl; this.description = description; this.topics = []; } } export class StakeholderInfo extends Stakeholder { isManager: boolean = false; isMember: boolean = false; } export class Topic { _id: string; name: string; alias: string; description: string; visibility: Visibility; creationDate: Date = null; updateDate: Date; defaultId: string; categories: Category[]; icon: string; constructor(name: string, description: string, alias: string, visibility:Visibility, defaultId: string = null, icon: string = null) { this._id = null; this.name = name; this.description = description; this.alias = alias; this.visibility = visibility; this.defaultId = defaultId; this.categories = []; this.icon = icon; } } export class Category { _id: string; name: string; alias: string; description: string; creationDate: Date = null; updateDate: Date; visibility: Visibility; defaultId: string; subCategories: SubCategory[]; constructor(name: string, description: string, alias: string, visibility: Visibility, defaultId: string = null) { this._id = null; this.name = name; this.description = description; this.alias = alias; this.visibility = visibility; this.defaultId = defaultId; this.subCategories = []; } } export class SubCategory { _id: string; name: string; alias: string; description: string; creationDate: Date = null; updateDate: Date; visibility: Visibility; defaultId: string; charts: Section[]; numbers: Section[]; constructor(name: string, description: string, alias: string, visibility: Visibility, defaultId: string = null) { this._id = null; this.name = name; this.description = description; this.alias = alias; this.visibility = visibility; this.defaultId = defaultId; this.charts = []; this.numbers = []; } } export class Section { _id: string; title: string; defaultId: string; creationDate: Date = null; updateDate: Date; stakeholderAlias: string; type: IndicatorType; indicators: Indicator[]; constructor(type: IndicatorType, title: string = null, defaultId: string = null, stakeholderAlias: string = null) { this._id = null; this.title = title; this.type = type; this.defaultId = defaultId; this.stakeholderAlias = stakeholderAlias; this.indicators = []; } } export class Indicator { _id: string; name: string; description: string; additionalDescription: string; creationDate: Date = null; updateDate: Date; type: IndicatorType; width: IndicatorSize; height: IndicatorSize; tags: string[]; visibility: Visibility; defaultId: string; indicatorPaths: IndicatorPath[]; constructor(name: string, description: string, additionalDescription:string, type: IndicatorType, width: IndicatorSize,height: IndicatorSize, visibility: Visibility, indicatorPaths: IndicatorPath[], defaultId: string = null) { this._id = null; this.name = name; this.description = description; this.additionalDescription = additionalDescription; this.type = type; this.width = width; this.height = height; this.visibility = visibility; this.defaultId = defaultId; this.indicatorPaths = indicatorPaths; } } export class IndicatorPath { type: IndicatorPathType; source: SourceType; url: string; safeResourceUrl: SafeResourceUrl; // initialize on front end jsonPath: string[]; chartObject: string; parameters: any; filters: any; filtersApplied: number = 0; constructor(type: IndicatorPathType, source: SourceType, url: string, chartObject: string, jsonPath: string[]) { this.type = type; this.url = url; this.source = source; this.jsonPath = jsonPath; this.chartObject = chartObject; this.parameters = {}; this.filters = {}; this.filtersApplied = 0; } static createParameters(funderName: string = null, title: string = null, chartType: string = null): any { return { index_name: funderName, title: title, type: chartType }; } } export type FilterType = "fundingL0"|"start_year" | "end_year" | "co-funded"; export class IndicatorFilterUtils{ static getFilter(field: string, filterType:FilterType) { if(["publication", "software", "dataset", "other", "result"].indexOf(field)!=-1){ return this.getResultFilter(field,filterType); }else if (field == "project"){ return this.getProjectFilter(filterType); } else if (field == "country"){ return this.getCountryFilter(filterType); }else if (field == "organization"){ return this.getOrganizationFilter(filterType); } } static getResultFilter(dbType: string = null, filterType:FilterType) { if (filterType == "fundingL0") { if(properties.useOldStatisticsSchema) { return '{"groupFilters":[{"field":"' + dbType + '.project.funding level 0","type":"=","values":["' + ChartHelper.prefix + 'fundingL0' + ChartHelper.suffix + '"]}],"op":"AND"}'; }else{//new statistcs schema return '{"groupFilters":[{"field":"' + dbType + '.project funding level 0","type":"=","values":["' + ChartHelper.prefix + 'fundingL0' + ChartHelper.suffix + '"]}],"op":"AND"}'; } } else if (filterType == "start_year") { return '{"groupFilters":[{"field":"' + dbType + '.year","type":">=","values":["' + ChartHelper.prefix + 'start_year' + ChartHelper.suffix + '"]}],"op":"AND"}'; } else if (filterType == "end_year") { return '{"groupFilters":[{"field":"' + dbType + '.year","type":"<=","values":["' + ChartHelper.prefix + 'end_year' + ChartHelper.suffix + '"]}],"op":"AND"}'; }else if (filterType == "co-funded") { return '{"groupFilters":[{"field":"' + dbType + '.No of funders","type":">","values":["1"]}],"op":"AND"}'; } } static getProjectFilter( filterType:FilterType) { if (filterType == "fundingL0") { return '{"groupFilters":[{"field":"project.funding level 0","type":"=","values":["' + ChartHelper.prefix + 'fundingL0' + ChartHelper.suffix + '"]}],"op":"AND"}'; } else if (filterType == "start_year") { return '{"groupFilters":[{"field":"project.start year","type":">=","values":["' + ChartHelper.prefix + 'start_year' + ChartHelper.suffix + '"]}],"op":"AND"}'; } else if (filterType == "end_year") { return '{"groupFilters":[{"field":"project.start year","type":"<=","values":["' + ChartHelper.prefix + 'end_year' + ChartHelper.suffix + '"]}],"op":"AND"}'; } } static getOrganizationFilter( filterType:FilterType) { if (filterType == "fundingL0") { return '{"groupFilters":[{"field":"organization.project.funding level 0","type":"=","values":["' + ChartHelper.prefix + 'fundingL0' + ChartHelper.suffix + '"]}],"op":"AND"}'; } else if (filterType == "start_year") { return '{"groupFilters":[{"field":"organization.project.start year","type":">=","values":["' + ChartHelper.prefix + 'start_year' + ChartHelper.suffix + '"]}],"op":"AND"}'; } else if (filterType == "end_year") { return '{"groupFilters":[{"field":"organization.project.start year","type":"<=","values":["' + ChartHelper.prefix + 'end_year' + ChartHelper.suffix + '"]}],"op":"AND"}'; } } static getCountryFilter( filterType:FilterType) { if (filterType == "fundingL0") { return '{"groupFilters":[{"field":"country.organization.project.funding level 0","type":"=","values":["' + ChartHelper.prefix + 'fundingL0' + ChartHelper.suffix + '"]}],"op":"AND"}'; } else if (filterType == "start_year") { return '{"groupFilters":[{"field":"country.organization.project.start year","type":">=","values":["' + ChartHelper.prefix + 'start_year' + ChartHelper.suffix + '"]}],"op":"AND"}'; } else if (filterType == "end_year") { return '{"groupFilters":[{"field":"country.organization.project.start year","type":"<=","values":["' + ChartHelper.prefix + 'end_year' + ChartHelper.suffix + '"]}],"op":"AND"}'; } } static filterIndexOf(filterToAdd, currentFilters):any{ for(let fi =0; fi< currentFilters.length; fi++){ for(let gfi =0; gfi< currentFilters[fi]["groupFilters"].length; gfi++ ){ if(currentFilters[fi]["groupFilters"][gfi].field == filterToAdd['groupFilters'][0]['field'] && currentFilters[fi]["groupFilters"][gfi].type == filterToAdd['groupFilters'][0]['type']){ return {"filter":fi, "groupFilter":gfi}; } } } return null; } }