瀏覽代碼

begin luticate utils and auth

tags/0.0.5
Robin Thoni 9 年之前
父節點
當前提交
d38c43d136
共有 32 個檔案被更改,包括 885 行新增5 行删除
  1. 1
    1
      app/build.gradle
  2. 18
    3
      app/src/main/java/com/rthoni/camotion/MainActivity.java
  3. 1
    0
      luticateauth/.gitignore
  4. 26
    0
      luticateauth/build.gradle
  5. 17
    0
      luticateauth/proguard-rules.pro
  6. 13
    0
      luticateauth/src/androidTest/java/com/luticate/auth/ApplicationTest.java
  7. 11
    0
      luticateauth/src/main/AndroidManifest.xml
  8. 18
    0
      luticateauth/src/main/java/com/luticate/auth/business/LuticatePermissionsBusiness.java
  9. 45
    0
      luticateauth/src/main/java/com/luticate/auth/business/LuticateUsersBusiness.java
  10. 18
    0
      luticateauth/src/main/java/com/luticate/auth/dataaccess/LuticatePermissionsDataAccess.java
  11. 28
    0
      luticateauth/src/main/java/com/luticate/auth/dataaccess/LuticateUsersDataAccess.java
  12. 63
    0
      luticateauth/src/main/java/com/luticate/auth/dbo/LuFullLoginDbo.java
  13. 113
    0
      luticateauth/src/main/java/com/luticate/auth/dbo/LuLoginDbo.java
  14. 54
    0
      luticateauth/src/main/java/com/luticate/auth/dbo/LuPermissionDbo.java
  15. 3
    0
      luticateauth/src/main/res/values/strings.xml
  16. 15
    0
      luticateauth/src/test/java/com/luticate/auth/ExampleUnitTest.java
  17. 1
    0
      luticateutils/.gitignore
  18. 26
    0
      luticateutils/build.gradle
  19. 17
    0
      luticateutils/proguard-rules.pro
  20. 13
    0
      luticateutils/src/androidTest/java/com/rthoni/utils/ApplicationTest.java
  21. 11
    0
      luticateutils/src/main/AndroidManifest.xml
  22. 7
    0
      luticateutils/src/main/java/com/luticate/utils/business/LuBusinessManager.java
  23. 127
    0
      luticateutils/src/main/java/com/luticate/utils/business/LuPromise.java
  24. 79
    0
      luticateutils/src/main/java/com/luticate/utils/business/LuRequest.java
  25. 7
    0
      luticateutils/src/main/java/com/luticate/utils/dataaccess/LuDataAccess.java
  26. 22
    0
      luticateutils/src/main/java/com/luticate/utils/dbo/LuBoolDbo.java
  27. 26
    0
      luticateutils/src/main/java/com/luticate/utils/dbo/LuDbo.java
  28. 65
    0
      luticateutils/src/main/java/com/luticate/utils/dbo/LuMultipleDbo.java
  29. 21
    0
      luticateutils/src/main/java/com/luticate/utils/dbo/LuVoidDbo.java
  30. 3
    0
      luticateutils/src/main/res/values/strings.xml
  31. 15
    0
      luticateutils/src/test/java/com/luticate/lutcateutils/ExampleUnitTest.java
  32. 1
    1
      settings.gradle

+ 1
- 1
app/build.gradle 查看文件

@@ -24,5 +24,5 @@ dependencies {
24 24
     testCompile 'junit:junit:4.12'
25 25
     compile 'com.android.support:appcompat-v7:22.2.1'
26 26
     compile 'com.android.support:design:22.2.1'
27
-    compile 'com.loopj.android:android-async-http:1.4.9'
27
+    compile project(':luticateauth')
28 28
 }

+ 18
- 3
app/src/main/java/com/rthoni/camotion/MainActivity.java 查看文件

@@ -2,7 +2,7 @@ package com.rthoni.camotion;
2 2
 
3 3
 import android.os.Bundle;
4 4
 import android.support.design.widget.FloatingActionButton;
5
-import android.support.design.widget.Snackbar;
5
+import android.util.Log;
6 6
 import android.view.View;
7 7
 import android.support.design.widget.NavigationView;
8 8
 import android.support.v4.view.GravityCompat;
@@ -13,6 +13,11 @@ import android.support.v7.widget.Toolbar;
13 13
 import android.view.Menu;
14 14
 import android.view.MenuItem;
15 15
 
16
+import com.luticate.auth.business.LuticateUsersBusiness;
17
+import com.luticate.auth.dbo.LuFullLoginDbo;
18
+import com.luticate.auth.dbo.LuLoginDbo;
19
+import com.luticate.utils.business.LuPromise;
20
+
16 21
 public class MainActivity extends AppCompatActivity
17 22
         implements NavigationView.OnNavigationItemSelectedListener {
18 23
 
@@ -27,8 +32,18 @@ public class MainActivity extends AppCompatActivity
27 32
         fab.setOnClickListener(new View.OnClickListener() {
28 33
             @Override
29 34
             public void onClick(View view) {
30
-                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
31
-                        .setAction("Action", null).show();
35
+                LuticateUsersBusiness.loginFull("http://camotion.gigi.rthoni.com", "user", "test42")
36
+                        .then(new LuPromise.LuConsumer<LuFullLoginDbo>() {
37
+                            @Override
38
+                            public void execute(LuFullLoginDbo data) {
39
+                                Log.d("Login", data.toString());
40
+                            }
41
+                        }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
42
+                            @Override
43
+                            public void execute(LuPromise.LuPromiseError error) {
44
+                                Log.e("Login", error.toString());
45
+                            }
46
+                        });
32 47
             }
33 48
         });
34 49
 

+ 1
- 0
luticateauth/.gitignore 查看文件

@@ -0,0 +1 @@
1
+/build

+ 26
- 0
luticateauth/build.gradle 查看文件

@@ -0,0 +1,26 @@
1
+apply plugin: 'com.android.library'
2
+
3
+android {
4
+    compileSdkVersion 22
5
+    buildToolsVersion "22.0.1"
6
+
7
+    defaultConfig {
8
+        minSdkVersion 14
9
+        targetSdkVersion 22
10
+        versionCode 1
11
+        versionName "1.0"
12
+    }
13
+    buildTypes {
14
+        release {
15
+            minifyEnabled false
16
+            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17
+        }
18
+    }
19
+}
20
+
21
+dependencies {
22
+    compile fileTree(dir: 'libs', include: ['*.jar'])
23
+    testCompile 'junit:junit:4.12'
24
+    compile 'com.android.support:appcompat-v7:22.2.1'
25
+    compile project(':luticateutils')
26
+}

+ 17
- 0
luticateauth/proguard-rules.pro 查看文件

@@ -0,0 +1,17 @@
1
+# Add project specific ProGuard rules here.
2
+# By default, the flags in this file are appended to flags specified
3
+# in /home/robin/Android/Sdk/tools/proguard/proguard-android.txt
4
+# You can edit the include path and order by changing the proguardFiles
5
+# directive in build.gradle.
6
+#
7
+# For more details, see
8
+#   http://developer.android.com/guide/developing/tools/proguard.html
9
+
10
+# Add any project specific keep options here:
11
+
12
+# If your project uses WebView with JS, uncomment the following
13
+# and specify the fully qualified class name to the JavaScript interface
14
+# class:
15
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16
+#   public *;
17
+#}

+ 13
- 0
luticateauth/src/androidTest/java/com/luticate/auth/ApplicationTest.java 查看文件

@@ -0,0 +1,13 @@
1
+package com.luticate.auth;
2
+
3
+import android.app.Application;
4
+import android.test.ApplicationTestCase;
5
+
6
+/**
7
+ * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8
+ */
9
+public class ApplicationTest extends ApplicationTestCase<Application> {
10
+    public ApplicationTest() {
11
+        super(Application.class);
12
+    }
13
+}

+ 11
- 0
luticateauth/src/main/AndroidManifest.xml 查看文件

@@ -0,0 +1,11 @@
1
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
+    package="com.luticate.auth">
3
+
4
+    <application
5
+        android:allowBackup="true"
6
+        android:label="@string/app_name"
7
+        android:supportsRtl="true">
8
+
9
+    </application>
10
+
11
+</manifest>

+ 18
- 0
luticateauth/src/main/java/com/luticate/auth/business/LuticatePermissionsBusiness.java 查看文件

@@ -0,0 +1,18 @@
1
+package com.luticate.auth.business;
2
+
3
+import com.luticate.auth.dataaccess.LuticatePermissionsDataAccess;
4
+import com.luticate.auth.dbo.LuPermissionDbo;
5
+import com.luticate.utils.business.LuBusinessManager;
6
+import com.luticate.utils.business.LuPromise;
7
+
8
+/**
9
+ *
10
+ * Created by robin on 11/27/15.
11
+ */
12
+public class LuticatePermissionsBusiness extends LuBusinessManager {
13
+
14
+    public static LuPromise<LuPermissionDbo.LuMultiplePermissionDbo> getAllEffectiveMe(String baseUrl)
15
+    {
16
+        return LuticatePermissionsDataAccess.getAllEffectiveMe(baseUrl);
17
+    }
18
+}

+ 45
- 0
luticateauth/src/main/java/com/luticate/auth/business/LuticateUsersBusiness.java 查看文件

@@ -0,0 +1,45 @@
1
+package com.luticate.auth.business;
2
+
3
+import com.luticate.auth.dataaccess.LuticateUsersDataAccess;
4
+import com.luticate.auth.dbo.LuFullLoginDbo;
5
+import com.luticate.auth.dbo.LuPermissionDbo;
6
+import com.luticate.utils.business.LuBusinessManager;
7
+import com.luticate.utils.business.LuPromise;
8
+import com.luticate.auth.dbo.LuLoginDbo;
9
+
10
+/**
11
+ *
12
+ * Created by robin on 11/27/15.
13
+ */
14
+public class LuticateUsersBusiness extends LuBusinessManager {
15
+
16
+    public static LuPromise<LuLoginDbo> login(String baseUrl, String username, String password)
17
+    {
18
+        return LuticateUsersDataAccess.login(baseUrl, username, password);
19
+    }
20
+
21
+    public static LuPromise<LuLoginDbo> logout(String baseUrl)
22
+    {
23
+        return LuticateUsersDataAccess.logout(baseUrl);
24
+    }
25
+
26
+    public static LuPromise<LuFullLoginDbo> loginFull(final String baseUrl, String username, String password)
27
+    {
28
+        final LuPromise<LuFullLoginDbo> promise = new LuPromise<>();
29
+        login(baseUrl, username, password).then(new LuPromise.LuConsumer<LuLoginDbo>() {
30
+            @Override
31
+            public void execute(final LuLoginDbo user) {
32
+                LuticatePermissionsBusiness.getAllEffectiveMe(baseUrl).then(new LuPromise.LuConsumer<LuPermissionDbo.LuMultiplePermissionDbo>() {
33
+                    @Override
34
+                    public void execute(LuPermissionDbo.LuMultiplePermissionDbo permissions) {
35
+                        LuFullLoginDbo dbo = new LuFullLoginDbo();
36
+                        dbo.setLoginDbo(user);
37
+                        dbo.setPermissions(permissions.getData());
38
+                        promise.resolve(dbo);
39
+                    }
40
+                }, promise.rejectConsumer());
41
+            }
42
+        }, promise.rejectConsumer());
43
+        return promise;
44
+    }
45
+}

+ 18
- 0
luticateauth/src/main/java/com/luticate/auth/dataaccess/LuticatePermissionsDataAccess.java 查看文件

@@ -0,0 +1,18 @@
1
+package com.luticate.auth.dataaccess;
2
+
3
+import com.luticate.auth.dbo.LuPermissionDbo;
4
+import com.luticate.utils.dataaccess.LuDataAccess;
5
+import com.luticate.utils.business.LuPromise;
6
+import com.luticate.utils.business.LuRequest;
7
+
8
+/**
9
+ *
10
+ * Created by robin on 11/27/15.
11
+ */
12
+public class LuticatePermissionsDataAccess extends LuDataAccess {
13
+
14
+    public static LuPromise<LuPermissionDbo.LuMultiplePermissionDbo> getAllEffectiveMe(String baseUrl)
15
+    {
16
+        return LuRequest.get(LuPermissionDbo.LuMultiplePermissionDbo.class, baseUrl + "/api/luticate/permissions/effective/me");
17
+    }
18
+}

+ 28
- 0
luticateauth/src/main/java/com/luticate/auth/dataaccess/LuticateUsersDataAccess.java 查看文件

@@ -0,0 +1,28 @@
1
+package com.luticate.auth.dataaccess;
2
+
3
+import com.luticate.auth.dbo.LuLoginDbo;
4
+import com.luticate.utils.dataaccess.LuDataAccess;
5
+import com.luticate.utils.business.LuPromise;
6
+import com.luticate.utils.business.LuRequest;
7
+
8
+import java.util.HashMap;
9
+
10
+/**
11
+ *
12
+ * Created by robin on 11/27/15.
13
+ */
14
+public class LuticateUsersDataAccess extends LuDataAccess {
15
+
16
+    public static LuPromise<LuLoginDbo> login(String baseUrl, String username, String password)
17
+    {
18
+        HashMap<String, String> array = new HashMap<>();
19
+        array.put("username", username);
20
+        array.put("password", password);
21
+        return LuRequest.post(LuLoginDbo.class, baseUrl + "/api/luticate/users/login", array);
22
+    }
23
+
24
+    public static LuPromise<LuLoginDbo> logout(String baseUrl)
25
+    {
26
+        return LuRequest.post(LuLoginDbo.class, baseUrl + "/api/luticate/users/logout");
27
+    }
28
+}

+ 63
- 0
luticateauth/src/main/java/com/luticate/auth/dbo/LuFullLoginDbo.java 查看文件

@@ -0,0 +1,63 @@
1
+package com.luticate.auth.dbo;
2
+
3
+import com.luticate.utils.dbo.LuDbo;
4
+
5
+import org.json.JSONArray;
6
+import org.json.JSONException;
7
+import org.json.JSONObject;
8
+
9
+import java.util.HashMap;
10
+import java.util.List;
11
+import java.util.Vector;
12
+
13
+/**
14
+ * Created by robin on 11/27/15.
15
+ */
16
+public class LuFullLoginDbo extends LuDbo {
17
+
18
+    private LuLoginDbo _loginDbo;
19
+
20
+    private List<LuPermissionDbo> _permissions;
21
+
22
+    public LuLoginDbo getLoginDbo() {
23
+        return _loginDbo;
24
+    }
25
+
26
+    public void setLoginDbo(LuLoginDbo loginDbo) {
27
+        _loginDbo = loginDbo;
28
+    }
29
+
30
+    public List<LuPermissionDbo> getPermissions() {
31
+        return _permissions;
32
+    }
33
+
34
+    public void setPermissions(List<LuPermissionDbo> permissions) {
35
+        _permissions = permissions;
36
+    }
37
+
38
+    @Override
39
+    public void fromJson(JSONObject json) throws JSONException {
40
+        _loginDbo = new LuLoginDbo();
41
+        _loginDbo.fromJson(json.getJSONObject("LoginDbo"));
42
+
43
+        _permissions = new Vector<>();
44
+        JSONArray array = json.getJSONArray("Permissions");
45
+        for (int i = 0; i < array.length(); ++i) {
46
+            LuPermissionDbo perm = new LuPermissionDbo();
47
+            perm.fromJson(array.getJSONObject(i));
48
+            _permissions.add(perm);
49
+        }
50
+    }
51
+
52
+    @Override
53
+    public HashMap<String, Object> toArray() {
54
+        HashMap<String, Object> array = new HashMap<>();
55
+        array.put("LoginDbo", _loginDbo.toArray());
56
+        JSONArray permissions = new JSONArray();
57
+        for (LuPermissionDbo perm : _permissions) {
58
+            permissions.put(perm.toArray());
59
+        }
60
+        array.put("Permissions", permissions);
61
+        return array;
62
+    }
63
+}

+ 113
- 0
luticateauth/src/main/java/com/luticate/auth/dbo/LuLoginDbo.java 查看文件

@@ -0,0 +1,113 @@
1
+package com.luticate.auth.dbo;
2
+
3
+import com.luticate.utils.dbo.LuDbo;
4
+
5
+import org.json.JSONException;
6
+import org.json.JSONObject;
7
+
8
+import java.util.HashMap;
9
+
10
+/**
11
+ * Created by robin on 11/27/15.
12
+ */
13
+public class LuLoginDbo extends LuDbo {
14
+
15
+    private String _username;
16
+
17
+    private String _firstname;
18
+
19
+    private String _lastname;
20
+
21
+    private String _email;
22
+
23
+    private String _token;
24
+
25
+    private int _id;
26
+
27
+    private Integer _profileId;
28
+
29
+    @Override
30
+    public HashMap<String, Object> toArray() {
31
+        HashMap<String, Object> array = new HashMap<>();
32
+        array.put("Username", _username);
33
+        array.put("Email", _email);
34
+        array.put("Firstname", _firstname);
35
+        array.put("Lastname", _lastname);
36
+        array.put("Token", _token);
37
+        array.put("Id", _id);
38
+        array.put("ProfileId", _profileId);
39
+        return array;
40
+    }
41
+
42
+    @Override
43
+    public void fromJson(JSONObject json) throws JSONException {
44
+        _username = json.getString("Username");
45
+        _email = json.getString("Email");
46
+        _firstname = json.getString("Firstname");
47
+        _lastname = json.getString("Lastname");
48
+        _token = json.getString("Token");
49
+        _id = json.getInt("Id");
50
+        try
51
+        {
52
+            _profileId = json.getInt("ProfileId");
53
+        } catch (Exception e) {
54
+
55
+        }
56
+    }
57
+
58
+    public String getUsername() {
59
+        return _username;
60
+    }
61
+
62
+    public void setUsername(String username) {
63
+        _username = username;
64
+    }
65
+
66
+    public String getFirstname() {
67
+        return _firstname;
68
+    }
69
+
70
+    public void setFirstname(String firstname) {
71
+        _firstname = firstname;
72
+    }
73
+
74
+    public String getLastname() {
75
+        return _lastname;
76
+    }
77
+
78
+    public void setLastname(String lastname) {
79
+        _lastname = lastname;
80
+    }
81
+
82
+    public String getEmail() {
83
+        return _email;
84
+    }
85
+
86
+    public void setEmail(String email) {
87
+        _email = email;
88
+    }
89
+
90
+    public String getToken() {
91
+        return _token;
92
+    }
93
+
94
+    public void setToken(String token) {
95
+        _token = token;
96
+    }
97
+
98
+    public int getId() {
99
+        return _id;
100
+    }
101
+
102
+    public void setId(int id) {
103
+        _id = id;
104
+    }
105
+
106
+    public Integer getProfileId() {
107
+        return _profileId;
108
+    }
109
+
110
+    public void setProfileId(Integer profileId) {
111
+        _profileId = profileId;
112
+    }
113
+}

+ 54
- 0
luticateauth/src/main/java/com/luticate/auth/dbo/LuPermissionDbo.java 查看文件

@@ -0,0 +1,54 @@
1
+package com.luticate.auth.dbo;
2
+
3
+import com.luticate.utils.dbo.LuDbo;
4
+import com.luticate.utils.dbo.LuMultipleDbo;
5
+
6
+import org.json.JSONException;
7
+import org.json.JSONObject;
8
+
9
+import java.util.HashMap;
10
+
11
+/**
12
+ *
13
+ * Created by robin on 11/27/15.
14
+ */
15
+public class LuPermissionDbo extends LuDbo {
16
+
17
+    public static class LuMultiplePermissionDbo extends LuMultipleDbo<LuPermissionDbo> {
18
+
19
+    }
20
+
21
+    private String _name;
22
+
23
+    private boolean _value;
24
+
25
+    public String getName() {
26
+        return _name;
27
+    }
28
+
29
+    public void setName(String name) {
30
+        _name = name;
31
+    }
32
+
33
+    public boolean isValue() {
34
+        return _value;
35
+    }
36
+
37
+    public void setValue(boolean value) {
38
+        _value = value;
39
+    }
40
+
41
+    @Override
42
+    public void fromJson(JSONObject json) throws JSONException {
43
+        _name = json.getString("Name");
44
+        _value = json.getBoolean("Value");
45
+    }
46
+
47
+    @Override
48
+    public HashMap<String, Object> toArray() {
49
+        HashMap<String, Object> array = new HashMap<>();
50
+        array.put("Name", _name);
51
+        array.put("Value", _value);
52
+        return array;
53
+    }
54
+}

+ 3
- 0
luticateauth/src/main/res/values/strings.xml 查看文件

@@ -0,0 +1,3 @@
1
+<resources>
2
+    <string name="app_name">LuticateAuth</string>
3
+</resources>

+ 15
- 0
luticateauth/src/test/java/com/luticate/auth/ExampleUnitTest.java 查看文件

@@ -0,0 +1,15 @@
1
+package com.luticate.auth;
2
+
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.*;
6
+
7
+/**
8
+ * To work on unit tests, switch the Test Artifact in the Build Variants view.
9
+ */
10
+public class ExampleUnitTest {
11
+    @Test
12
+    public void addition_isCorrect() throws Exception {
13
+        assertEquals(4, 2 + 2);
14
+    }
15
+}

+ 1
- 0
luticateutils/.gitignore 查看文件

@@ -0,0 +1 @@
1
+/build

+ 26
- 0
luticateutils/build.gradle 查看文件

@@ -0,0 +1,26 @@
1
+apply plugin: 'com.android.library'
2
+
3
+android {
4
+    compileSdkVersion 22
5
+    buildToolsVersion "22.0.1"
6
+
7
+    defaultConfig {
8
+        minSdkVersion 14
9
+        targetSdkVersion 22
10
+        versionCode 1
11
+        versionName "1.0"
12
+    }
13
+    buildTypes {
14
+        release {
15
+            minifyEnabled false
16
+            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17
+        }
18
+    }
19
+}
20
+
21
+dependencies {
22
+    compile fileTree(dir: 'libs', include: ['*.jar'])
23
+    testCompile 'junit:junit:4.12'
24
+    compile 'com.android.support:appcompat-v7:22.2.1'
25
+    compile 'com.loopj.android:android-async-http:1.4.9'
26
+}

+ 17
- 0
luticateutils/proguard-rules.pro 查看文件

@@ -0,0 +1,17 @@
1
+# Add project specific ProGuard rules here.
2
+# By default, the flags in this file are appended to flags specified
3
+# in /home/robin/Android/Sdk/tools/proguard/proguard-android.txt
4
+# You can edit the include path and order by changing the proguardFiles
5
+# directive in build.gradle.
6
+#
7
+# For more details, see
8
+#   http://developer.android.com/guide/developing/tools/proguard.html
9
+
10
+# Add any project specific keep options here:
11
+
12
+# If your project uses WebView with JS, uncomment the following
13
+# and specify the fully qualified class name to the JavaScript interface
14
+# class:
15
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16
+#   public *;
17
+#}

