merge wrt-plugins-tizen_0.2.0-4
[profile/ivi/wrt-plugins-tizen.git] / src / platform / API / Messaging / MessageFilterValidator.cpp
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. 
15 */
16 /*
17  * MessageFilterValidator.cpp
18  *
19  *  Created on: 2011. 10. 31.
20  *      Author: sangtai
21  */
22
23 #include "MessageFilterValidator.h"
24 #include "MessageFilterValidatorFactory.h"
25
26 #include <dpl/log/log.h>
27 #include "log.h"
28
29 using namespace WrtDeviceApis::Commons;
30 using namespace WrtDeviceApis::CommonsJavaScript;
31 using namespace TizenApis::Api::Tizen;
32
33 namespace TizenApis {
34         namespace Platform {
35                 namespace Messaging {
36
37                 MessageFilterValidator::MessageFilterValidator(PropertyStructArray properties)
38                                 :FilterValidator(properties){
39                         m_isTypeSetted = false;
40
41                         initMatchFlagVectors();
42                         initAttributeAndMatchFlagsMap();
43                 }
44
45                 MessageFilterValidator::~MessageFilterValidator(){
46                 }
47
48                 void MessageFilterValidator::initMatchFlagVectors(){
49                         m_typeMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_EXACTLY);
50
51                         m_idMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_EXACTLY);
52
53                         m_folderIdMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_EXACTLY);
54
55                         m_timestampMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_EXACTLY);
56
57                         m_fromMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_EXACTLY);
58                         m_fromMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_CONTAINS);
59                         m_fromMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_STARTSWITH);
60                         m_fromMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_ENDSWITH);
61
62                         m_toMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_EXACTLY);
63                         m_toMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_CONTAINS);
64                         m_toMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_STARTSWITH);
65                         m_toMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_ENDSWITH);
66
67                         m_ccMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_EXACTLY);
68                         m_ccMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_CONTAINS);
69                         m_ccMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_STARTSWITH);
70                         m_ccMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_ENDSWITH);
71
72                         m_bccMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_EXACTLY);
73                         m_bccMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_CONTAINS);
74                         m_bccMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_STARTSWITH);
75                         m_bccMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_ENDSWITH);
76
77                         m_bodyMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_CONTAINS);
78
79                         m_isReadMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_EXACTLY);
80
81                         m_priorityMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_EXACTLY);
82
83                         m_attachmentMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_EXACTLY);
84                         
85                         m_subjectMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_EXACTLY);
86                         m_subjectMatchFlagVec.push_back(MessageFilterValidatorFactory::MATCH_CONTAINS);
87                 }
88
89                 void MessageFilterValidator::initAttributeAndMatchFlagsMap(){
90                         m_attributeAndMatchFlagsMap[MessageFilterValidatorFactory::ATTRIBUTE_TYPE]      = m_typeMatchFlagVec;
91                         m_attributeAndMatchFlagsMap[MessageFilterValidatorFactory::ATTRIBUTE_ID]        = m_idMatchFlagVec;
92                         m_attributeAndMatchFlagsMap[MessageFilterValidatorFactory::ATTRIBUTE_FOLDER]    = m_folderIdMatchFlagVec;
93                         m_attributeAndMatchFlagsMap[MessageFilterValidatorFactory::ATTRIBUTE_TIMESTAMP] = m_timestampMatchFlagVec;
94                         m_attributeAndMatchFlagsMap[MessageFilterValidatorFactory::ATTRIBUTE_FROM]      = m_fromMatchFlagVec;
95                         m_attributeAndMatchFlagsMap[MessageFilterValidatorFactory::ATTRIBUTE_CC]        = m_ccMatchFlagVec;
96                         m_attributeAndMatchFlagsMap[MessageFilterValidatorFactory::ATTRIBUTE_BCC]       = m_bccMatchFlagVec;
97                         m_attributeAndMatchFlagsMap[MessageFilterValidatorFactory::ATTRIBUTE_BODY]      = m_bodyMatchFlagVec;
98                         m_attributeAndMatchFlagsMap[MessageFilterValidatorFactory::ATTRIBUTE_IS_READ]   = m_isReadMatchFlagVec;
99                         m_attributeAndMatchFlagsMap[MessageFilterValidatorFactory::ATTRIBUTE_PRIORITY]  = m_priorityMatchFlagVec;
100                         m_attributeAndMatchFlagsMap[MessageFilterValidatorFactory::ATTRIBUTE_SUBJECT]   = m_subjectMatchFlagVec;
101                         m_attributeAndMatchFlagsMap[MessageFilterValidatorFactory::ATTRIBUTE_ATTACHMENT] = m_attachmentMatchFlagVec;
102                         m_attributeAndMatchFlagsMap[MessageFilterValidatorFactory::ATTRIBUTE_TO]        = m_toMatchFlagVec;
103                 }
104
105                 bool MessageFilterValidator::validateAttributeRange(std::string& attrName, Api::Tizen::AnyPtr& initialValue, Api::Tizen::AnyPtr& endValue, int depth){
106                         if(initialValue == NULL || endValue == NULL)
107                                 return false;
108
109                         bool retVal = false;
110
111                         if(attrName.compare(MessageFilterValidatorFactory::ATTRIBUTE_TIMESTAMP)!=0){
112                                 MsgLogError(">>> [ERROR] Not Supported attribute :[" << attrName << "]");
113                                 return false;
114                         }
115
116                         retVal = FilterValidator::validateAttributeRange(attrName, initialValue, endValue, depth);
117                         return retVal;
118                 }
119
120
121                 bool MessageFilterValidator::vectorContains(std::vector<std::string>& vec, Api::Tizen::MatchFlag& value){
122                         std::string matchStr;
123                         switch(value)
124                         {
125                         case Api::Tizen::MATCH_EXACTLY:
126                                 matchStr = "EXACTLY";
127                                 break;
128                         case Api::Tizen::MATCH_FULLSTRING:
129                                 matchStr = "FULLSTRING";
130                                 break;
131                         case Api::Tizen::MATCH_CONTAINS:
132                                 matchStr = "CONTAINS";
133                                 break;
134                         case Api::Tizen::MATCH_STARTSWITH:
135                                 matchStr = "STARTSWITH";
136                                 break;
137                         case Api::Tizen::MATCH_ENDSWITH:
138                                 matchStr = "ENDSWITH";
139                                 break;
140                         case Api::Tizen::MATCH_EXISTS:
141                                 matchStr = "EXISTS";
142                                 break;
143                         default:
144                                 matchStr = "";
145                                 break;
146                         }
147
148                         std::vector<std::string>::iterator it;
149                         for(it=vec.begin(); it<vec.end(); it++){
150                                 if((*it).compare(matchStr) == 0){
151                                         return true;
152                                 }
153                         }
154
155                         return false;
156                 }
157
158                 bool MessageFilterValidator::validateAttribute(std::string& attrName, Api::Tizen::MatchFlag& matchFlag,
159                                 Api::Tizen::AnyPtr& matchValue, int depth){
160                         bool retBool = false;
161
162                         if(matchValue == NULL)
163                                 return false;
164
165                         if(validateAttributeEach(attrName, matchFlag, matchValue, depth) == false){
166                                 MsgLogWanning(">>>[Warning] attrName:[" << attrName << "] is invalid value:[" << matchValue->toString() << "]");
167                                 return false;
168                         }
169
170                         retBool = FilterValidator::validateAttribute(attrName, matchFlag, matchValue, depth);
171                         LogDebug(">>> retBool:" << retBool);
172                         return retBool;
173                 }
174
175                 bool MessageFilterValidator::validateAttributeEach(std::string& attrName,MatchFlag& matchFlag, Api::Tizen::AnyPtr& value, int depth){
176                         LogDebug("<<< attrName:[" << attrName << "], value:[" << value->toString() << "]");
177
178                         if(attrName.compare(MessageFilterValidatorFactory::ATTRIBUTE_TYPE)==0){
179                                 if(m_isTypeSetted == true){
180                                         MsgLogError(">>> [ERROR] duplicated type :[" << attrName << "], value:[" <<
181                                                         value->toString()<< "]");
182                                         return false;
183                                 }else{
184                                         m_isTypeSetted = true;
185                                 }
186                         }
187
188                         if(m_attributeAndMatchFlagsMap.count(attrName) == 0)
189                         {
190                                 MsgLogWanning(">>>[Waning] attrName is not supported:[" << attrName << "]");
191                                 return false;
192                         }
193
194                         std::vector<std::string> vec = m_attributeAndMatchFlagsMap.find(attrName)->second;
195
196                         if(vectorContains(vec, matchFlag)==false){
197                                 MsgLogWanning(">>>[Waning]MatchFlag check fail unsupported flag:[" << matchFlag << "] for Attribute:[" << attrName << "]");
198                                 return false;
199                         }
200
201                         LogDebug(">>> return true");
202                         return true;
203                 }
204
205                         }       //namespace Messaging
206         }
207
208 }