• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "dk_error.h"
18 
19 #include <unistd.h>
20 #include <sys/stat.h>
21 
22 namespace OHOS {
23 namespace FileManagement {
24 namespace CloudSync {
25 using namespace std;
26 using namespace NativeRdb;
27 using namespace Media;
28 
29 /* record type */
30 const string AlbumDataConvertor::recordType_ = "album";
31 
AlbumDataConvertor(OperationType type)32 AlbumDataConvertor::AlbumDataConvertor(OperationType type) : type_(type)
33 {
34 }
35 
Convert(DriveKit::DKRecord & record,NativeRdb::ResultSet & resultSet)36 int32_t AlbumDataConvertor::Convert(DriveKit::DKRecord &record, NativeRdb::ResultSet &resultSet)
37 {
38     DriveKit::DKRecordData data;
39     /* basic */
40     RETURN_ON_ERR(HandleAlbumName(data, resultSet));
41     RETURN_ON_ERR(HandleAlbumLogicType(data, resultSet));
42     RETURN_ON_ERR(HandlePath(data, resultSet));
43     RETURN_ON_ERR(HandleType(data, resultSet));
44 
45     /* properties */
46     RETURN_ON_ERR(HandleProperties(data, resultSet));
47 
48     /* control info */
49     record.SetRecordType(recordType_);
50     int32_t albumType = -1;
51     if (GetInt(Media::PhotoAlbumColumns::ALBUM_TYPE, albumType, resultSet) == E_OK &&
52         albumType == AlbumType::SOURCE) {
53         HandleSourceAlbum(data, resultSet);
54     }
55     if (type_ == FILE_CREATE) {
56         record.SetNewCreate(true);
57         RETURN_ON_ERR(HandleAlbumId(data, resultSet));
58         RETURN_ON_ERR(HandleRecordId(record, resultSet));
59     } else {
60         int32_t ret = FillRecordId(record, resultSet);
61         if (ret != E_OK) {
62             LOGE("fill record id err %{public}d", ret);
63             return ret;
64         }
65         data[ALBUM_ID] = DriveKit::DKRecordField(record.GetRecordId());
66     }
67     /* set data */
68     record.SetRecordData(data);
69 
70     return E_OK;
71 }
72 
73 /* record id */
FillRecordId(DriveKit::DKRecord & record,NativeRdb::ResultSet & resultSet)74 int32_t AlbumDataConvertor::FillRecordId(DriveKit::DKRecord &record,
75     NativeRdb::ResultSet &resultSet)
76 {
77     string val;
78     int32_t ret = GetString(PhotoAlbumColumns::ALBUM_CLOUD_ID, val, resultSet);
79     if (ret != E_OK) {
80         return ret;
81     }
82     record.SetRecordId(val);
83     return E_OK;
84 }
85 
86 /* properties */
HandleProperties(DriveKit::DKRecordData & data,NativeRdb::ResultSet & resultSet)87 int32_t AlbumDataConvertor::HandleProperties(DriveKit::DKRecordData &data, NativeRdb::ResultSet &resultSet)
88 {
89     DriveKit::DKRecordFieldMap map;
90     /* general */
91     RETURN_ON_ERR(HandleGeneral(map, resultSet));
92 
93     /* set map */
94     data[ALBUM_PROPERTIES] = DriveKit::DKRecordField(map);
95     return E_OK;
96 }
97 
98 /* properties - general */
HandleGeneral(DriveKit::DKRecordFieldMap & map,NativeRdb::ResultSet & resultSet)99 int32_t AlbumDataConvertor::HandleGeneral(DriveKit::DKRecordFieldMap &map, NativeRdb::ResultSet &resultSet)
100 {
101     auto size = GALLERY_ALBUM_COLUMNS.size();
102     /* size - 1: skip cloud id */
103     for (decltype(size) i = 0; i < size; i++) {
104         const string &key = GALLERY_ALBUM_COLUMNS[i];
105         DataType type = GALLERY_ALBUM_COLUMN_TYPES[i];
106         switch (type) {
107             case DataType::INT: {
108                 SET_RECORD_INT(key, resultSet, map);
109                 break;
110             }
111             case DataType::LONG: {
112                 SET_RECORD_LONG(key, resultSet, map);
113                 break;
114             }
115             case DataType::DOUBLE: {
116                 SET_RECORD_DOUBLE(key, resultSet, map);
117                 break;
118             }
119             case DataType::STRING: {
120                 SET_RECORD_STRING(key, resultSet, map);
121                 break;
122             }
123             case DataType::BOOL: {
124                 SET_RECORD_BOOL(key, resultSet, map);
125                 break;
126             }
127         }
128     }
129     return E_OK;
130 }
131 
HandleRecordId(DriveKit::DKRecord & record,NativeRdb::ResultSet & resultSet)132 int32_t AlbumDataConvertor::HandleRecordId(DriveKit::DKRecord &record, NativeRdb::ResultSet &resultSet)
133 {
134     DriveKit::DKRecordData data;
135     record.GetRecordData(data);
136     string recordId = "";
137     int32_t ret = GetString(PhotoAlbumColumns::ALBUM_CLOUD_ID, recordId, resultSet);
138     if (ret != 0 && !recordId.empty()) {
139         record.SetRecordId(recordId);
140         return E_OK;
141     }
142     int32_t albumType = -1;
143     std::string prefix = "album-";
144     ret = GetInt(Media::PhotoAlbumColumns::ALBUM_TYPE, albumType, resultSet);
145     if (ret == E_OK && albumType == AlbumType::SOURCE) {
146         std::string bundleName = "";
147         GetString(TMP_ALBUM_BUNDLE_NAME, bundleName, resultSet);
148         std::replace(bundleName.begin(), bundleName.end(), '.', '-');
149         record.SetRecordId(prefix + bundleName);
150         return E_OK;
151     }
152     uuid_t uuid;
153     uuid_generate(uuid);
154     char str[UUID_STR_LENGTH] = {};
155     uuid_unparse(uuid, str);
156     std::string suffix(str);
157     record.SetRecordId(prefix + suffix);
158     return E_OK;
159 }
160 
HandleSourceAlbum(DriveKit::DKRecordData & data,ResultSet & resultSet)161 int32_t AlbumDataConvertor::HandleSourceAlbum(DriveKit::DKRecordData &data, ResultSet &resultSet)
162 {
163     DriveKit::DKRecordFieldMap map;
164     int32_t albumType = -1;
165     if (GetInt(Media::PhotoAlbumColumns::ALBUM_TYPE, albumType, resultSet) == E_OK &&
166         albumType == AlbumType::SOURCE) {
167         data[ALBUM_PROPERTIES].GetRecordMap(map);
168         RETURN_ON_ERR(HandleLocalLanguage(map, resultSet));
169         RETURN_ON_ERR(HandleBundleName(map, resultSet));
170     }
171     data[ALBUM_PROPERTIES] = DriveKit::DKRecordField(map);
172     return E_OK;
173 }
174 
ExtractBundleName(DriveKit::DKRecordFieldMap & map,NativeRdb::ValuesBucket & valueBucket)175 int32_t AlbumDataConvertor::ExtractBundleName(DriveKit::DKRecordFieldMap &map, NativeRdb::ValuesBucket &valueBucket)
176 {
177     string bundleName = "";
178     if (map[TMP_ALBUM_BUNDLE_NAME].GetString(bundleName) != DriveKit::DKLocalErrorCode::NO_ERROR) {
179         return E_INVAL_ARG;
180     }
181     valueBucket.PutString(TMP_ALBUM_BUNDLE_NAME, bundleName);
182     return E_OK;
183 }
184 
ExtractLocalLanguage(DriveKit::DKRecordFieldMap & map,NativeRdb::ValuesBucket & valueBucket)185 int32_t AlbumDataConvertor::ExtractLocalLanguage(DriveKit::DKRecordFieldMap &map, NativeRdb::ValuesBucket &valueBucket)
186 {
187     string localLanguage = "";
188     if (map[TMP_ALBUM_LOCAL_LANGUAGE].GetString(localLanguage) != DriveKit::DKLocalErrorCode::NO_ERROR) {
189         return E_INVAL_ARG;
190     }
191     valueBucket.PutString(TMP_ALBUM_LOCAL_LANGUAGE, localLanguage);
192     return E_OK;
193 }
194 
Convert(DriveKit::DKRecord & record,NativeRdb::ValuesBucket & valueBucket)195 int32_t AlbumDataConvertor::Convert(DriveKit::DKRecord &record, NativeRdb::ValuesBucket &valueBucket)
196 {
197     DriveKit::DKRecordData data;
198     record.GetRecordData(data);
199 
200     if (data.find(ALBUM_PROPERTIES) == data.end()) {
201         LOGE("record data donnot have properties set");
202         return E_INVAL_ARG;
203     }
204     DriveKit::DKRecordFieldMap properties = data[ALBUM_PROPERTIES];
205 
206     auto size = GALLERY_ALBUM_COLUMNS.size();
207     for (decltype(size) i = 0; i < size; i++) {
208         auto field = GALLERY_ALBUM_COLUMNS[i];
209         auto type = GALLERY_ALBUM_COLUMN_TYPES[i];
210         if (properties.find(field) == properties.end()) {
211             LOGE("filed %s not found in record.properties", field.c_str());
212             return E_DATA;
213         }
214         int32_t ret = HandleField(properties[field], valueBucket, field, type);
215         if (ret != E_OK) {
216             LOGE("HandleField %s failed", field.c_str());
217             return ret;
218         }
219     }
220     return E_OK;
221 }
222 } // namespace CloudSync
223 } // namespace FileManagement
224 } // namespace OHOS
225