+ 13
- 0
luticateutils/src/androidTest/java/com/rthoni/utils/ApplicationTest.java 查看文件

@@ -0,0 +1,13 @@
1
+package com.luticate.utils;
2
+
3
+import android.app.Application;
4
+import android.test.ApplicationTestCase;
5
+
6
+/**
7
+ * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8
+ */
9
+public class ApplicationTest extends ApplicationTestCase<Application> {
10
+    public ApplicationTest() {
11
+        super(Application.class);
12
+    }
13
+}

+ 11
- 0
luticateutils/src/main/AndroidManifest.xml 查看文件

@@ -0,0 +1,11 @@
1
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
+    package="com.luticate.utils">
3
+
4
+    <application
5
+        android:allowBackup="true"
6
+        android:label="@string/app_name"
7
+        android:supportsRtl="true">
8
+
9
+    </application>
10
+
11
+</manifest>

+ 7
- 0
luticateutils/src/main/java/com/luticate/utils/business/LuBusinessManager.java 查看文件

@@ -0,0 +1,7 @@
1
+package com.luticate.utils.business;
2
+
3
+/**
4
+ * Created by robin on 11/27/15.
5
+ */
6
+public class LuBusinessManager {
7
+}

+ 127
- 0
luticateutils/src/main/java/com/luticate/utils/business/LuPromise.java 查看文件

@@ -0,0 +1,127 @@
1
+package com.luticate.utils.business;
2
+
3
+/**
4
+ *
5
+ * Created by robin on 11/27/15.
6
+ */
7
+public class LuPromise<T> {
8
+
9
+    public interface LuConsumer<T> {
10
+        void execute(T data);
11
+    }
12
+
13
+    public static class LuPromiseError {
14
+
15
+        public LuPromiseError()
16
+        {
17
+
18
+        }
19
+
20
+        public LuPromiseError(String error, int statusCode)
21
+        {
22
+            _error = error;
23
+            _statusCode = statusCode;
24
+        }
25
+
26
+        public String getError() {
27
+            return _error;
28
+        }
29
+
30
+        public void setError(String error) {
31
+            _error = error;
32
+        }
33
+
34
+        public int getStatusCode() {
35
+            return _statusCode;
36
+        }
37
+
38
+        public void setStatusCode(int statusCode) {
39
+            _statusCode = statusCode;
40
+        }
41
+
42
+        private String _error;
43
+
44
+        private int _statusCode;
45
+
46
+        @Override
47
+        public String toString() {
48
+            return _error + " (HTTP Code : " + _statusCode + ")";
49
+        }
50
+    }
51
+
52
+    public enum LuPromiseStatus {
53
+        Running,
54
+        Resolved,
55
+        Failed
56
+    }
57
+
58
+    private LuConsumer<T> _onSuccess = new LuConsumer<T>() {
59
+        @Override
60
+        public void execute(T data) {
61
+        }
62
+    };
63
+
64
+    private LuConsumer<LuPromiseError> _onFailed = new LuConsumer<LuPromiseError>() {
65
+        @Override
66
+        public void execute(LuPromiseError data) {
67
+        }
68
+    };
69
+
70
+    private LuPromiseStatus _status = LuPromiseStatus.Running;
71
+
72
+    private T _data = null;
73
+
74
+    private LuPromiseError _error = null;
75
+
76
+    public void then(LuConsumer<T> onSuccess, LuConsumer<LuPromiseError> onFailed)
77
+    {
78
+        _onFailed = onFailed;
79
+        if (_status == LuPromiseStatus.Failed) {
80
+            _onFailed.execute(_error);
81
+        }
82
+        then(onSuccess);
83
+    }
84
+
85
+    public void then(LuConsumer<T> onSuccess)
86
+    {
87
+        _onSuccess = onSuccess;
88
+        if (_status == LuPromiseStatus.Resolved) {
89
+            _onSuccess.execute(_data);
90
+        }
91
+    }
92
+
93
+    public void resolve(T data)
94
+    {
95
+        if (_status == LuPromiseStatus.Running) {
96
+            _data = data;
97
+            _error = null;
98
+            _status = LuPromiseStatus.Resolved;
99
+            _onSuccess.execute(_data);
100
+        }
101
+    }
102
+
103
+    public void reject(LuPromiseError error)
104
+    {
105
+        if (_status == LuPromiseStatus.Running) {
106
+            _data = null;
107
+            _error = error;
108
+            _status = LuPromiseStatus.Failed;
109
+            _onFailed.execute(_error);
110
+        }
111
+    }
112
+
113
+    public LuConsumer<LuPromiseError> rejectConsumer()
114
+    {
115
+        return new LuConsumer<LuPromiseError>() {
116
+            @Override
117
+            public void execute(LuPromiseError error) {
118
+                reject(error);
119
+            }
120
+        };
121
+    }
122
+
123
+    public LuPromiseStatus getStatus()
124
+    {
125
+        return _status;
126
+    }
127
+}

