1/*
2 * Copyright (C) 2014 The Android Open Source Project
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#include "DmtJavaPluginTree.h"
18#include "DmtJavaPluginNode.h"
19#include "DmtJavaPluginManager.h"
20#include "DmtJavaPluginCommon.h"
21
22DmtJavaPluginTree::DmtJavaPluginTree(const char* rootPath, DMMap<DMString, DMString>& mapParameters)
23   : DmtRWPluginTree(), mIsAtomic(FALSE)
24{
25    DmtJavaPlugin_Debug("Inside: DmtJavaPluginTree constructor...\n");
26    Init(rootPath);
27    DmtJavaPlugin_Debug("Finish init rootPath, and begin create new instance of DmtJavaPluginManager..\n");
28    m_pluginManager = new DmtJavaPluginManager(rootPath, mapParameters);
29    DmtJavaPlugin_Debug("Finish create new instance of DmtJavaPluginManager..\n");
30    m_parameters = mapParameters;
31}
32
33DmtJavaPluginTree::~DmtJavaPluginTree()
34{
35    m_pluginManager = NULL;
36    m_parameters.clear();
37}
38
39SYNCML_DM_RET_STATUS_T DmtJavaPluginTree::DeleteNode(const char* path)
40{
41    DmtJavaPlugin_Debug("Inside: DmtJavaPluginTree::DeleteNode, path = %s\n", path);
42    if (m_pluginManager == NULL)
43    {
44        DmtJavaPlugin_Debug("Plugin manager is not created\n");
45        return SYNCML_DM_FAIL;
46    }
47
48    SYNCML_DM_RET_STATUS_T res = DmtRWPluginTree::DeleteNode(path);
49    DmtJavaPlugin_Debug("DmtJavaPluginTree::DeleteNode: DmtRWPluginTree::DeleteNode res = %d\n", res);
50    if (res == SYNCML_DM_SUCCESS)
51    {
52        res = m_pluginManager->DeleteNode(path);
53    }
54    DmtJavaPlugin_Debug("Leave: DmtJavaPluginTree::DeleteNode, res = %d\n", res);
55    return res;
56}
57
58SYNCML_DM_RET_STATUS_T DmtJavaPluginTree::CreateInteriorNode(const char* path, PDmtNode& ptrCreatedNode)
59{
60    DmtJavaPlugin_Debug("Inside: DmtJavaPluginTree::CreateInteriorNode, path = %s \n", path);
61
62    if (m_pluginManager == NULL)
63    {
64        DmtJavaPlugin_Debug("Plugin manager is not created\n");
65        return SYNCML_DM_FAIL;
66    }
67
68    SYNCML_DM_RET_STATUS_T res = DmtRWPluginTree::CreateInteriorNode(path, ptrCreatedNode);
69    DmtJavaPlugin_Debug("Leave: DmtJavaPluginTree::CreateInteriorNode, res = %d\n", res);
70    return res;
71}
72
73SYNCML_DM_RET_STATUS_T DmtJavaPluginTree::CreateInteriorNodeInternal(const char* path,
74                                                                     PDmtNode& ptrCreatedNode,
75                                                                     const DMStringVector& childNodeNames)
76{
77    DmtJavaPlugin_Debug("Inside: DmtJavaPluginTree::CreateInteriorNodeInternal, path = %s\n", path);
78
79    SYNCML_DM_RET_STATUS_T res = m_pluginManager->CreateInteriorNode(path);
80    if (res == SYNCML_DM_SUCCESS)
81    {
82        PDmtJavaPluginNode pNode = new DmtJavaPluginNode(this, path, childNodeNames);
83        res = this->SetNode(path, static_cast<PDmtNode>(pNode));
84        if (res != SYNCML_DM_SUCCESS)
85        {
86            m_pluginManager->DeleteNode(path);
87        }
88        ptrCreatedNode = pNode;
89    }
90    DmtJavaPlugin_Debug("Leave: DmtJavaPluginTree::CreateInteriorNodeInternal, path = %s\n", path);
91    return res;
92}
93
94SYNCML_DM_RET_STATUS_T DmtJavaPluginTree::CreateLeafNode(const char* path, PDmtNode& ptrCreatedNode, const DmtData& value)
95{
96    DmtJavaPlugin_Debug("Inside: DmtJavaPluginTree::CreateLeafNode, path = %s\n", path);
97    if (m_pluginManager == NULL)
98    {
99        DmtJavaPlugin_Debug("Plugin manager is not created\n");
100        return SYNCML_DM_FAIL;
101    }
102
103    SYNCML_DM_RET_STATUS_T res = DmtRWPluginTree::CreateLeafNode(path, ptrCreatedNode, value);
104    DmtJavaPlugin_Debug("Leave: DmtJavaPluginTree::CreateLeafNode, res = %d\n", res);
105    return res;
106}
107
108SYNCML_DM_RET_STATUS_T DmtJavaPluginTree::CreateLeafNode(const char* path, PDmtNode& ptrCreatedNode, const DmtData& value, BOOLEAN isESN)
109{
110    DmtJavaPlugin_Debug("Inside: DmtJavaPluginTree::CreateLeafNode, path = %s\n", path);
111    if (m_pluginManager == NULL)
112    {
113        DmtJavaPlugin_Debug("Plugin manager is not created\n");
114        return SYNCML_DM_FAIL;
115    }
116
117    SYNCML_DM_RET_STATUS_T res = DmtRWPluginTree::CreateLeafNode(path, ptrCreatedNode, value, isESN);
118    DmtJavaPlugin_Debug("Leave: DmtJavaPluginTree::CreateLeafNode, res = %d\n", res);
119    return res;
120}
121
122SYNCML_DM_RET_STATUS_T DmtJavaPluginTree::CreateLeafNodeInternal(const char* path, PDmtNode& ptrCreatedNode, const DmtData& value)
123{
124    DmtJavaPlugin_Debug("Inside: DmtJavaPluginTree::CreateLeafNodeInternal, path = %s\n", path);
125
126    SYNCML_DM_RET_STATUS_T res = m_pluginManager->CreateLeafNode(path, value);
127    if (res == SYNCML_DM_SUCCESS)
128    {
129        PDmtJavaPluginNode pNode = new DmtJavaPluginNode(this, path, value);
130        res = this->SetNode(path, static_cast<PDmtNode>(pNode));
131        if (res != SYNCML_DM_SUCCESS)
132        {
133            m_pluginManager->DeleteNode(path);
134        }
135        ptrCreatedNode = pNode;
136    }
137    DmtJavaPlugin_Debug("Leave: DmtJavaPluginTree::CreateLeafNodeInternal, path = %s\n", path);
138    return res;
139}
140
141SYNCML_DM_RET_STATUS_T DmtJavaPluginTree::CreateLeafNodeInternal(const char* path, PDmtNode& ptrCreatedNode, const DmtData& value, BOOLEAN isESN)
142{
143    DmtJavaPlugin_Debug("Inside: DmtJavaPluginTree::CreateLeafNodeInternal, path = %s, isESN = %d\n", path, isESN);
144    return CreateLeafNodeInternal(path, ptrCreatedNode, value);
145}
146
147SYNCML_DM_RET_STATUS_T DmtJavaPluginTree::RenameNode(const char* path, const char* szNewNodeName)
148{
149    DmtJavaPlugin_Debug("Inside: DmtJavaPluginTree::RenameNode, path = %s\n", path);
150
151    if (m_pluginManager == NULL)
152    {
153        DmtJavaPlugin_Debug("Plugin manager is not created\n");
154        return SYNCML_DM_FAIL;
155    }
156
157    // This method is not supported by DmtPluginTree so we don't call java plug-in
158    // SYNCML_DM_RET_STATUS_T res = m_pluginManager->RenameNode(path, szNewNodeName);
159    return SYNCML_DM_FEATURE_NOT_SUPPORTED;
160}
161
162SYNCML_DM_RET_STATUS_T DmtJavaPluginTree::GetNode(const char* szPath, PDmtNode& ptrNode)
163{
164    if (!szPath)
165    {
166        szPath = "";
167    }
168    DmtJavaPlugin_Debug("DmtJavaPluginTree::GetNode(%s)\n", szPath);
169    return DmtPluginTree::GetNode(szPath, ptrNode);
170}
171
172SYNCML_DM_RET_STATUS_T DmtJavaPluginTree::BuildPluginTree()
173{
174    DmtJavaPlugin_Debug("Inside: DmtJavaPluginTree::BuildPluginTree\n");
175    if (m_pluginManager == NULL)
176    {
177        DmtJavaPlugin_Debug("Plugin manager is not created\n");
178        return SYNCML_DM_FAIL;
179    }
180
181    SYNCML_DM_RET_STATUS_T res = m_pluginManager->BuildPluginTree(this);
182    DmtJavaPlugin_Debug("Leave: DmtJavaPluginTree::BuildPluginTree, res = %d\n", res);
183    return res;
184}
185
186DMMap<DMString, DMString>& DmtJavaPluginTree::GetParameters()
187{
188    return m_parameters;
189}
190
191DMString& DmtJavaPluginTree::GetRootPath()
192{
193    return m_strRootPath;
194}
195
196SYNCML_DM_RET_STATUS_T DmtJavaPluginTree::GetNodeValueInternal(const char* path, DmtData& value)
197{
198    DmtJavaPlugin_Debug("Inside: DmtJavaPluginTree::GetNodeValueInternal, path = %s\n", path);
199    if (m_pluginManager == NULL)
200    {
201        DmtJavaPlugin_Debug("Plugin manager is not created\n");
202        return SYNCML_DM_FAIL;
203    }
204
205    SYNCML_DM_RET_STATUS_T res = m_pluginManager->GetNodeValue(path, value);
206    DmtJavaPlugin_Debug("Leave: DmtJavaPluginTree::GetNodeValueInternal, res = %d\n", res);
207    return res;
208}
209
210SYNCML_DM_RET_STATUS_T DmtJavaPluginTree::SetNodeValueInternal(const char* path, const DmtData& value)
211{
212    DmtJavaPlugin_Debug("Inside: DmtJavaPluginTree::SetNodeValueInternal, path = %s\n", path);
213    if (m_pluginManager == NULL)
214    {
215        DmtJavaPlugin_Debug("Plugin manager is not created\n");
216        return SYNCML_DM_FAIL;
217    }
218
219    SYNCML_DM_RET_STATUS_T res = m_pluginManager->SetNodeValue(path, value);
220    DmtJavaPlugin_Debug("Leave: DmtJavaPluginTree::SetNodeValueInternal, res = %d\n", res);
221    return res;
222}
223
224BOOLEAN DmtJavaPluginTree::IsAtomic()
225{
226    DmtJavaPlugin_Debug("DmtJavaPluginTree::IsAtomic\n");
227    return mIsAtomic;
228}
229
230SYNCML_DM_RET_STATUS_T DmtJavaPluginTree::Flush()
231{
232    DmtJavaPlugin_Debug("Inside: DmtJavaPluginTree::Flush\n");
233
234    //TODO: Workaround. Commit will be called by engine in any case.
235    Commit();
236
237    // ehb005:should not set this NULL because tree could be accessed again after flush
238    //m_pluginManager = NULL;
239
240    SYNCML_DM_RET_STATUS_T res = DmtRWPluginTree::Flush();
241
242    DmtJavaPlugin_Debug("Leave: DmtJavaPluginTree::Flush, res = %d\n", res);
243
244    return res;
245}
246
247SYNCML_DM_RET_STATUS_T DmtJavaPluginTree::Commit()
248{
249    DmtJavaPlugin_Debug("Inside: DmtJavaPluginTree::Commit\n");
250    if (m_pluginManager == NULL)
251    {
252        DmtJavaPlugin_Debug("Plugin manager is not created\n");
253        return SYNCML_DM_FAIL;
254    }
255
256    mIsAtomic = FALSE;
257
258    SYNCML_DM_RET_STATUS_T res = m_pluginManager->Commit();
259    DmtJavaPlugin_Debug("Leave: DmtJavaPluginTree::Commit, res = %d\n", res);
260    return res;
261}
262
263SYNCML_DM_RET_STATUS_T DmtJavaPluginTree::Begin()
264{
265    DmtJavaPlugin_Debug("DmtJavaPluginTree::Begin\n");
266    mIsAtomic = TRUE;
267    return SYNCML_DM_SUCCESS;
268}
269
270SYNCML_DM_RET_STATUS_T DmtJavaPluginTree::Rollback()
271{
272    DmtJavaPlugin_Debug("DmtJavaPluginTree::Rollback\n");
273    mIsAtomic = FALSE;
274    return SYNCML_DM_FEATURE_NOT_SUPPORTED;
275}
276