Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / platform / API / Messaging / FolderFilterValidator.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  * FolderFilterValidator.cpp
18  *
19  *  Created on: 2011. 10. 31.
20  *      Author: sangtai
21  */
22
23 #include "FolderFilterValidator.h"
24 #include "FolderFilterValidatorFactory.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                 FolderFilterValidator::FolderFilterValidator(PropertyStructArray properties):FilterValidator(properties){
38                         m_isAccountIdSetted = false;
39
40                         initMatchFlagVectors();
41                         initAttributeAndMatchFlagsMap();
42                 }
43
44                 FolderFilterValidator::~FolderFilterValidator(){
45                 }
46
47                 void FolderFilterValidator::initMatchFlagVectors(){
48                         m_accountIdMatchFlagVec.push_back(FolderFilterValidatorFactory::MATCH_EXACTLY);
49                         m_folderPathMatchFlagVec.push_back(FolderFilterValidatorFactory::MATCH_EXACTLY);
50                 }
51
52                 void FolderFilterValidator::initAttributeAndMatchFlagsMap(){
53                         m_attributeAndMatchFlagsMap[FolderFilterValidatorFactory::ATTRIBUTE_ACCOUNTID]      = m_accountIdMatchFlagVec;
54                         m_attributeAndMatchFlagsMap[FolderFilterValidatorFactory::ATTRIBUTE_FOLDERPATH]        = m_folderPathMatchFlagVec;
55                 }
56
57                 bool FolderFilterValidator::validateAttributeRange(std::string& attrName, Api::Tizen::AnyPtr& initialValue, Api::Tizen::AnyPtr& endValue, int depth){
58                         bool retVal = false;
59                         return retVal;
60                 }
61
62                 bool FolderFilterValidator::vectorContains(std::vector<std::string>& vec, Api::Tizen::MatchFlag& value){
63                         std::string matchStr;
64                         switch(value)
65                         {
66                         case Api::Tizen::MATCH_EXACTLY:
67                                 matchStr = "EXACTLY";
68                                 break;
69                         case Api::Tizen::MATCH_FULLSTRING:
70                                 matchStr = "FULLSTRING";
71                                 break;
72                         case Api::Tizen::MATCH_CONTAINS:
73                                 matchStr = "CONTAINS";
74                                 break;
75                         case Api::Tizen::MATCH_STARTSWITH:
76                                 matchStr = "STARTSWITH";
77                                 break;
78                         case Api::Tizen::MATCH_ENDSWITH:
79                                 matchStr = "ENDSWITH";
80                                 break;
81                         case Api::Tizen::MATCH_EXISTS:
82                                 matchStr = "EXISTS";
83                                 break;
84                         default:
85                                 matchStr = "";
86                                 break;
87                         }
88
89                         std::vector<std::string>::iterator it;
90                         for(it=vec.begin(); it<vec.end(); it++){
91                                 if((*it).compare(matchStr) == 0){
92                                         return true;
93                                 }
94                         }
95
96                         return false;
97                 }
98
99                 bool FolderFilterValidator::validateAttribute(std::string& attrName, Api::Tizen::MatchFlag& matchFlag,
100                                 Api::Tizen::AnyArrayPtr& values, int depth){
101                         bool retBool = false;
102
103                         AnyArray::iterator iter;
104                         for(iter=values->begin(); iter!=values->end(); iter++){
105                                 AnyPtr value = *iter;
106                                 if(validateAttributeEach(attrName, matchFlag, value, depth) == false){
107                                         MsgLogWanning(">>>[Warning] attrName:[" << attrName << "] is invalid value:[" << value->toString() << "]");
108                                         return false;
109                                 }
110                         }
111                         LogDebug("## 001 ##");
112                         retBool = FilterValidator::validateAttribute(attrName, matchFlag, values, depth);
113                         LogDebug("## 002 ##");                  
114                         LogDebug(">>> retBool:" << retBool);
115                         return retBool;
116                 }
117
118                 bool FolderFilterValidator::validateAttributeEach(std::string& attrName, Api::Tizen::MatchFlag& matchFlag, Api::Tizen::AnyPtr& value, int depth){
119                         LogDebug("<<< attrName:[" << attrName << "], value:[" << value->toString() << "]");
120
121                         if(attrName.compare(FolderFilterValidatorFactory::ATTRIBUTE_ACCOUNTID)==0){
122                                 LogDebug("<<< m_isAccountIdSetted :[" << m_isAccountIdSetted << "]");
123                                 if(m_isAccountIdSetted == true){
124                                         LogError(">>> [ERROR] duplicated account :[" << attrName << "], value:[" <<
125                                                         value->toString()<< "]");
126                                         return false;
127                                 }else{
128                                         m_isAccountIdSetted = true;
129                                 }
130                         }
131
132                         if(m_attributeAndMatchFlagsMap.count(attrName) == 0)
133                         {
134                                 MsgLogWanning(">>>[Waning] attrName is not supported:[" << attrName << "]");
135                                 return false;                   
136                         }
137
138                         std::vector<std::string> vec = m_attributeAndMatchFlagsMap.find(attrName)->second;
139
140                         if(vectorContains(vec, matchFlag)==false){
141                                 MsgLogWanning(">>>[Waning]MatchFlag check fail unsupported flag:[" << matchFlag << "] for Attribute:[" << attrName << "]");
142                                 return false;
143                         }
144                         return true;
145                 }
146
147         }
148         }
149
150 }