/* * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; namespace Tizen.Pims.Calendar { /// /// A filter includes the conditions for the search. /// public class CalendarFilter:IDisposable { internal IntPtr _filterHandle; /// /// Creates a filter with a condition for a string type. /// /// The view URI of a filter /// The property ID to add a condition /// The match flag /// The match value /// Thrown when an invoked method is not supported /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory public CalendarFilter(string viewUri, uint propertyId, StringMatchType matchType, string matchValue) { int error = 0; error = Interop.Calendar.Filter.Create(viewUri, out _filterHandle); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error); throw CalendarErrorFactory.GetException(error); } error = Interop.Calendar.Filter.AddString(_filterHandle, propertyId, matchType, matchValue); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Creates a filter with a condition for an integer type. /// /// The view URI of a filter /// The property ID to add a condition /// The match flag /// The match value /// Thrown when an invoked method is not supported /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory public CalendarFilter(string viewUri, uint propertyId, IntegerMatchType matchType, int matchValue) { int error = 0; error = Interop.Calendar.Filter.Create(viewUri, out _filterHandle); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error); throw CalendarErrorFactory.GetException(error); } error = Interop.Calendar.Filter.AddInteger(_filterHandle, propertyId, matchType, matchValue); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Creates a filter with a condition for long type. /// /// The view URI of a filter /// The property ID to add a condition /// The match flag /// The match value /// Thrown when an invoked method is not supported /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory public CalendarFilter(string viewUri, uint propertyId, IntegerMatchType matchType, long matchValue) { int error = 0; error = Interop.Calendar.Filter.Create(viewUri, out _filterHandle); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error); throw CalendarErrorFactory.GetException(error); } error = Interop.Calendar.Filter.AddLong(_filterHandle, propertyId, matchType, matchValue); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Creates a filter with a condition for double type. /// /// The view URI of a filter /// The property ID to add a condition /// The match flag /// The match value /// Thrown when an invoked method is not supported /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory public CalendarFilter(string viewUri, uint propertyId, IntegerMatchType matchType, double matchValue) { int error = 0; error = Interop.Calendar.Filter.Create(viewUri, out _filterHandle); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error); throw CalendarErrorFactory.GetException(error); } error = Interop.Calendar.Filter.AddDouble(_filterHandle, propertyId, matchType, matchValue); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Creates a filter with a condition for CalendarTime type. /// /// The view URI of a filter /// The property ID to add a condition /// The match flag /// The match value /// Thrown when an invoked method is not supported /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory public CalendarFilter(string viewUri, uint propertyId, IntegerMatchType matchType, CalendarTime matchValue) { int error = 0; error = Interop.Calendar.Filter.Create(viewUri, out _filterHandle); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error); throw CalendarErrorFactory.GetException(error); } Interop.Calendar.Record.DateTime time = CalendarRecord.ConvertCalendarTimeToStruct(matchValue); error = Interop.Calendar.Filter.AddCalendarTime(_filterHandle, propertyId, matchType, time); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } ~CalendarFilter() { Dispose(false); } /// /// Enumeration for the filter match type of a string. /// public enum StringMatchType { /// /// Full string, case-sensitive /// Exactly, /// /// Full string, case-insensitive /// Fullstring, /// /// Sub string, case-insensitive /// Contains, /// /// Start with, case-insensitive /// Startswith, /// /// End with, case-insensitive /// Endswith, /// /// IS NOT NUL /// Exists, } /// /// Enumeration for the filter match type of an integer. /// public enum IntegerMatchType { /// /// '=' /// Equal, /// /// '>' /// GreaterThan, /// /// '>=' /// GreaterThanOrEqual, /// /// < /// LessThan, /// /// <= /// LessThanOrEqual, /// /// <>, this flag can yield poor performance /// NotEqual, /// /// IS NULL /// None, } /// /// Enumeration for a filter operator. /// public enum LogicalOperator { /// /// AND /// And, /// /// OR /// Or, } #region IDisposable Support private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!disposedValue) { Log.Debug(Globals.LogTag, "Dispose :" + disposing); int error = Interop.Calendar.Filter.Destroy(_filterHandle); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "Destroy Failed with error " + error); throw CalendarErrorFactory.GetException(error); } disposedValue = true; } } /// /// Releases all resources used by the CalendarFilter. /// It should be called after finished using of the object. /// public void Dispose() { Dispose(true); } #endregion /// /// Adds a condition for the string type. /// /// The operator type /// The property ID to add a condition /// The match flag /// The match valu /// Thrown when an invoked method is not supported /// Thrown when one of the arguments provided to a method is not valid public void AddCondition(LogicalOperator logicalOperator, uint propertyId, StringMatchType matchType, string matchValue) { int error = Interop.Calendar.Filter.AddOperator(_filterHandle, logicalOperator); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw CalendarErrorFactory.GetException(error); } error = Interop.Calendar.Filter.AddString(_filterHandle, propertyId, matchType, matchValue); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Adds a condition for the integer type. /// /// The operator type /// The property ID to add a condition /// The match flag /// The match valu /// Thrown when an invoked method is not supported /// Thrown when one of the arguments provided to a method is not valid public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, int matchValue) { int error = Interop.Calendar.Filter.AddOperator(_filterHandle, logicalOperator); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw CalendarErrorFactory.GetException(error); } error = Interop.Calendar.Filter.AddInteger(_filterHandle, propertyId, matchType, matchValue); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Adds a condition for the long type. /// /// The operator type /// The property ID to add a condition /// The match flag /// The match valu /// Thrown when an invoked method is not supported /// Thrown when one of the arguments provided to a method is not valid public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, long matchValue) { int error = Interop.Calendar.Filter.AddOperator(_filterHandle, logicalOperator); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw CalendarErrorFactory.GetException(error); } error = Interop.Calendar.Filter.AddLong(_filterHandle, propertyId, matchType, matchValue); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Adds a condition for the double type. /// /// The operator type /// The property ID to add a condition /// The match flag /// The match valu /// Thrown when an invoked method is not supported /// Thrown when one of the arguments provided to a method is not valid public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, double matchValue) { int error = Interop.Calendar.Filter.AddOperator(_filterHandle, logicalOperator); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw CalendarErrorFactory.GetException(error); } error = Interop.Calendar.Filter.AddDouble(_filterHandle, propertyId, matchType, matchValue); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Adds a condition for the CalendarTime type. /// /// The operator type /// The property ID to add a condition /// The match flag /// The match valu /// Thrown when an invoked method is not supported /// Thrown when one of the arguments provided to a method is not valid public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, CalendarTime matchValue) { int error = Interop.Calendar.Filter.AddOperator(_filterHandle, logicalOperator); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw CalendarErrorFactory.GetException(error); } Interop.Calendar.Record.DateTime time = CalendarRecord.ConvertCalendarTimeToStruct(matchValue); error = Interop.Calendar.Filter.AddCalendarTime(_filterHandle, propertyId, matchType, time); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Adds a child filter to a parent filter. /// /// The operator type /// The child filter /// Thrown when an invoked method is not supported /// Thrown when one of the arguments provided to a method is not valid public void AddFilter(LogicalOperator logicalOperator, CalendarFilter filter) { int error = Interop.Calendar.Filter.AddOperator(_filterHandle, logicalOperator); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw CalendarErrorFactory.GetException(error); } error = Interop.Calendar.Filter.AddFilter(_filterHandle, filter._filterHandle); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "AddFilter Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } } }