fix setting fileid for existing file
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / datamodel / FileDataStorageManager.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package eu.alefzero.owncloud.datamodel;
20
21 import java.util.Vector;
22
23 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
24 import android.accounts.Account;
25 import android.content.ContentProviderClient;
26 import android.content.ContentResolver;
27 import android.content.ContentValues;
28 import android.database.Cursor;
29 import android.net.Uri;
30 import android.os.RemoteException;
31 import android.util.Log;
32
33 public class FileDataStorageManager implements DataStorageManager {
34
35 private ContentResolver mContentResolver;
36 private ContentProviderClient mContentProvider;
37 private Account mAccount;
38
39 private static String TAG = "FileDataStorageManager";
40
41 public FileDataStorageManager(Account account, ContentResolver cr) {
42 mContentProvider = null;
43 mContentResolver = cr;
44 mAccount = account;
45 }
46
47 public FileDataStorageManager(Account account, ContentProviderClient cp) {
48 mContentProvider = cp;
49 mContentResolver = null;
50 mAccount = account;
51 }
52
53 @Override
54 public OCFile getFileByPath(String path) {
55 Cursor c = getCursorForValue(ProviderTableMeta.FILE_PATH, path);
56 if (c.moveToFirst())
57 return createFileInstance(c);
58 return null;
59 }
60
61 @Override
62 public OCFile getFileById(long id) {
63 Cursor c = getCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
64 if (c.moveToFirst())
65 return createFileInstance(c);
66 return null;
67 }
68
69 @Override
70 public boolean fileExists(long id) {
71 return fileExists(ProviderTableMeta._ID, String.valueOf(id));
72 }
73
74 @Override
75 public boolean fileExists(String path) {
76 return fileExists(ProviderTableMeta.FILE_PATH, path);
77 }
78
79 @Override
80 public boolean saveFile(OCFile file) {
81 boolean overriden = false;
82 ContentValues cv = new ContentValues();
83 cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
84 cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
85 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
86 cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
87 cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
88 if (file.getParentId() != 0)
89 cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
90 cv.put(ProviderTableMeta.FILE_PATH, file.getPath());
91 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
92 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
93
94 if (fileExists(file.getPath())) {
95 file.setFileId(getFileByPath(file.getPath()).getFileId());
96 overriden = true;
97 if (getContentResolver() != null) {
98 getContentResolver().update(ProviderTableMeta.CONTENT_URI,
99 cv,
100 ProviderTableMeta._ID + "=?",
101 new String[] {String.valueOf(file.getFileId())});
102 } else {
103 try {
104 getContentProvider().update(ProviderTableMeta.CONTENT_URI,
105 cv,
106 ProviderTableMeta._ID + "=?",
107 new String[] {String.valueOf(file.getFileId())});
108 } catch (RemoteException e) {
109 Log.e(TAG, "Fail to insert insert file to database " + e.getMessage());
110 }
111 }
112 } else {
113 Uri result_uri = null;
114 if (getContentResolver() != null) {
115 result_uri = getContentResolver().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
116 } else {
117 try {
118 result_uri = getContentProvider().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
119 } catch (RemoteException e) {
120 Log.e(TAG, "Fail to insert insert file to database " + e.getMessage());
121 }
122 }
123 if (result_uri != null) {
124 long new_id = Long.parseLong(result_uri.getPathSegments().get(1));
125 file.setFileId(new_id);
126 }
127 }
128
129 if (file.isDirectory() && file.needsUpdatingWhileSaving())
130 for (OCFile f : getDirectoryContent(file))
131 saveFile(f);
132
133 return overriden;
134 }
135
136 public void setAccount(Account account) {
137 mAccount = account;
138 }
139
140 public Account getAccount() {
141 return mAccount;
142 }
143
144 public void setContentResolver(ContentResolver cr) {
145 mContentResolver = cr;
146 }
147
148 public ContentResolver getContentResolver() {
149 return mContentResolver;
150 }
151
152 public void setContentProvider(ContentProviderClient cp) {
153 mContentProvider = cp;
154 }
155
156 public ContentProviderClient getContentProvider() {
157 return mContentProvider;
158 }
159
160 public Vector<OCFile> getDirectoryContent(OCFile f) {
161 if (f.isDirectory() && f.getFileId() != -1) {
162 Vector<OCFile> ret = new Vector<OCFile>();
163
164 Uri req_uri = Uri.withAppendedPath(
165 ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(f.getFileId()));
166 Cursor c = null;
167
168 if (getContentProvider() != null) {
169 try {
170 c = getContentProvider().query(req_uri, null, null, null, null);
171 } catch (RemoteException e) {
172 Log.e(TAG, e.getMessage());
173 return ret;
174 }
175 } else {
176 c = getContentResolver().query(req_uri, null, null, null, null);
177 }
178
179 if (c.moveToFirst()) {
180 do {
181 OCFile child = createFileInstance(c);
182 ret.add(child);
183 } while (c.moveToNext());
184 }
185
186 c.close();
187 return ret;
188 }
189 return null;
190 }
191
192
193 private boolean fileExists(String cmp_key, String value) {
194 Cursor c;
195 if (getContentResolver() != null) {
196 c = getContentResolver().query(ProviderTableMeta.CONTENT_URI,
197 null,
198 cmp_key + "=?",
199 new String[] {value},
200 null);
201 } else {
202 try {
203 c = getContentProvider().query(ProviderTableMeta.CONTENT_URI,
204 null,
205 cmp_key + "=?",
206 new String[] {value},
207 null);
208 } catch (RemoteException e) {
209 Log.e(TAG, "Couldn't determine file existance, assuming non existance: " + e.getMessage());
210 return false;
211 }
212 }
213 return c.moveToFirst();
214 }
215
216 private Cursor getCursorForValue(String key, String value) {
217 Cursor c = null;
218 if (getContentResolver() != null) {
219 c = getContentResolver().query(ProviderTableMeta.CONTENT_URI,
220 null,
221 key + "=?",
222 new String[] {value},
223 null);
224 } else {
225 try {
226 c = getContentProvider().query(ProviderTableMeta.CONTENT_URI,
227 null,
228 key + "=?",
229 new String[]{value},
230 null);
231 } catch (RemoteException e) {
232 Log.e(TAG, "Could not get file details: " + e.getMessage());
233 c = null;
234 }
235 }
236 return c;
237 }
238
239 private OCFile createFileInstance(Cursor c) {
240 OCFile file = null;
241 if (c != null) {
242 file = new OCFile(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH)));
243 file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
244 file.setParentId(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_PARENT)));
245 file.setStoragePath(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
246 file.setMimetype(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)));
247 file.setFileLength(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH)));
248 file.setCreationTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_CREATION)));
249 file.setModificationTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
250 }
251 return file;
252 }
253
254 }