-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathProjectServiceImpl.java
More file actions
351 lines (321 loc) · 12.3 KB
/
Copy pathProjectServiceImpl.java
File metadata and controls
351 lines (321 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
package com.google.appinventor.server;
import com.google.appinventor.server.project.CommonProjectService;
import com.google.appinventor.server.project.youngandroid.YoungAndroidProjectService;
import com.google.appinventor.server.storage.StorageIo;
import com.google.appinventor.server.storage.StorageIoInstanceHolder;
import com.google.appinventor.shared.rpc.RpcResult;
import com.google.appinventor.shared.rpc.project.FileDescriptor;
import com.google.appinventor.shared.rpc.project.FileDescriptorWithContent;
import com.google.appinventor.shared.rpc.project.NewProjectParameters;
import com.google.appinventor.shared.rpc.project.ProjectRootNode;
import com.google.appinventor.shared.rpc.project.ProjectService;
import com.google.appinventor.shared.rpc.project.UserProject;
import com.google.appinventor.shared.rpc.project.youngandroid.YoungAndroidProjectNode;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.logging.Logger;
/**
* The implementation of the RPC service which runs on the server.
*
* <p>Note that this service must be state-less so that it can be run on
* multiple servers.
*
*/
public class ProjectServiceImpl extends OdeRemoteServiceServlet implements ProjectService {
private static final Logger LOG = Logger.getLogger(ProjectServiceImpl.class.getName());
private static final long serialVersionUID = -8316312003804169166L;
private final transient StorageIo storageIo = StorageIoInstanceHolder.INSTANCE;
// RPC implementation for YoungAndroid projects
private final transient YoungAndroidProjectService youngAndroidProject =
new YoungAndroidProjectService(storageIo);
/**
* Creates a new project.
* @param projectType type of new project
* @param projectName name of new project
* @param params optional parameter (project type dependent)
*
* @return a {@link UserProject} for new project
*/
@Override
public UserProject newProject(String projectType, String projectName,
NewProjectParameters params) {
final String userId = userInfoProvider.getUserId();
long projectId = getProjectRpcImpl(userId, projectType).
newProject(userId, projectName, params);
return makeUserProject(userId, projectId);
}
/**
* Copies a project with a new name.
* @param oldProjectId old project ID
* @param newName new project name
*
* @return a {@link UserProject} for new project
*/
@Override
public UserProject copyProject(long oldProjectId, String newName){
final String userId = userInfoProvider.getUserId();
long projectId = getProjectRpcImpl(userId, oldProjectId).
copyProject(userId, oldProjectId, newName);
return makeUserProject(userId, projectId);
}
/**
* Deletes a project.
* @param projectId project ID
*/
@Override
public void deleteProject(long projectId) {
final String userId = userInfoProvider.getUserId();
getProjectRpcImpl(userId, projectId).deleteProject(userId, projectId);
}
/**
* Returns an array with project IDs.
*
* @return IDs of projects found by the back-end
*/
@Override
public long[] getProjects() {
List<Long> projects = storageIo.getProjects(userInfoProvider.getUserId());
long[] projectIds = new long[projects.size()];
int i = 0;
for (Long project : projects) {
projectIds[i++] = project;
}
return projectIds;
}
/**
* Returns a list with pairs of project id and name.
*
* @return list of pairs of project IDs names found by backend
*/
@Override
public List<UserProject> getProjectInfos() {
String userId = userInfoProvider.getUserId();
List<Long> projectIds = storageIo.getProjects(userId);
List<UserProject> projectInfos = Lists.newArrayListWithExpectedSize(projectIds.size());
for (Long projectId : projectIds) {
projectInfos.add(makeUserProject(userId, projectId));
}
return projectInfos;
}
/**
* Returns the root node for the given project.
* @param projectId project ID as received by {@link #getProjects()}
*
* @return root node of project
*/
@Override
public ProjectRootNode getProject(long projectId) {
final String userId = userInfoProvider.getUserId();
return getProjectRpcImpl(userId, projectId).getRootNode(userId, projectId);
}
/**
* Returns a string with the project settings.
* @param projectId project ID
*
* @return settings
*/
@Override
public String loadProjectSettings(long projectId) {
String userId = userInfoProvider.getUserId();
return storageIo.loadProjectSettings(userId, projectId);
}
/**
* Stores a string with the project settings.
* @param projectId project ID
* @param settings project settings
*/
@Override
public void storeProjectSettings(long projectId, String settings) {
String userId = userInfoProvider.getUserId();
getProjectRpcImpl(userId, projectId).storeProjectSettings(userId, projectId, settings);
}
/**
* Deletes a file in the given project.
* @param projectId project ID
* @param fileId ID of file to delete
* @return modification date for project
*/
@Override
public long deleteFile(long projectId, String fileId) {
final String userId = userInfoProvider.getUserId();
return getProjectRpcImpl(userId, projectId).deleteFile(userId, projectId, fileId);
}
/**
* Deletes all files that are contained directly in the given directory. Files
* in subdirectories are not deleted.
* @param projectId project ID
* @param directory path of the directory
* @return modification date for project
*/
@Override
public long deleteFiles(long projectId, String directory) {
final String userId = userInfoProvider.getUserId();
return getProjectRpcImpl(userId, projectId).deleteFiles(userId, projectId,
directory);
}
/**
* Loads the file information associated with a node in the project tree. The
* actual return value depends on the file kind. Source (text) files should
* typically return their contents. Image files will be more likely to return
* the URL that the browser can find them at.
*
* @param projectId project ID
* @param fileId project node whose source should be loaded
*
* @return implementation dependent
*/
@Override
public String load(long projectId, String fileId) {
final String userId = userInfoProvider.getUserId();
return getProjectRpcImpl(userId, projectId).load(userId, projectId, fileId);
}
/**
* Loads the file information associated with a node in the project tree. The
* actual return value is the raw file contents.
*
* @param projectId project ID
* @param fileId project node whose source should be loaded
*
* @return raw file content
*/
@Override
public byte [] loadraw(long projectId, String fileId) {
final String userId = userInfoProvider.getUserId();
return getProjectRpcImpl(userId, projectId).loadraw(userId, projectId, fileId);
}
/**
* Loads the contents of multiple files.
*
* @param files list containing file descriptor of files to be loaded
* @return list containing file descriptors and their associated content
*/
@Override
public List<FileDescriptorWithContent> load(List<FileDescriptor> files) {
List<FileDescriptorWithContent> result = Lists.newArrayList();
final String userId = userInfoProvider.getUserId();
for (FileDescriptor file : files) {
long projectId = file.getProjectId();
String fileId = file.getFileId();
result.add(new FileDescriptorWithContent(
projectId, fileId,
getProjectRpcImpl(userId, projectId).load(userId, projectId, fileId)));
}
return result;
}
/**
* Saves the content of the file associated with a node in the project tree.
*
* @param projectId project ID
* @param fileId project node whose source should be saved
* @param content content to be saved
* @return modification date for project
*
* @see #load(long, String)
*/
@Override
public long save(long projectId, String fileId, String content) {
// Log parameters except for content
final String userId = userInfoProvider.getUserId();
return getProjectRpcImpl(userId, projectId).save(userId, projectId, fileId,
content);
}
/**
* Saves the contents of multiple files.
*
* @param filesAndContent list containing file descriptors and their
* associated content
* @return modification date for last modified project of list
*/
@Override
public long save(List<FileDescriptorWithContent> filesAndContent) {
final String userId = userInfoProvider.getUserId();
long date = 0;
for (FileDescriptorWithContent fileAndContent : filesAndContent) {
long projectId = fileAndContent.getProjectId();
date = getProjectRpcImpl(userId, projectId).
save(userId, projectId, fileAndContent.getFileId(), fileAndContent.getContent());
}
return date;
}
/**
* Invokes a build command for the project on the back-end.
*
* @param projectId project ID
* @param target build target (optional, implementation dependent)
*
* @return results of build
*/
@Override
public RpcResult build(long projectId, String target) {
// Dispatch
final String userId = userInfoProvider.getUserId();
return getProjectRpcImpl(userId, projectId).build(
userInfoProvider.getUser(), projectId, target);
}
/**
* Gets the result of a build command for the project.
*
* @param projectId project ID
* @param target build target (optional, implementation dependent)
*
* @return results of build. The following values may be in RpcResult.result:
* 0: Build is done and was successful
* 1: Build is done and was unsuccessful
* -1: Build is not yet done.
*/
@Override
public RpcResult getBuildResult(long projectId, String target) {
// Dispatch
final String userId = userInfoProvider.getUserId();
return getProjectRpcImpl(userId, projectId).getBuildResult(
userInfoProvider.getUser(), projectId, target);
}
/*
* Write the serialized response out to stdout. This is a very unusual thing
* to do, but it allows us to create a static file version of the response
* without deploying a servlet.
*/
@Override
protected void onAfterResponseSerialized(String serializedResponse) {
System.out.println(serializedResponse); // COV_NF_LINE
}
private UserProject makeUserProject(String userId, long projectId) {
// TODO(user): note that multiple calls like this on the data store
// can be really inefficient. Make a storagIo.getProject() method to get
// all of this at once?
return new UserProject(projectId, storageIo.getProjectName(userId, projectId),
storageIo.getProjectType(userId, projectId),
storageIo.getProjectDateCreated(userId, projectId),
storageIo.getProjectDateModified(userId, projectId));
}
/*
* Returns the RPC implementation for the given project type.
*/
private CommonProjectService getProjectRpcImpl(final String userId, long projectId) {
String projectType = storageIo.getProjectType(userId, projectId);
if (!projectType.isEmpty()) {
return getProjectRpcImpl(userId, storageIo.getProjectType(userId, projectId));
} else {
throw CrashReport.createAndLogError(LOG, getThreadLocalRequest(),
"user=" + userId + ", project=" + projectId,
new IllegalArgumentException("Can't find project " + projectId));
}
}
private CommonProjectService getProjectRpcImpl(final String userId, String projectType) {
if (projectType.equals(YoungAndroidProjectNode.YOUNG_ANDROID_PROJECT_TYPE)) {
return youngAndroidProject;
} else {
throw CrashReport.createAndLogError(LOG, getThreadLocalRequest(), null,
new IllegalArgumentException("Unknown project type:" + projectType));
}
}
@Override
public long addFile(long projectId, String fileId) {
final String userId = userInfoProvider.getUserId();
return getProjectRpcImpl(userId, projectId).addFile(userId, projectId, fileId);
}
}