+ 79
- 0
luticateutils/src/main/java/com/luticate/utils/business/LuRequest.java 查看文件

@@ -0,0 +1,79 @@
1
+package com.luticate.utils.business;
2
+
3
+import com.loopj.android.http.AsyncHttpClient;
4
+import com.loopj.android.http.JsonHttpResponseHandler;
5
+import com.loopj.android.http.RequestParams;
6
+import com.luticate.utils.dbo.LuDbo;
7
+
8
+import org.json.JSONObject;
9
+
10
+import java.util.HashMap;
11
+
12
+import cz.msebera.android.httpclient.Header;
13
+
14
+/**
15
+ *
16
+ * Created by robin on 11/27/15.
17
+ */
18
+public class LuRequest {
19
+
20
+    private static AsyncHttpClient client = new AsyncHttpClient();
21
+
22
+    protected static <T extends LuDbo> JsonHttpResponseHandler getHandler(final Class<T> type, final LuPromise<T> promise)
23
+    {
24
+        return new JsonHttpResponseHandler()
25
+        {
26
+            @Override
27
+            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
28
+                try {
29
+                    T dbo = type.newInstance();
30
+                    try {
31
+                        dbo.fromJson(response.getJSONObject("Data"));
32
+                        promise.resolve(dbo);
33
+                    } catch (Exception e)
34
+                    {
35
+                        e.printStackTrace();
36
+                        promise.reject(new LuPromise.LuPromiseError("Failed to parse success server response", statusCode));
37
+                    }
38
+                } catch (Exception e) {
39
+                    e.printStackTrace();
40
+                    promise.reject(new LuPromise.LuPromiseError("Failed to initialize server response", statusCode));
41
+                }
42
+            }
43
+
44
+            @Override
45
+            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
46
+                try {
47
+                    promise.reject(new LuPromise.LuPromiseError(errorResponse.getString("Message"), statusCode));
48
+                } catch (Exception e) {
49
+                    e.printStackTrace();
50
+                    promise.reject(new LuPromise.LuPromiseError("Failed to parse error server response", statusCode));
51
+                }
52
+            }
53
+        };
54
+    }
55
+
56
+    public static <T extends LuDbo> LuPromise<T> get(Class<T> type, String url, HashMap<String, String> getParams)
57
+    {
58
+        LuPromise<T> promise = new LuPromise<>();
59
+        client.get(url, new RequestParams(getParams), getHandler(type, promise));
60
+        return promise;
61
+    }
62
+
63
+    public static <T extends LuDbo> LuPromise<T> get(Class<T> type, String url)
64
+    {
65
+        return get(type, url, new HashMap<String, String>());
66
+    }
67
+
68
+    public static <T extends LuDbo> LuPromise<T> post(Class<T> type, String url, HashMap<String, String> postParams)
69
+    {
70
+        LuPromise<T> promise = new LuPromise<>();
71
+        client.post(url, new RequestParams(postParams), getHandler(type, promise));
72
+        return promise;
73
+    }
74
+
75
+    public static <T extends LuDbo> LuPromise<T> post(Class<T> type, String url)
76
+    {
77
+        return post(type, url, new HashMap<String, String>());
78
+    }
79
+}

+ 7
- 0
luticateutils/src/main/java/com/luticate/utils/dataaccess/LuDataAccess.java 查看文件

@@ -0,0 +1,7 @@
1
+package com.luticate.utils.dataaccess;
2
+
3
+/**
4
+ * Created by robin on 11/27/15.
5
+ */
6
+public abstract class LuDataAccess {
7
+}

+ 22
- 0
luticateutils/src/main/java/com/luticate/utils/dbo/LuBoolDbo.java 查看文件

@@ -0,0 +1,22 @@
1
+package com.luticate.utils.dbo;
2
+
3
+import org.json.JSONException;
4
+import org.json.JSONObject;
5
+
6
+import java.util.HashMap;
7
+
8
+/**
9
+ * Created by robin on 11/27/15.
10
+ */
11
+public class LuBoolDbo extends LuDbo {
12
+    private boolean _bool;
13
+
14
+    @Override
15
+    public void fromJson(JSONObject json) throws JSONException {
16
+    }
17
+
18
+    @Override
19
+    public HashMap<String, Object> toArray() {
20
+        return null;
21
+    }
22
+}

