Skip to content

Commit 89391b2

Browse files
authored
Merge pull request #1498 from dimagi/copy_of_credentialModel
Duplicate of credentialModel
2 parents 7e9152c + f10e493 commit 89391b2

6 files changed

Lines changed: 117 additions & 1 deletion

File tree

src/main/java/org/commcare/resources/model/ResourceTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ public void initializeResources(CommCarePlatform platform, boolean isUpgrade) th
942942
setMissingResources(missingResources);
943943
}
944944

945-
private void attemptResourceInitialization(CommCarePlatform platform, boolean isUpgrade,
945+
public void attemptResourceInitialization(CommCarePlatform platform, boolean isUpgrade,
946946
Resource r, Vector<Resource> missingResources) throws ResourceInitializationException {
947947
try {
948948
r.getInstaller().initialize(platform, isUpgrade);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.commcare.suite.model;
2+
3+
import org.javarosa.core.util.externalizable.DeserializationException;
4+
import org.javarosa.core.util.externalizable.ExtUtil;
5+
import org.javarosa.core.util.externalizable.Externalizable;
6+
import org.javarosa.core.util.externalizable.PrototypeFactory;
7+
8+
import java.io.DataInputStream;
9+
import java.io.DataOutputStream;
10+
import java.io.IOException;
11+
12+
/**
13+
* Apps use this model to convey the types of credentials it issues
14+
*/
15+
public class Credential implements Externalizable {
16+
17+
private String level;
18+
private String type;
19+
20+
/**
21+
* Serialization Only!!!
22+
*/
23+
public Credential() {
24+
}
25+
26+
public Credential(String level, String type) {
27+
this.level = level;
28+
this.type = type;
29+
}
30+
31+
@Override
32+
public void readExternal(DataInputStream in, PrototypeFactory pf)
33+
throws IOException, DeserializationException {
34+
level = ExtUtil.readString(in);
35+
type = ExtUtil.readString(in);
36+
}
37+
38+
@Override
39+
public void writeExternal(DataOutputStream out) throws IOException {
40+
ExtUtil.writeString(out, level);
41+
ExtUtil.writeString(out, type);
42+
}
43+
44+
public String getLevel() {
45+
return level;
46+
}
47+
48+
public String getType() {
49+
return type;
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return "Credential{" +
55+
"level='" + level + '\'' +
56+
", type='" + type + '\'' +
57+
'}';
58+
}
59+
}

src/main/java/org/commcare/suite/model/Profile.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class Profile implements Persistable {
4848
* were added for multiple app seating functionality
4949
*/
5050
private boolean fromOld;
51+
private Vector<Credential> credentials;
5152

5253
@SuppressWarnings("unused")
5354
public Profile() {
@@ -77,6 +78,7 @@ public Profile(int version, String authRef, String uniqueId, String displayName,
7778
properties = new Vector<>();
7879
roots = new Vector<>();
7980
dependencies = new Vector<>();
81+
credentials = new Vector<>();
8082
featureStatus = new Hashtable<>();
8183
//turn on default features
8284
featureStatus.put("users", true);
@@ -191,6 +193,14 @@ public void setDependencies(Vector<AndroidPackageDependency> dependencies) {
191193
this.dependencies = dependencies;
192194
}
193195

196+
public void setCredentials(Vector<Credential> credentials) {
197+
this.credentials = credentials;
198+
}
199+
200+
public Vector<Credential> getCredentials() {
201+
return credentials;
202+
}
203+
194204
/**
195205
* A helper method which initializes the properties specified
196206
* by this profile definition.
@@ -228,6 +238,8 @@ public void readExternal(DataInputStream in, PrototypeFactory pf)
228238
buildProfileId = ExtUtil.readString(in);
229239
dependencies = (Vector<AndroidPackageDependency>)ExtUtil.read(in,
230240
new ExtWrapList(AndroidPackageDependency.class), pf);
241+
credentials = (Vector<Credential>)ExtUtil.read(in,
242+
new ExtWrapList(Credential.class), pf);
231243
}
232244

233245
@Override
@@ -244,5 +256,6 @@ public void writeExternal(DataOutputStream out) throws IOException {
244256
ExtUtil.write(out, new ExtWrapMap(featureStatus));
245257
ExtUtil.writeString(out, buildProfileId);
246258
ExtUtil.write(out, new ExtWrapList(dependencies));
259+
ExtUtil.write(out, new ExtWrapList(credentials));
247260
}
248261
}

src/main/java/org/commcare/xml/ProfileParser.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
*/
44
package org.commcare.xml;
55

6+
import org.commcare.cases.util.StringUtils;
67
import org.commcare.resources.model.Resource;
78
import org.commcare.resources.model.ResourceTable;
89
import org.commcare.suite.model.AndroidPackageDependency;
10+
import org.commcare.suite.model.Credential;
911
import org.commcare.suite.model.Profile;
1012
import org.commcare.util.CommCarePlatform;
1113
import org.javarosa.core.reference.RootTranslator;
@@ -27,7 +29,11 @@ public class ProfileParser extends ElementParser<Profile> {
2729

2830
private static final String NAME_DEPENDENCIES = "dependencies";
2931
private static final String NAME_ANDROID_PACKAGE = "android_package";
32+
private static final String NAME_CREDENTIALS = "credentials";
33+
private static final String NAME_CREDENTIAL = "credential";
3034
private static final String ATTR_ID = "id";
35+
private static final String ATTR_CREDENTIAL_LEVEL = "level";
36+
private static final String ATTR_CREDENTIAL_TYPE = "type";
3137

3238
ResourceTable table;
3339
String resourceId;
@@ -244,13 +250,35 @@ private void parseFeatures(Profile profile) throws XmlPullParserException, IOExc
244250
} else if (tag.equals(NAME_DEPENDENCIES)) {
245251
profile.setDependencies(parseDependencies());
246252
} else if (tag.equals("sense")) {
253+
}else if (tag.equals(NAME_CREDENTIALS)) {
254+
profile.setCredentials(parseCredentials());
247255
}
248256

249257
profile.setFeatureActive(tag, isActive);
250258
//TODO: set feature activation in profile
251259
}
252260
}
253261

262+
private Vector<Credential> parseCredentials()
263+
throws InvalidStructureException, XmlPullParserException, IOException {
264+
Vector<Credential> appCredentials = new Vector<>();
265+
while (nextTagInBlock(NAME_CREDENTIALS)) {
266+
String tag = parser.getName().toLowerCase();
267+
if (tag.equals(NAME_CREDENTIAL)) {
268+
String level = parser.getAttributeValue(null, ATTR_CREDENTIAL_LEVEL);
269+
String type = parser.getAttributeValue(null, ATTR_CREDENTIAL_TYPE);
270+
if (StringUtils.isEmpty(level)) {
271+
throw new InvalidStructureException("No level defined for credential");
272+
}
273+
if (StringUtils.isEmpty(type)) {
274+
throw new InvalidStructureException("No type defined for credential");
275+
}
276+
appCredentials.add(new Credential(level, type));
277+
}
278+
}
279+
return appCredentials;
280+
}
281+
254282
private Vector<AndroidPackageDependency> parseDependencies()
255283
throws InvalidStructureException, XmlPullParserException, IOException {
256284
Vector<AndroidPackageDependency> appDependencies = new Vector<>();

src/test/java/org/commcare/backend/suite/model/test/ProfileTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.commcare.resources.model.Resource;
1010
import org.commcare.resources.model.ResourceTable;
1111
import org.commcare.suite.model.AndroidPackageDependency;
12+
import org.commcare.suite.model.Credential;
1213
import org.commcare.suite.model.Profile;
1314
import org.commcare.test.utilities.PersistableSandbox;
1415
import org.commcare.util.engine.CommCareConfigEngine;
@@ -92,6 +93,16 @@ public void testDependenciesParse() {
9293
assertEquals(Arrays.toString(expectedDependencies),Arrays.toString(p.getDependencies().toArray()));
9394
}
9495

96+
@Test
97+
public void testCredentialsParse() {
98+
Profile p = getProfile(BASIC_PROFILE_PATH);
99+
assertTrue(p.isFeatureActive("credentials"));
100+
Credential[] expectedCredentials = new Credential[2];
101+
expectedCredentials[0] = new Credential("3MON_ACTIVE", "APP_ACTIVITY");
102+
expectedCredentials[1] = new Credential("6MON_ACTIVE", "APP_ACTIVITY");
103+
assertEquals(Arrays.toString(expectedCredentials),Arrays.toString(p.getCredentials().toArray()));
104+
}
105+
95106
private void compareProfiles(Profile a, Profile b) {
96107
if(!ArrayUtilities.arraysEqual(a.getPropertySetters(), b.getPropertySetters())) {
97108
fail("Mismatch of property setters between profiles");

src/test/resources/basic_profile.ccpr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@
9191
<android_package id="org.commcare.test"/>
9292
</dependencies>
9393

94+
<credentials active="true">
95+
<credential level="3MON_ACTIVE" type="APP_ACTIVITY"></credential>
96+
<credential level="6MON_ACTIVE" type="APP_ACTIVITY"></credential>
97+
</credentials>
98+
9499
</features>
95100

96101
<suite><resource id="suite" version="102">

0 commit comments

Comments
 (0)