1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "album_data_convertor.h"
17
18 #include <unistd.h>
19 #include <sys/stat.h>
20
21 namespace OHOS {
22 namespace FileManagement {
23 namespace CloudSync {
24 using namespace std;
25 using namespace NativeRdb;
26 using namespace Media;
27
28 /* record type */
29 const string AlbumDataConvertor::recordType_ = "album";
30
AlbumDataConvertor(OperationType type)31 AlbumDataConvertor::AlbumDataConvertor(OperationType type) : type_(type)
32 {
33 }
34
Convert(DriveKit::DKRecord & record,NativeRdb::ResultSet & resultSet)35 int32_t AlbumDataConvertor::Convert(DriveKit::DKRecord &record, NativeRdb::ResultSet &resultSet)
36 {
37 DriveKit::DKRecordData data;
38 /* properties */
39 RETURN_ON_ERR(HandleProperties(data, resultSet));
40 /* basic */
41 RETURN_ON_ERR(HandleAlbumName(data, resultSet));
42 RETURN_ON_ERR(HandleAlbumLogicType(data, resultSet));
43 RETURN_ON_ERR(HandleType(data, resultSet));
44 RETURN_ON_ERR(HandlePath(data, resultSet));
45
46 /* control info */
47 record.SetRecordType(recordType_);
48 if (type_ == ALBUM_CREATE) {
49 record.SetNewCreate(true);
50 RETURN_ON_ERR(HandleAlbumId(data, resultSet));
51 record.SetRecordId(data[ALBUM_ID]);
52 } else {
53 int32_t ret = FillRecordId(record, resultSet);
54 if (ret != E_OK) {
55 LOGE("fill record id err %{public}d", ret);
56 return ret;
57 }
58 data[ALBUM_ID] = DriveKit::DKRecordField(record.GetRecordId());
59 }
60
61 /* set data */
62 record.SetRecordData(data);
63
64 return E_OK;
65 }
66
67 /* record id */
FillRecordId(DriveKit::DKRecord & record,NativeRdb::ResultSet & resultSet)68 int32_t AlbumDataConvertor::FillRecordId(DriveKit::DKRecord &record,
69 NativeRdb::ResultSet &resultSet)
70 {
71 string val;
72 int32_t ret = GetString(PhotoAlbumColumns::ALBUM_CLOUD_ID, val, resultSet);
73 if (ret != E_OK) {
74 return ret;
75 }
76 record.SetRecordId(val);
77 return E_OK;
78 }
79
80 /* properties */
HandleProperties(DriveKit::DKRecordData & data,NativeRdb::ResultSet & resultSet)81 int32_t AlbumDataConvertor::HandleProperties(DriveKit::DKRecordData &data, NativeRdb::ResultSet &resultSet)
82 {
83 DriveKit::DKRecordFieldMap map;
84 /* general */
85 RETURN_ON_ERR(HandleGeneral(map, resultSet));
86 /* set map */
87 data[ALBUM_PROPERTIES] = DriveKit::DKRecordField(map);
88 return E_OK;
89 }
90
91 /* properties - general */
HandleGeneral(DriveKit::DKRecordFieldMap & map,NativeRdb::ResultSet & resultSet)92 int32_t AlbumDataConvertor::HandleGeneral(DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet)
93 {
94 auto size = GALLERY_ALBUM_COLUMNS.size();
95 /* size - 1: skip cloud id */
96 for (decltype(size) i = 0; i < size - 1; i++) {
97 const string &key = GALLERY_ALBUM_COLUMNS[i];
98 DataType type = GALLERY_ALBUM_COLUMN_TYPES[i];
99 switch (type) {
100 case DataType::INT: {
101 SET_RECORD_INT(key, resultSet, map);
102 break;
103 }
104 case DataType::LONG: {
105 SET_RECORD_LONG(key, resultSet, map);
106 break;
107 }
108 case DataType::DOUBLE: {
109 SET_RECORD_DOUBLE(key, resultSet, map);
110 break;
111 }
112 case DataType::STRING: {
113 SET_RECORD_STRING(key, resultSet, map);
114 break;
115 }
116 case DataType::BOOL: {
117 SET_RECORD_BOOL(key, resultSet, map);
118 break;
119 }
120 }
121 }
122 return E_OK;
123 }
124
Convert(DriveKit::DKRecord & record,NativeRdb::ValuesBucket & valueBucket)125 int32_t AlbumDataConvertor::Convert(DriveKit::DKRecord &record, NativeRdb::ValuesBucket &valueBucket)
126 {
127 DriveKit::DKRecordData data;
128 record.GetRecordData(data);
129
130 if (data.find(ALBUM_PROPERTIES) == data.end()) {
131 LOGE("record data donnot have properties set");
132 return E_INVAL_ARG;
133 }
134 DriveKit::DKRecordFieldMap properties = data[ALBUM_PROPERTIES];
135
136 auto size = GALLERY_ALBUM_COLUMNS.size();
137 for (decltype(size) i = 0; i < size - 1; i++) {
138 auto field = GALLERY_ALBUM_COLUMNS[i];
139 auto type = GALLERY_ALBUM_COLUMN_TYPES[i];
140 if (properties.find(field) == properties.end()) {
141 LOGE("filed %s not found in record.properties", field.c_str());
142 return E_DATA;
143 }
144 int32_t ret = HandleField(properties[field], valueBucket, field, type);
145 if (ret != E_OK) {
146 LOGE("HandleField %s failed", field.c_str());
147 return ret;
148 }
149 }
150
151 return E_OK;
152 }
153 } // namespace CloudSync
154 } // namespace FileManagement
155 } // namespace OHOS
156