Skip to content

Commit 4569941

Browse files
author
Adrian Moreno
committed
Merge branch 'api_v2'
2 parents e14a4ea + 9647cb2 commit 4569941

46 files changed

Lines changed: 3342 additions & 1296 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<name>StackSync Server</name>
55
<groupId>com.stacksync</groupId>
66
<artifactId>stacksync-server</artifactId>
7-
<version>0.5.1</version>
7+
<version>0.6.10</version>
88
<repositories>
99
<repository>
1010
<id>Sonatype repository</id>
@@ -31,12 +31,12 @@
3131
<dependency>
3232
<groupId>com.stacksync</groupId>
3333
<artifactId>stacksync-commons</artifactId>
34-
<version>1.3.8</version>
34+
<version>1.4.1</version>
3535
</dependency>
3636
<dependency>
3737
<groupId>objectmq</groupId>
3838
<artifactId>objectmq</artifactId>
39-
<version>0.5.6</version>
39+
<version>0.5.7</version>
4040
</dependency>
4141
<dependency>
4242
<groupId>junit</groupId>
@@ -176,4 +176,4 @@
176176
</plugin>
177177
</plugins>
178178
</build>
179-
</project>
179+
</project>

src/main/java/com/stacksync/syncservice/db/DAOUtil.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import com.stacksync.commons.models.Chunk;
1616
import com.stacksync.commons.models.Item;
1717
import com.stacksync.commons.models.ItemMetadata;
18+
import com.stacksync.commons.models.User;
19+
import com.stacksync.commons.models.UserWorkspace;
1820
import com.stacksync.commons.models.Workspace;
1921

