<commit_before>import { ICasesConfig } from '../models/cases-config';
import { Inject, Injectable, InjectionToken } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/debounce';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/observable/of';
import { Case } from '../models/case.model';
import { QueryParamsHelper } from './helpers/cases.service.query-params-helper';
import { UrlSerializer } from '@angular/router';
import { CasePreview, CaseState, CaseTimeState, ErrorHandlerService } from '@ansyn/core';
import { UUID } from "angular2-uuid";
import * as moment from 'moment';
import { StorageService, StoredEntity } from "@ansyn/core/services/storage/storage.service";

export const casesConfig: InjectionToken<ICasesConfig> = new InjectionToken('cases-config');

@Injectable()
export class CasesService {
	static defaultTime: CaseTimeState = {
		type: 'absolute',
		from: moment().subtract(1, 'y').toDate(),
		to: new Date()
	};

	queryParamsHelper: QueryParamsHelper = new QueryParamsHelper(this);
	baseUrl;
	paginationLimit = 15;
	queryParamsKeys;

	public contextValues = {
		imageryCountBefore: -1,
		imageryCountAfter: -1,
		defaultOverlay: '',
		time: undefined
	};

	get defaultCase() {
		return this.config.defaultCase;
	}

	constructor(protected storageService: StorageService,
				@Inject(casesConfig) public config: ICasesConfig,
				public urlSerializer: UrlSerializer,
				public errorHandlerService: ErrorHandlerService) {
		this.paginationLimit = this.config.paginationLimit;
		this.queryParamsKeys = this.config.casesQueryParamsKeys;
	}

	loadCases(casesOffset: number = 0): Observable<any> {
		return this.storageService.getPage<CasePreview>(this.config.schema, casesOffset, this.paginationLimit)
			.map(previews => previews.map(preview => this.parseCasePreview(preview)))
			.catch(err => this.errorHandlerService.httpErrorHandle(err));
	}

	parseCasePreview(casePreview: CasePreview): CasePreview {
		return {
			...casePreview,
			creationTime: new Date(casePreview.creationTime),
			lastModified: new Date(casePreview.lastModified)
		};
	}

	parseCase(caseValue: Case) {
		return {
			...caseValue,
			creationTime: new Date(caseValue.creationTime),
			lastModified: new Date(caseValue.lastModified),
			state: {
				...caseValue.state,
				time: Boolean(caseValue.state.time) ? {
					...caseValue.state.time,
					from: new Date(caseValue.state.time.from),
					to: new Date(caseValue.state.time.to),
				} : null
			}
		};
	}

	getPreview(caseValue: Case): CasePreview {
		return {
			id: caseValue.id,
			name: caseValue.name,
			creationTime: caseValue.creationTime,
			owner: caseValue.owner,
			lastModified: caseValue.lastModified,
			selectedContextId: caseValue.selectedContextId
		};
	}

	convertToStoredEntity(caseValue: Case): StoredEntity<CasePreview, CaseState> {
		return {
			preview: this.getPreview(caseValue),
			data: caseValue.state
		};
	}

	createCase(selectedCase: Case): Observable<Case> {
		const currentTime = new Date();
		const uuid = UUID.UUID();
		selectedCase.id = uuid;
		selectedCase.creationTime = currentTime;
		selectedCase.lastModified = currentTime;
		return this.storageService.create(this.config.schema, this.convertToStoredEntity(selectedCase))
			.map(_ => selectedCase)
			.catch(err => {
				return this.errorHandlerService.httpErrorHandle(err);
			});<commit_msg>cases: add the selectedContextId to the preview only if it's defined<commit_after>import { ICasesConfig } from '../models/cases-config';
import { Inject, Injectable, InjectionToken } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/debounce';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/observable/of';
import { Case } from '../models/case.model';
import { QueryParamsHelper } from './helpers/cases.service.query-params-helper';
import { UrlSerializer } from '@angular/router';
import { CasePreview, CaseState, CaseTimeState, ErrorHandlerService } from '@ansyn/core';
import { UUID } from "angular2-uuid";
import * as moment from 'moment';
import { StorageService, StoredEntity } from "@ansyn/core/services/storage/storage.service";

export const casesConfig: InjectionToken<ICasesConfig> = new InjectionToken('cases-config');

@Injectable()
export class CasesService {
	static defaultTime: CaseTimeState = {
		type: 'absolute',
		from: moment().subtract(1, 'y').toDate(),
		to: new Date()
	};

	queryParamsHelper: QueryParamsHelper = new QueryParamsHelper(this);
	baseUrl;
	paginationLimit = 15;
	queryParamsKeys;

	public contextValues = {
		imageryCountBefore: -1,
		imageryCountAfter: -1,
		defaultOverlay: '',
		time: undefined
	};

	get defaultCase() {
		return this.config.defaultCase;
	}

	constructor(protected storageService: StorageService,
				@Inject(casesConfig) public config: ICasesConfig,
				public urlSerializer: UrlSerializer,
				public errorHandlerService: ErrorHandlerService) {
		this.paginationLimit = this.config.paginationLimit;
		this.queryParamsKeys = this.config.casesQueryParamsKeys;
	}

	loadCases(casesOffset: number = 0): Observable<any> {
		return this.storageService.getPage<CasePreview>(this.config.schema, casesOffset, this.paginationLimit)
			.map(previews => previews.map(preview => this.parseCasePreview(preview)))
			.catch(err => this.errorHandlerService.httpErrorHandle(err));
	}

	parseCasePreview(casePreview: CasePreview): CasePreview {
		return {
			...casePreview,
			creationTime: new Date(casePreview.creationTime),
			lastModified: new Date(casePreview.lastModified)
		};
	}

	parseCase(caseValue: Case) {
		return {
			...caseValue,
			creationTime: new Date(caseValue.creationTime),
			lastModified: new Date(caseValue.lastModified),
			state: {
				...caseValue.state,
				time: Boolean(caseValue.state.time) ? {
					...caseValue.state.time,
					from: new Date(caseValue.state.time.from),
					to: new Date(caseValue.state.time.to),
				} : null
			}
		};
	}

	getPreview(caseValue: Case): CasePreview {
		const casePreview: CasePreview = {
			id: caseValue.id,
			name: caseValue.name,
			creationTime: caseValue.creationTime,
			owner: caseValue.owner,
			lastModified: caseValue.lastModified
		};

		if (caseValue.selectedContextId) {
			casePreview.selectedContextId = caseValue.selectedContextId;
		}

		return casePreview;
	}

	convertToStoredEntity(caseValue: Case): StoredEntity<CasePreview, CaseState> {
		return {
			preview: this.getPreview(caseValue),
			data: caseValue.state
		};
	}

	createCase(selectedCase: Case): Observable<Case> {
		const currentTime = new Date();
		const uuid = UUID.UUID();
		selectedCase.id = uuid;
		selectedCase.creationTime = currentTime;
		selectedCase.lastModified = currentTime;
		return this.storageService.create(this.config.schema, this.convertToStoredEntity(selectedCase))
			.map(_ => selectedCase)
			.catch(err => {
				return this.errorHandlerService.httpErrorHandle(err);
			});
{'fraction_non_alphanumeric': 0.12412512498214541, 'fraction_numerical': 0.001999714326524782, 'mean_word_length': 4.839866555462886, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, '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': '2765812', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2132, 'n_words': 434}