Hey guys,
I've been writing in Blogger for almost 10 years this is a time to move on.
I'm happy to announce my new blog at Medium:
Ohad.
Professional Integration and Web Technologies blog
public string BuildFilterQuery(string Xmlquery) { var query = string.Empty; var doc = new XmlDocument(); var values = string.Empty; doc.LoadXml(Xmlquery); try { string fieldID; string conditionOperator; string conditionValues = string.Empty; XmlNodeList filters = doc.SelectNodes("//Root//Filter"); for (int i = 0; (i <= (filters.Count - 1)); i++) { XmlNodeList root = filters[i].SelectNodes(".//FilterItem"); query = (query + Convert.ToString("(")); foreach (XmlNode item in root) { fieldID = item.SelectSingleNode("FieldID").InnerText; conditionOperator = item.SelectSingleNode("Operator").InnerText.ToLower(); conditionValue = item.SelectSingleNode("Operator").InnerText; switch (conditionOperator) { case "contains": query = string.Format("{0} [{1}] {2} \'%{3}%\' AND", query, DataManipulate.ConvertFromSafe(fieldID), dOperators[conditionOperator], conditionValue); break; case "after": query = string.Format("{0} Convert([{1}], \'System.DateTime\') {2} \"#{3}#\" AND", query, DataManipulate.ConvertFromSafe(fieldID), dOperators[conditionOperator], conditionValue); break; case "greater than": query = string.Format("{0} Convert([{1}], \'System.Int64\') {2} {3} AND", query, DataManipulate.ConvertFromSafe(fieldID), dOperators[conditionOperator], conditionValue); break; default: query = string.Format("{0} [{1}] {2} \'{3}\' AND", query, DataManipulate.ConvertFromSafe(fieldID), dOperators[conditionOperator], conditionValue); break; } } if (query.EndsWith(" AND")) { query = (query.Substring(0, query.LastIndexOf(" AND")) + ")"); } query = (query + " OR "); } } catch (Exception ex) { throw ex; } return query; }
[XmlRoot(ElementName = "Filter")] public class Filter { [XmlElement(ElementName = "FilterName")] public string FilterName { get; set; } [XmlElement(ElementName = "FilterItem")] public List<FilterItem> FilterItem { get; set; } }
public interface IQueryStringType {string ConcatQueryString(QueryStringTypeModel queryStringTypeModel); }
public class QueryStringTypeModel { public string FilterName { get; set; } public string Operator { get; set; } public List<string> ValueList { get; set; } public Dictionary<string, string> TranslateOperators { get; set; } public QueryStringTypeSettingsModel QueryStringTypeSettingsModel { get; set; } }
public class ContainsQueryStringType : IQueryStringType { public string ConcatQueryString(QueryStringTypeModel queryStringTypeModel) { var convertedFromSafe = $"[{DataManipulate.ConvertFromSafe(queryStringTypeModel.FilterName)}]"; var concatOperator = $"{queryStringTypeModel.TranslateOperators[queryStringTypeModel.Operator]}"; var value = $"'%{queryStringTypeModel.ValueList[0]}%'"; var response = $"{convertedFromSafe} {concatOperator} {value}"; return response; } }
public class QueryStringTypeSettingsModelRepository { private readonly Dictionary<string, QueryStringTypeSettingsModel> _queryStringTypeSettingsModelDic = new Dictionary<string, QueryStringTypeSettingsModel>(); public QueryStringTypeSettingsModelRepository() { var containsSettingsModel = new QueryStringTypeSettingsModel() { QueryStringType = new ContainsQueryStringType(), };
this._queryStringTypeSettingsModelDic.Add("Contains", containsSettingsModel); this._queryStringTypeSettingsModelDic.Add("Not contains", containsSettingsModel); } public QueryStringTypeSettingsModel GetModel(string @operator) { if (!this._queryStringTypeSettingsModelDic.ContainsKey(@operator)) return null; var queryStringTypeSettingsModel = this._queryStringTypeSettingsModelDic[@operator]; return queryStringTypeSettingsModel; } }
public class QueryStringCalculator : IQueryStringCalculator { public string GetQueryString(QueryStringTypeModel queryStringTypeModel) { var response = string.Empty; if (queryStringTypeModel.QueryStringTypeSettingsModel == null || string.IsNullOrEmpty(queryStringTypeModel.FilterName) || string.IsNullOrEmpty(queryStringTypeModel.Operator)) return response; var queryStringType = queryStringTypeModel.QueryStringTypeSettingsModel.QueryStringType; response = queryStringType.ConcatQueryString(queryStringTypeModel); return response; } }
public class FilterItemToQueryStringConvertor : IFilterItemToQueryStringConvertor { private readonly IQueryStringCalculator _queryStringCalculator; private readonly IQueryStringTypeModelGenerator _queryStringTypeModelGenerator; public FilterItemToQueryStringConvertor(IQueryStringCalculator queryStringCalculator, IQueryStringTypeModelGenerator queryStringTypeModelGenerator) { _queryStringCalculator = queryStringCalculator; _queryStringTypeModelGenerator = queryStringTypeModelGenerator; } public string GetQueryString(List<FilterItem> filterItems, Dictionary<string,string> dOperators) { var response = string.Empty; if (filterItems.Count == 0) return response; var queryStringCollection = new List<string>(); foreach (var filterItem in filterItems) { var queryStringTypeGeneratorModel = new QueryStringTypeGeneratorModel { FilterItem = filterItem, TranslateOperators = dOperators }; var queryStringTypeModel = _queryStringTypeModelGenerator.GetQueryStringTypeModel(queryStringTypeGeneratorModel); var queryString = _queryStringCalculator.GetQueryString(queryStringTypeModel); queryStringCollection.Add(queryString); } response = queryStringCollection.Aggregate((current, next) => current + " AND " + next); return response; } }
public class FilterCollectionToQueryStringConvertor : IFilterCollectionToQueryStringConvertor
{
private readonly IFilterItemToQueryStringConvertor _filterItemToQueryStringConvertor;
public FilterCollectionToQueryStringConvertor(IFilterItemToQueryStringConvertor filterItemToQueryStringConvertor)
{
this._filterItemToQueryStringConvertor = filterItemToQueryStringConvertor;
}
public string GetQueryString(List<Filter> filters,
Dictionary<string, string> dOperators)
{
var response = string.Empty;
if (filters.Count == 0)
return response;
var queryStringCollection = new List<string>();
foreach (var filter in filters)
{
var queryString = _filterItemToQueryStringConvertor.GetQueryString(filter.FilterItem, dOperators);
queryStringCollection.Add(queryString);
}
response = queryStringCollection.Aggregate((current, next) => current + " OR " + next);
return response;
}
}
this.observableGeneratorService.getBooleanObservable().subscribe((shouldPresentNumbers) => { if(shouldPresentNumbers) { this.observableGeneratorService.getNumberObservable().subscribe((val) => { this.uglyNumbers.push(val); }) } });
this.goodObservable$ = this.observableGeneratorService.getBooleanObservable() .concatMap((shouldPresentNumbers) => { let numbersObservable = shouldPresentNumbers ? this.observableGeneratorService.getNumberObservable() : Observable.empty(); return numbersObservable; }) // concatMap would map "true" to each emitted value (1,2,3,4,5) and creates an observable. // Finally it returns: Observable of 1,2,3,4,5 // this could also be done by using ".map(...).combineAll()" .toArray(); // turn into an array of observable
<li *ngFor="let good of goodObservable$ | async"> {{good}} </li>
<div *ngIf="authService.isLoggedIn"> <h2>Logged In</h2> </div> <div *ngIf="!authService.isLoggedIn"> <h2>Not Logged</h2> </div>
<div *allowed="true"> <h2>Logged In</h2> </div> <div *allowed="false"> <h2>Not Logged</h2> </div>
private _allowed: boolean; private ngIfDirective: NgIf; private isLoggedInSubs: Subscription; @Input() public set allowed(val: boolean) { this._allowed = val; this.setNgIf(); }; constructor(private templateRef: TemplateRef<any>, private viewContainer: ViewContainerRef, private authService: AuthService) { if(!this.ngIfDirective) { this.ngIfDirective = new NgIf(this.viewContainer,this.templateRef); } this.isLoggedInSubs = this.authService.isLoggedInChanged.subscribe((value) => { this.setNgIf(); }); } setNgIf() { this.ngIfDirective.ngIf = this.authService.authModel.isLoggedIn == this._allowed; }
public isLoggedInChanged: Subject<string> = new Subject<string>(); constructor() { this.authModel = new AuthModel(); } set isLoggedIn(val: boolean) { this.authModel.isLoggedIn = val; this.isLoggedInChanged.next(val); } get isLoggedIn(): boolean { return this.authModel.isLoggedIn; } public switchLoggedIn() { this.isLoggedIn = !this.isLoggedIn; }
@Injectable() export class GlobalNotifications { private nameToSubject: Map<string, Subject> = new Map<string, Subject>();
getSubject(name: any) : Subject { let subjectObj : Subject = this.nameToSubject.get(name); if(!subjectObj) { subjectObj = new Subject(); this.nameToSubject.set(name,subjectObj); } return subjectObj; }
export class SecondComponent implements OnDestroy { private dataReceived : Array<string> = []; private subscriberFirst : Subscriber; constructor(private _globalNot:GlobalNotifications) { this.subscriberFirst = this._globalNot.getSubject('firstToSubs').subscribe((val) => this.dataReceived.push(val)); }
ngOnDestroy() {
this.subscriberFirst.unsubscribe();
}
export class FirstComponent { constructor(private _globalNot:GlobalNotifications) { } onClick() { let subjectObj : Subject = this._globalNot.getSubject('firstToSubs'); subjectObj.next("Sent from First Component"); } }
@Component({ selector: 'first-component', template: '<h1>First Component (Sender)</h1><button (click)="onClick()">Send to components</button>' }) export class FirstComponent { @Output('messageSent') messageSent = new EventEmitter<string>(); onClick() { this.messageSent.emit("Sent from First Component"); } }
@Component({ selector: 'my-app', template: ` <first-component (messageSent)="onMessageSentFromFirst($event)"></first-component> <br/> <second-component [dataReceived]="messages" [isChanged]="isChanged"></second-component> <br/> `, }) export class App { public messages:any = []; private isChanged:boolean; public onMessageSentFromFirst(event : any) { this.messages.push(event); this.isChanged = !this.isChanged; } }
@Component({ selector: 'second-component', template: '<h1>Second Component (Receiver)</h1><div *ngFor="let data of dataReceived">{{data}}</div>' }) export class SecondComponent { @Input() private dataReceived : Array<string>; private dataReceivedLength = 0; @Input() public isChanged:boolean; ngOnChanges(data:any) { if(data.isChanged) { this.onMessageReceived(this.dataReceived[this.dataReceived.length-1]); } } ngDoCheck() { if(this.dataReceived.length != this.dataReceivedLength) { this.dataReceivedLength = this.dataReceived.length; this.onMessageReceived(this.dataReceived[this.dataReceived.length-1]); } } public onMessageReceived(message : string) { // Perform actions } }
interface IReceiver { receiveMessage: (message:string) => string };
@Injectable() export class Communicator { public receiver : IReceiver; }
export class SecondComponent implements IReceiver { private message: string; constructor(private _communicator:Communicator) { _communicator.receiver = this; } public receiveMessage(message:string) : string { this.message = message; return message + " Received"; } }
export class FirstComponent implements OnInit { message: string; status: string; constructor(private _communicator:Communicator) { } onClick() { this.status = this._communicator.receiver.receiveMessage(this.message); } }
constructor(private valueAccessor: DefaultValueAccessor, private model: NgModel)
valueAccessor.registerOnChange = (fn: (val: any) => void) => { valueAccessor.onChange = (val) => { }
let regexp = new RegExp('\\d+(?:[\.]\\d{0,})?$'); let isNumber : boolean = regexp.test(val); let isEmpty : boolean = val == '';
if(!isNumber && !isEmpty) { model.control.setValue(model.value); return; } return fn(val);
<input type="text" [(ngModel)]="val" numbersOnlyInput>
export const CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR: any = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => CustomInputComponent), multi: true };
@Component({ selector: 'custom-input', template: `<div class="form-group"> <label><ng-content></ng-content> <input (input)="handleInput($event.target)" (blur)="onBlur()" > </label> </div>`, providers: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR] })
//From ControlValueAccessor interface registerOnChange(fn: any) { this.onChangeCallback = fn; }
handleInput(target: any): void { const val = target.value; let regexp = new RegExp('\\d+(?:[\.]\\d{0,})?$'); let isNumber = regexp.test(val); let isEmpty = val == ''; if(!isNumber && !isEmpty) { let model = this._inj.get(NgControl); // get NgModel model.control.setValue(model.value); target.value = model.value; return; } this.onChangeCallback(val); }
Hey guys, I've been writing in Blogger for almost 10 years this is a time to move on. I'm happy to announce my new blog at Med...