2022
/**
@@ -226,7 +228,7 @@ public static List<String> getArrayFromResultSet(ResultSet rs, String field) {
226228
List<String> result;
227229

228230
try {
229-
Array arrayChunks = rs.getArray("chunks");
231+
Array arrayChunks = rs.getArray(field);
230232
String[] chunks = (String[]) arrayChunks.getArray();
231233
result = Arrays.asList(chunks);
232234

@@ -283,6 +285,24 @@ public static ItemMetadata getItemMetadataFromResultSet(ResultSet result)
283285
return metadata;
284286
}
285287

288+
public static UserWorkspace getUserWorkspaceFromResultSet(ResultSet result)
289+
throws SQLException {
290+
291+
User user = new User();
292+
user.setId(UUID.fromString(result.getString("id")));
293+
user.setName(result.getString("name"));
294+
user.setEmail(result.getString("email"));
295+
296+
Workspace workspace = new Workspace();
297+
workspace.setId(UUID.fromString(result.getString("workspace_id")));
298+
299+
UserWorkspace userWorkspace = new UserWorkspace(user, workspace);
300+
userWorkspace.setOwner(result.getBoolean("is_owner"));
301+
userWorkspace.setJoinedAt(result.getDate("joined_at"));
302+
303+
return userWorkspace;
304+
}
305+
286306
public static Chunk getChunkFromResultSet(ResultSet result) throws SQLException {
287307

288308
Chunk chunk = new Chunk();

src/main/java/com/stacksync/syncservice/db/ItemDAO.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ public interface ItemDAO {
2828
public ItemMetadata findByUserId(UUID serverUserId, Boolean includeDeleted) throws DAOException;
2929

3030
public ItemMetadata findItemVersionsById(Long id) throws DAOException;
31+
32+
public List<String> migrateItem(Long itemId, UUID workspaceId) throws DAOException;
3133

3234
}

src/main/java/com/stacksync/syncservice/db/WorkspaceDAO.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.UUID;
55

66
import com.stacksync.commons.models.User;
7+
import com.stacksync.commons.models.UserWorkspace;
78
import com.stacksync.commons.models.Workspace;
89
import com.stacksync.syncservice.exceptions.dao.DAOException;
910

@@ -22,7 +23,11 @@ public interface WorkspaceDAO {
2223
public void update(User user, Workspace workspace) throws DAOException;
2324

2425
public void addUser(User user, Workspace workspace) throws DAOException;
26+
27+
public void deleteUser(User user, Workspace workspace) throws DAOException;
2528

2629
public void delete(UUID id) throws DAOException;
30+
31+
public List<UserWorkspace> getMembersById(UUID workspaceId) throws DAOException;
2732

2833
}

src/main/java/com/stacksync/syncservice/db/postgresql/PostgresqlItemDAO.java

Lines changed: 77 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -170,29 +170,34 @@ public List<ItemMetadata> getItemsByWorkspaceId(UUID workspaceId)
170170
@Override
171171
public List<ItemMetadata> getItemsById(Long id) throws DAOException {
172172
Object[] values = { id };
173-
173+
174174
String query = "WITH RECURSIVE "
175175
+ " q AS "
176176
+ " ( "
177-
+ " SELECT i.id as item_id, "
178-
+ " i.parent_id, i.filename, iv.version, i.client_parent_file_version, "
179-
+ " i.is_folder, iv.size, iv.status, "
180-
+ " i.mimetype, iv.checksum, iv.client_name, "
181-
+ " iv.modified_at, ARRAY[i.id] AS level_array "
177+
+ " SELECT i.id AS item_id, i.parent_id, i.client_parent_file_version, "
178+
+ " i.filename, iv.id AS version_id, iv.version, i.is_folder, "
179+
+ " i.workspace_id, "
180+
+ " iv.size, iv.status, i.mimetype, "
181+
+ " iv.checksum, iv.device_id, iv.modified_at, "
182+
+ " ARRAY[i.id] AS level_array "
182183
+ " FROM item i "
183184
+ " INNER JOIN item_version iv ON i.id = iv.item_id AND i.latest_version = iv.version "
184185
+ " WHERE i.id = ? "
185186
+ " UNION ALL "
186-
+ " SELECT i2.id as item_id, i2.parent_id, i2.client_parent_file_version, "
187-
+ " i2.filename, iv2.version, i2.is_folder, "
188-
+ " iv2.size, iv2.status, i2.mimetype, "
189-
+ " iv2.checksum, iv2.client_name, iv2.modified_at, "
190-
+ " q.level_array || i2.id "
187+
+ " SELECT i2.id AS item_id, i2.parent_id, i2.client_parent_file_version, "
188+
+ " i2.filename, iv2.id AS version_id, iv2.version, i2.is_folder, "
189+
+ " i2.workspace_id, "
190+
+ " iv2.size, iv2.status, i2.mimetype, "
191+
+ " iv2.checksum, iv2.device_id, iv2.modified_at, "
192+
+ " q.level_array || i2.id "
191193
+ " FROM q "
192-
+ " JOIN item i2 ON i2.parent_id = q.id "
194+
+ " JOIN item i2 ON i2.parent_id = q.item_id "
193195
+ " INNER JOIN item_version iv2 ON i2.id = iv2.item_id AND i2.latest_version = iv2.version "
194-
+ " ) " + " SELECT array_upper(level_array, 1) as level, q.* "
195-
+ " FROM q " + " ORDER BY " + " level_array ASC";
196+
+ " ) "
197+
+ " SELECT array_upper(level_array, 1) as level, q.* "
198+
+ " FROM q "
199+
+ " ORDER BY "
200+
+ " level_array ASC";
196201

197202
ResultSet result = null;
198203
List<ItemMetadata> list = new ArrayList<ItemMetadata>();
@@ -367,10 +372,10 @@ public ItemMetadata findItemVersionsById(Long fileId) throws DAOException {
367372
// TODO: check include_deleted
368373
Object[] values = { fileId };
369374

370-
String query = "SELECT i.id AS item_id, i.parent_id, i.client_parent_file_version, i.filename, i.is_folder, i.mimetype, "
371-
+ " iv.version, iv.size, iv.status, iv.checksum, "
375+
String query = "SELECT i.id AS item_id, i.parent_id, i.client_parent_file_version, i.filename, i.is_folder, i.mimetype, i.workspace_id, "
376+
+ " iv.version, iv.size, iv.status, iv.checksum, iv.device_id, "
372377
+ " iv.modified_at, '1' AS level, '' AS path FROM item i "
373-
+ " inner join item_version iv on iv.item_id = i.id where i.id = ? order by iv.version DESC ";
378+
+ " inner join item_version iv on iv.item_id = i.id where i.id = ? ORDER BY iv.version DESC ";
374379

375380
ResultSet result = null;
376381

@@ -411,6 +416,60 @@ private boolean resultSetHasRows(ResultSet resultSet) {
411416
}
412417
return hasRows;
413418
}
414-
419+
420+
@Override
421+
public List<String> migrateItem(Long itemId, UUID workspaceId) throws DAOException{
422+
423+
Object[] values = { itemId, workspaceId.toString() };
424+
425+
String query = "WITH RECURSIVE "
426+
+ " q AS "
427+
+ " ( "
428+
+ " SELECT i.* "
429+
+ " FROM item i "
430+
+ " WHERE i.id = ? "
431+
+ " UNION ALL "
432+
+ " SELECT i2.* "
433+
+ " FROM q "
434+
+ " JOIN item i2 ON i2.parent_id = q.id "
435+
+ " ) "
436+
+ " UPDATE item i3 SET workspace_id = ?::uuid "
437+
+ " FROM q "
438+
+ " WHERE q.id = i3.id";
439+
440+
executeUpdate(query, values);
441+
442+
List<String> chunksToMigrate;
443+
444+
try{
445+
chunksToMigrate = getChunksToMigrate(itemId);
446+
}catch (SQLException e){
447+
throw new DAOException(e);
448+
}
449+
450+
return chunksToMigrate;
451+
452+
}
453+
454+
private List<String> getChunksToMigrate(Long itemId) throws DAOException, SQLException {
455+
456+
Object[] values = { itemId };
457+
458+
String query = "SELECT get_unique_chunks_to_migrate(?) AS chunks";
459+
460+
ResultSet result = executeQuery(query, values);
461+
List<String> chunksList;
462+
463+
if (result.next()){
464+
chunksList = DAOUtil.getArrayFromResultSet(result, "chunks");
465+
}
466+
else{
467+
chunksList = new ArrayList<String>();
468+
}
469+
470+
471+
return chunksList;
472+
473+
}
415474

416475
}

src/main/java/com/stacksync/syncservice/db/postgresql/PostgresqlUserDAO.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public User findById(UUID userID) throws DAOException {
3939
logger.error(e);
4040
throw new DAOException(DAOError.INTERNAL_SERVER_ERROR);
4141
}
42+
43+
if (user == null){
44+
throw new DAOException(DAOError.USER_NOT_FOUND);
45+
}
4246

4347
return user;
4448
}

src/main/java/com/stacksync/syncservice/db/postgresql/PostgresqlWorkspaceDAO.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
import com.stacksync.commons.models.Item;
1313
import com.stacksync.commons.models.User;
14+
import com.stacksync.commons.models.UserWorkspace;
1415
import com.stacksync.commons.models.Workspace;
1516
import com.stacksync.syncservice.db.DAOError;
17+
import com.stacksync.syncservice.db.DAOUtil;
1618
import com.stacksync.syncservice.db.WorkspaceDAO;
1719
import com.stacksync.syncservice.exceptions.dao.DAOException;
1820
import com.stacksync.syncservice.exceptions.dao.NoResultReturnedDAOException;
@@ -200,13 +202,32 @@ public void addUser(User user, Workspace workspace) throws DAOException {
200202

201203
executeUpdate(query, values);
202204
}
205+
206+
@Override
207+
public void deleteUser(User user, Workspace workspace) throws DAOException {
208+
209+
if (user == null || !user.isValid()) {
210+
throw new IllegalArgumentException("User not valid");
211+
} else if (workspace == null || !workspace.isValid()) {
212+
throw new IllegalArgumentException("Workspace not valid");
213+
}
214+
215+
Object[] values = { workspace.getId(), user.getId() };
216+
217+
String query = "DELETE FROM workspace_user WHERE workspace_id=?::uuid, user_id=?::uuid";
218+
219+
executeUpdate(query, values);
220+
}
203221

204222
@Override
205223
public Workspace getByItemId(Long itemId) throws DAOException {
206224
ResultSet resultSet = null;
207225
Workspace workspace = null;
208226

209-
String query = "SELECT * FROM workspace w INNER JOIN item i ON w.id = i.workspace_id WHERE i.id = ?::uuid";
227+
String query = "SELECT * FROM workspace w " +
228+
" INNER JOIN workspace_user wu ON wu.workspace_id = w.id " +
229+
" INNER JOIN item i ON w.id = i.workspace_id " +
230+
" WHERE i.id = ?";
210231

211232
try {
212233
resultSet = executeQuery(query, new Object[] { itemId });
@@ -221,4 +242,32 @@ public Workspace getByItemId(Long itemId) throws DAOException {
221242

222243
return workspace;
223244
}
245+
246+
@Override
247+
public List<UserWorkspace> getMembersById(UUID workspaceId) throws DAOException {
248+
ResultSet resultSet = null;
249+
List<UserWorkspace> users = new ArrayList<UserWorkspace>();
250+
251+
String query = " SELECT u.*, CASE WHEN u.id=w.owner_id THEN True ELSE False END AS is_owner " +
252+
" , wu.created_at AS joined_at, wu.workspace_id " +
253+
" FROM workspace w " +
254+
" INNER JOIN workspace_user wu ON wu.workspace_id = w.id " +
255+
" INNER JOIN user1 u ON wu.user_id = u.id " +
256+
" WHERE w.id = ?::uuid";
257+
258+
try {
259+
resultSet = executeQuery(query, new Object[] { workspaceId });
260+
261+
while (resultSet.next()) {
262+
UserWorkspace userWorkspace = DAOUtil.getUserWorkspaceFromResultSet(resultSet);
263+
users.add(userWorkspace);
264+
}
265+
} catch (SQLException e) {
266+
logger.error(e);
267+
throw new DAOException(DAOError.INTERNAL_SERVER_ERROR);
268+
}
269+
270+
return users;
271+
}
272+
224273
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.stacksync.syncservice.exceptions;
2+
3+
public class InternalServerError extends Exception {
4+
5+
private static final long serialVersionUID = 7240966151447816363L;
6+
7+
public InternalServerError() {
8+
super();
9+
}
10+
11+
public InternalServerError(String message) {
12+
super(message);
13+
}
14+
15+
public InternalServerError(String message, Throwable cause) {
16+
super(message, cause);
17+
}
18+
19+
public InternalServerError(Throwable cause) {
20+
super(cause);
21+
}
22+
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
package com.stacksync.syncservice.exceptions.storage;
3+
4+
public class ObjectNotFoundException extends Exception {
5+
6+
private static final long serialVersionUID = -5254463856122396229L;
7+
8+
public ObjectNotFoundException(String message) {
9+
super(message);
10+
}
11+
12+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.stacksync.syncservice.handler;
2+
3+
import java.util.List;
4+
5+
import com.stacksync.commons.models.Item;
6+
import com.stacksync.commons.models.ItemMetadata;
7+
import com.stacksync.commons.models.User;
8+
import com.stacksync.syncservice.rpc.messages.APICommitResponse;
9+
import com.stacksync.syncservice.rpc.messages.APICreateFolderResponse;
10+
import com.stacksync.syncservice.rpc.messages.APIDeleteResponse;
11+
import com.stacksync.syncservice.rpc.messages.APIGetFolderMembersResponse;
12+
import com.stacksync.syncservice.rpc.messages.APIGetMetadata;
13+
import com.stacksync.syncservice.rpc.messages.APIGetVersions;
14+
import com.stacksync.syncservice.rpc.messages.APIGetWorkspaceInfoResponse;
15+
import com.stacksync.syncservice.rpc.messages.APIRestoreMetadata;
16+
import com.stacksync.syncservice.rpc.messages.APIShareFolderResponse;
17+
import com.stacksync.syncservice.rpc.messages.APIUnshareFolderResponse;
18+
19+
public interface APIHandler {
20+
21+
public APIGetMetadata getMetadata(User user, Long fileId, Boolean includeChunks, Long version, Boolean isFolder);
22+
23+
public APICommitResponse createFile(User user, ItemMetadata fileToSave);
24+
25+
public APICommitResponse updateData(User user, ItemMetadata fileToUpdate);
26+
27+
public APICommitResponse updateMetadata(User user, ItemMetadata fileToUpdate);
28+
29+
public APICreateFolderResponse createFolder(User user, ItemMetadata item);
30+
31+
public APIRestoreMetadata restoreMetadata(User user, ItemMetadata item);
32+
33+
public APIDeleteResponse deleteItem(User user, ItemMetadata item);
34+
35+
public APIGetVersions getVersions(User user, ItemMetadata item);
36+
37+
public APIShareFolderResponse shareFolder(User user, Item item, List<String> emails);
38+
39+
public APIUnshareFolderResponse unshareFolder(User user, Item item, List<String> emails);
40+
41+
public APIGetWorkspaceInfoResponse getWorkspaceInfo(User user, ItemMetadata item);
42+
43+
public APIGetFolderMembersResponse getFolderMembers(User user, Item item);
44+
}

0 commit comments

Comments
 (0)