+ 26
- 0
luticateutils/src/main/java/com/luticate/utils/dbo/LuDbo.java 查看文件

@@ -0,0 +1,26 @@
1
+package com.luticate.utils.dbo;
2
+
3
+import org.json.JSONException;
4
+import org.json.JSONObject;
5
+
6
+import java.util.HashMap;
7
+
8
+/**
9
+ * Created by robin on 11/27/15.
10
+ */
11
+public abstract class LuDbo {
12
+
13
+    public abstract void fromJson(JSONObject json) throws JSONException;
14
+
15
+    public abstract HashMap<String, Object> toArray();
16
+
17
+    public JSONObject toJson()
18
+    {
19
+        return new JSONObject(toArray());
20
+    }
21
+
22
+    @Override
23
+    public String toString() {
24
+        return toJson().toString();
25
+    }
26
+}

+ 65
- 0
luticateutils/src/main/java/com/luticate/utils/dbo/LuMultipleDbo.java 查看文件

@@ -0,0 +1,65 @@
1
+package com.luticate.utils.dbo;
2
+
3
+import org.json.JSONArray;
4
+import org.json.JSONException;
5
+import org.json.JSONObject;
6
+
7
+import java.lang.reflect.ParameterizedType;
8
+import java.util.HashMap;
9
+import java.util.List;
10
+import java.util.Vector;
11
+
12
+/**
13
+ *
14
+ * Created by robin on 11/27/15.
15
+ */
16
+public class LuMultipleDbo<T extends LuDbo> extends LuDbo {
17
+
18
+    public List<T> getData() {
19
+        return _data;
20
+    }
21
+
22
+    public void setData(List<T> data) {
23
+        _data = data;
24
+    }
25
+
26
+    public int getCount() {
27
+        return _count;
28
+    }
29
+
30
+    public void setCount(int count) {
31
+        _count = count;
32
+    }
33
+
34
+    private List<T> _data = new Vector<>();
35
+
36
+    private int _count;
37
+
38
+    @Override
39
+    public void fromJson(JSONObject json) throws JSONException {
40
+        _count = json.getInt("Count");
41
+        JSONArray array = json.getJSONArray("Data");
42
+        for (int i = 0; i < array.length(); ++i) {
43
+            try {
44
+                T dbo = (T)((Class)((ParameterizedType)this.getClass().
45
+                        getGenericSuperclass()).getActualTypeArguments()[0]).newInstance();
46
+                dbo.fromJson(array.getJSONObject(i));
47
+                _data.add(dbo);
48
+            } catch (Exception e) {
49
+                e.printStackTrace();
50
+            }
51
+        }
52
+    }
53
+
54
+    @Override
55
+    public HashMap<String, Object> toArray() {
56
+        HashMap<String, Object> array = new HashMap<>();
57
+        array.put("Count", _count);
58
+        Vector<HashMap<String, Object>> data = new Vector<>();
59
+        for (T value : _data) {
60
+            data.add(value.toArray());
61
+        }
62
+        array.put("Data", data);
63
+        return array;
64
+    }
65
+}

+ 21
- 0
luticateutils/src/main/java/com/luticate/utils/dbo/LuVoidDbo.java 查看文件

@@ -0,0 +1,21 @@
1
+package com.luticate.utils.dbo;
2
+
3
+import org.json.JSONException;
4
+import org.json.JSONObject;
5
+
6
+import java.util.HashMap;
7
+
8
+/**
9
+ * Created by robin on 11/27/15.
10
+ */
11
+public class LuVoidDbo extends LuDbo {
12
+    @Override
13
+    public void fromJson(JSONObject json) throws JSONException {
14
+
15
+    }
16
+
17
+    @Override
18
+    public HashMap<String, Object> toArray() {
19
+        return null;
20
+    }
21
+}

+ 3
- 0
luticateutils/src/main/res/values/strings.xml 查看文件

@@ -0,0 +1,3 @@
1
+<resources>
2
+    <string name="app_name">LutcateUtils</string>
3
+</resources>

+ 15
- 0
luticateutils/src/test/java/com/luticate/lutcateutils/ExampleUnitTest.java 查看文件

@@ -0,0 +1,15 @@
1
+package com.rthoni.utils;
2
+
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.*;
6
+
7
+/**
8
+ * To work on unit tests, switch the Test Artifact in the Build Variants view.
9
+ */
10
+public class ExampleUnitTest {
11
+    @Test
12
+    public void addition_isCorrect() throws Exception {
13
+        assertEquals(4, 2 + 2);
14
+    }
15
+}

+ 1
- 1
settings.gradle 查看文件

@@ -1 +1 @@
1
-include ':app'
1
+include ':app', ':luticateutils', ':luticateauth'

Loading…
取消
儲存