Skip to content

Commit 67ceae4

Browse files
committed
增加导入导出
1 parent 87aa1bf commit 67ceae4

10 files changed

Lines changed: 406 additions & 4 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
# Mobile Tools for Java (J2ME)
1111
.mtj.tmp/
1212

13+
*.iml
14+
1315
# Package Files #
1416
*.jar
1517
*.war
@@ -20,3 +22,4 @@
2022

2123
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2224
hs_err_pid*
25+
/pom.xml

pom.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@
3939
</dependency>
4040
</dependencies>
4141

42-
</project>
42+
43+
44+
45+
</project>

src/main/java/com/javabaas/javasdk/JBApp.java

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ public void onSuccess(JBResult result) {
191191
JBApp app = getAppFromMap((Map<String, Object>) result.getData().get("result"));
192192
callback.done(true, app, null);
193193
}
194-
195194
}
196195

197196
@Override
@@ -294,6 +293,99 @@ public void onFailure(JBException error) {
294293
});
295294
}
296295

296+
public static JBAppExport export(String appId) throws JBException {
297+
final JBAppExport[] lists = {null};
298+
exportFromJavabaas(true, appId, new JBAppExportCallback() {
299+
@Override
300+
public void done(boolean success, JBAppExport appExport, JBException e) {
301+
if (success) {
302+
lists[0] = appExport;
303+
} else {
304+
JBExceptionHolder.add(e);
305+
}
306+
}
307+
});
308+
if (JBExceptionHolder.exists()) {
309+
throw JBExceptionHolder.remove();
310+
}
311+
return lists[0];
312+
}
313+
314+
public static void exportInBackground(String appId, JBAppExportCallback callback) {
315+
exportFromJavabaas(false, appId, callback);
316+
}
317+
318+
private static void exportFromJavabaas(final boolean sync, final String appId, final JBAppExportCallback callback) {
319+
String path = JBHttpClient.getAdminPath(appId + "/" + "export");
320+
JBHttpClient.INSTANCE().sendRequest(path, JBHttpMethod.GET, null, null, sync, new JBObjectCallback() {
321+
@Override
322+
public void onSuccess(JBResult result) {
323+
if (callback == null) {
324+
return;
325+
}
326+
if (result.getData() == null || result.getData().get("result") == null) {
327+
callback.done(false, null,new JBException(JBCode.APP_NOT_FOUND));
328+
} else {
329+
JBAppExport appExport = getAppExportFromMap((Map<String, Object>) result.getData().get("result"));
330+
callback.done(true, appExport, null);
331+
}
332+
}
333+
334+
@Override
335+
public void onFailure(JBException error) {
336+
if (callback != null) {
337+
callback.done(false, null, error);
338+
}
339+
}
340+
});
341+
}
342+
343+
public static void importData(String data) throws JBException {
344+
importDataToJavabaas(true, data, new JBImportCallback() {
345+
@Override
346+
public void done(boolean success, JBException e) {
347+
if (!success) {
348+
JBExceptionHolder.add(e);
349+
}
350+
}
351+
});
352+
if (JBExceptionHolder.exists()) {
353+
throw JBExceptionHolder.remove();
354+
}
355+
}
356+
357+
public static void importDataInBackground(String data, JBImportCallback callback) {
358+
importDataToJavabaas(false, data, callback);
359+
}
360+
361+
private static void importDataToJavabaas(final boolean sync, final String data, JBImportCallback callback) {
362+
String path = JBHttpClient.getAdminPath("import");
363+
Map<String, Object> body = JBUtils.readValue(data, Map.class);
364+
JBHttpClient.INSTANCE().sendRequest(path, JBHttpMethod.POST, null, body, sync, new JBObjectCallback() {
365+
@Override
366+
public void onSuccess(JBResult result) {
367+
if (callback == null) {
368+
return;
369+
}
370+
callback.done(true, null);
371+
}
372+
373+
@Override
374+
public void onFailure(JBException error) {
375+
if (callback == null) {
376+
return;
377+
}
378+
callback.done(false, error);
379+
}
380+
});
381+
}
382+
383+
private static JBAppExport getAppExportFromMap(Map<String, Object> map) {
384+
String exportStr = JBUtils.writeValueAsString(map);
385+
JBAppExport appExport = JBUtils.readValue(exportStr, JBAppExport.class);
386+
return appExport;
387+
}
388+
297389
private static List<JBApp> getAppListFromMap(LinkedHashMap<String, Object> map) {
298390
if (map == null || map.get("result") == null) {return new LinkedList<>();}
299391
List<Map<String, Object>> maps = (List<Map<String, Object>>) map.get("result");
@@ -545,4 +637,70 @@ public String toString() {
545637
}
546638
}
547639

