tizen 2.3.1 release
[framework/web/wearable/wrt-security.git] / ace / dao / AceDAOUtilities.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  *
18  *
19  * @file       AceDaoReadOnly.h
20  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
21  * @author     Grzegorz Krawczyk (g.krawczyk@samsung.com)
22  * @version    0.1
23  * @brief
24  */
25
26 #include <openssl/md5.h>
27 #include <dpl/assert.h>
28 #include <dpl/foreach.h>
29
30 #include <ace-dao-ro/AceDatabase.h>
31 #include <ace-dao-ro/AceDAOUtilities.h>
32 #include <ace-dao-ro/AceDAOReadOnly.h>
33
34 namespace AceDB {
35
36 namespace {
37 const char* ACE_DB_DATABASE = "/opt/dbspace/.ace.db";
38 DPL::DB::SqlConnection::Flag::Type ACE_DB_FLAGS =
39     DPL::DB::SqlConnection::Flag::UseLucene;
40 }
41
42 DPL::DB::ThreadDatabaseSupport AceDaoUtilities::m_databaseInterface(
43         ACE_DB_DATABASE, ACE_DB_FLAGS);
44
45 BaseAttribute::Type AceDaoUtilities::intToAttributeType(int val)
46 {
47     switch (val) {
48     case 0:
49         return BaseAttribute::Type::Subject;
50     case 1:
51         return BaseAttribute::Type::Environment;
52     case 2:
53         return BaseAttribute::Type::Resource;
54     case 3:
55         return BaseAttribute::Type::FunctionParam;
56     case 4:
57         return BaseAttribute::Type::WidgetParam;
58
59     default:
60         Assert(0 && "Unknown Attribute type value");
61         return BaseAttribute::Type::Subject; //remove compilation warrning
62     }
63 }
64
65 int AceDaoUtilities::attributeTypeToInt(BaseAttribute::Type type)
66 {
67     // we cannot cast enum -> int because this cast will be removed from next c++ standard
68     switch (type) {
69     case BaseAttribute::Type::Subject:
70         return 0;
71     case BaseAttribute::Type::Environment:
72         return 1;
73     case BaseAttribute::Type::Resource:
74         return 2;
75     case BaseAttribute::Type::FunctionParam:
76         return 3;
77     case BaseAttribute::Type::WidgetParam:
78         return 4;
79
80     default:
81         Assert(0 && "Unknown Attribute type!");
82         return 0; //remove compilation warrning
83     }
84 }
85
86 int AceDaoUtilities::preferenceToInt(PreferenceTypes p)
87 {
88     switch (p) {
89         case PreferenceTypes::PREFERENCE_PERMIT:
90         return 1;
91     case PreferenceTypes::PREFERENCE_DENY:
92         return 0;
93     case PreferenceTypes::PREFERENCE_BLANKET_PROMPT:
94         return 2;
95     case PreferenceTypes::PREFERENCE_SESSION_PROMPT:
96         return 3;
97     case PreferenceTypes::PREFERENCE_ONE_SHOT_PROMPT:
98         return 4;
99
100     default:
101         return -1;
102     }
103 }
104
105 PreferenceTypes AceDaoUtilities::intToPreference(int p)
106 {
107     switch (p) {
108     case 1:
109         return PreferenceTypes::PREFERENCE_PERMIT;
110     case 0:
111         return PreferenceTypes::PREFERENCE_DENY;
112     case 2:
113         return PreferenceTypes::PREFERENCE_BLANKET_PROMPT;
114     case 3:
115         return PreferenceTypes::PREFERENCE_SESSION_PROMPT;
116     case 4:
117         return PreferenceTypes::PREFERENCE_ONE_SHOT_PROMPT;
118
119     default:
120         return PreferenceTypes::PREFERENCE_DEFAULT;
121     }
122 }
123
124 VerdictTypes AceDaoUtilities::intToVerdict(int v)
125 {
126     switch (v) {
127     case -1:
128         return VerdictTypes::VERDICT_UNKNOWN;
129     case 0:
130         return VerdictTypes::VERDICT_DENY;
131     case 1:
132         return VerdictTypes::VERDICT_PERMIT;
133     case 2:
134         return VerdictTypes::VERDICT_INAPPLICABLE;
135
136     default:
137         Assert(0 && "Cannot convert int to verdict");
138         return VerdictTypes::VERDICT_UNKNOWN; // remove compile warrning
139     }
140 }
141
142 int AceDaoUtilities::verdictToInt(VerdictTypes v)
143 {
144     switch (v) {
145     case VerdictTypes::VERDICT_UNKNOWN:
146         return -1;
147     case VerdictTypes::VERDICT_DENY:
148         return 0;
149     case VerdictTypes::VERDICT_PERMIT:
150         return 1;
151     case VerdictTypes::VERDICT_INAPPLICABLE:
152         return 2;
153
154     default:
155         Assert(0 && "Unknown Verdict value");
156         return -1; // remove compile warrning
157     }
158 }
159
160 bool AceDaoUtilities::getSubjectByUri(const std::string &uri,
161                                       DPL::DB::ORM::ace::AceSubject::Row &row)
162 {
163     using namespace DPL::DB::ORM;
164     using namespace DPL::DB::ORM::ace;
165     ACE_DB_SELECT(select, AceSubject, &m_databaseInterface);
166     select->Where(Equals<AceSubject::id_uri>(DPL::FromUTF8String(uri)));
167     std::list<AceSubject::Row> rows = select->GetRowList();
168     if (rows.empty()) {
169         return false;
170     }
171
172     row = rows.front();
173     return true;
174 }
175
176 bool AceDaoUtilities::getResourceByUri(const std::string &uri,
177                                        DPL::DB::ORM::ace::AceDevCap::Row &row)
178 {
179     using namespace DPL::DB::ORM;
180     using namespace DPL::DB::ORM::ace;
181     ACE_DB_SELECT(select, AceDevCap, &m_databaseInterface);
182     select->Where(Equals<AceDevCap::id_uri>(DPL::FromUTF8String(uri)));
183     std::list<AceDevCap::Row> rows = select->GetRowList();
184     if (rows.empty()) {
185         return false;
186     }
187
188     row = rows.front();
189     return true;
190 }
191
192
193 }