640+
public static class JBAppExport {
641+
private String id;
642+
private String name;
643+
private String key;
644+
private String masterKey;
645+
private CloudSetting cloudSetting;
646+
private List<JBClazz.JBClazzExport> clazzs;
647+
private AppAccounts appAccounts;
648+
649+
public String getId() {
650+
return id;
651+
}
652+
653+
public void setId(String id) {
654+
this.id = id;
655+
}
656+
657+
public String getName() {
658+
return name;
659+
}
660+
661+
public void setName(String name) {
662+
this.name = name;
663+
}
664+
665+
public String getKey() {
666+
return key;
667+
}
668+
669+
public void setKey(String key) {
670+
this.key = key;
671+
}
672+
673+
public String getMasterKey() {
674+
return masterKey;
675+
}
676+
677+
public void setMasterKey(String masterKey) {
678+
this.masterKey = masterKey;
679+
}
680+
681+
public CloudSetting getCloudSetting() {
682+
return cloudSetting;
683+
}
684+
685+
public void setCloudSetting(CloudSetting cloudSetting) {
686+
this.cloudSetting = cloudSetting;
687+
}
688+
689+
public List<JBClazz.JBClazzExport> getClazzs() {
690+
return clazzs;
691+
}
692+
693+
public void setClazzs(List<JBClazz.JBClazzExport> clazzs) {
694+
this.clazzs = clazzs;
695+
}
696+
697+
public AppAccounts getAppAccounts() {
698+
return appAccounts;
699+
}
700+
701+
public void setAppAccounts(AppAccounts appAccounts) {
702+
this.appAccounts = appAccounts;
703+
}
704+
}
705+
548706
}

src/main/java/com/javabaas/javasdk/JBClazz.java

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,98 @@ public void onFailure(JBException error) {
293293
});
294294
}
295295

296+
public static JBClazzExport export(String className) throws JBException {
297+
final JBClazzExport[] lists = {null};
298+
exportFromJavabaas(true, className, new JBClazzExportCallback() {
299+
@Override
300+
public void done(boolean success, JBClazzExport clazzExport, JBException e) {
301+
if (success) {
302+
lists[0] = clazzExport;
303+
} else {
304+
JBExceptionHolder.add(e);
305+
}
306+
}
307+
});
308+
if (JBExceptionHolder.exists()) {
309+
throw JBExceptionHolder.remove();
310+
}
311+
return lists[0];
312+
}
313+
314+
public static void exportInBackground(String className, JBClazzExportCallback callback) {
315+
exportFromJavabaas(false, className, callback);
316+
}
317+
318+
private static void exportFromJavabaas(final boolean sync, final String className, final JBClazzExportCallback callback) {
319+
String path = JBHttpClient.getClazzPath(className + "/" + "export");
320+
JBHttpClient.INSTANCE().sendRequest(path, JBHttpMethod.GET, null, null, sync, new JBObjectCallback() {
321+
@Override
322+
public void onSuccess(JBResult result) {
323+
if (callback == null) {
324+
return;
325+
}
326+
if (result.getData() == null || result.getData().get("result") == null) {
327+
callback.done(false, null,new JBException(JBCode.CLAZZ_NOT_FOUND));
328+
} else {
329+
JBClazzExport clazzExport = getClazzExportFromMap((Map<String, Object>) result.getData().get("result"));
330+
callback.done(true, clazzExport, null);
331+
}
332+
}
333+
334+
@Override
335+
public void onFailure(JBException error) {
336+
if (callback != null) {
337+
callback.done(false, null, error);
338+
}
339+
}
340+
});
341+
}
342+
343+
public static void importData(String data) throws JBException {
344+
importDataToJavabaas(true, data, new JBImportCallback() {
345+
@Override
346+
public void done(boolean success, JBException e) {
347+
if (!success) {
348+
JBExceptionHolder.add(e);
349+
}
350+
}
351+
});
352+
if (JBExceptionHolder.exists()) {
353+
throw JBExceptionHolder.remove();
354+
}
355+
}
356+
357+
public static void importDataInBackground(String data, JBImportCallback callback) {
358+
importDataToJavabaas(false, data, callback);
359+
}
360+
361+
private static void importDataToJavabaas(final boolean sync, final String data, JBImportCallback callback) {
362+
String path = JBHttpClient.getClazzPath("import");
363+
Map<String, Object> body = JBUtils.readValue(data, Map.class);
364+
JBHttpClient.INSTANCE().sendRequest(path, JBHttpMethod.POST, null, body, sync, new JBObjectCallback() {
365+
@Override
366+
public void onSuccess(JBResult result) {
367+
if (callback != null) {
368+
callback.done(true, null);
369+
}
370+
}
371+
372+
@Override
373+
public void onFailure(JBException error) {
374+
if (callback == null) {
375+
return;
376+
}
377+
callback.done(false, error);
378+
}
379+
});
380+
}
381+
382+
private static JBClazzExport getClazzExportFromMap(Map<String, Object> map) {
383+
String exportStr = JBUtils.writeValueAsString(map);
384+
JBClazzExport clazzExport = JBUtils.readValue(exportStr, JBClazzExport.class);
385+
return clazzExport;
386+
}
387+
296388
private static List<JBClazz> getClazzListFromMap(LinkedHashMap<String, Object> map) {
297389
if (map == null || map.get("result") == null) {return new LinkedList<>();}
298390
List<Map<String, Object>> maps = (List<Map<String, Object>>) map.get("result");
@@ -387,4 +479,52 @@ public String toString() {
387479

388480
}
389481

482+
public static class JBClazzExport {
483+
private String id;
484+
private String name;
485+
private JBClazzAcl acl;
486+
private boolean internal;
487+
private List<JBField.JBFieldExport> fields;
488+
489+
public String getId() {
490+
return id;
491+
}
492+
493+
public void setId(String id) {
494+
this.id = id;
495+
}
496+
497+
public String getName() {
498+
return name;
499+
}
500+
501+
public void setName(String name) {
502+
this.name = name;
503+
}
504+
505+
public JBClazzAcl getAcl() {
506+
return acl;
507+
}
508+
509+
public void setAcl(JBClazzAcl acl) {
510+
this.acl = acl;
511+
}
512+
513+
public boolean isInternal() {
514+
return internal;
515+
}
516+
517+
public void setInternal(boolean internal) {
518+
this.internal = internal;
519+
}
520+
521+
public List<JBField.JBFieldExport> getFields() {
522+
return fields;
523+
}
524+
525+
public void setFields(List<JBField.JBFieldExport> fields) {
526+
this.fields = fields;
527+
}
528+
}
529+
390530
}

src/main/java/com/javabaas/javasdk/JBConfig.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,17 @@ public static void initMaster(String remote, String appId, String masterKey) {
2525
}
2626

2727
public static void useApp(JBApp app) {
28-
JBConfig.getInstance().initConfig(null, app.getId(), app.getKey(), app.getMasterKey(), null);
28+
if (app == null) {
29+
JBConfig.getInstance().removeAppConfig();
30+
} else {
31+
JBConfig.getInstance().initConfig(null, app.getId(), app.getKey(), app.getMasterKey(), null);
32+
}
33+
}
34+
35+
public void removeAppConfig() {
36+
this.masterKey = null;
37+
this.key = null;
38+
this.appId = null;
2939
}
3040

3141
private JBConfig() {}

0 commit comments

Comments
 (0)