forked from avniproject/avni-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
avniproject#762 | Working code with MetabaseQueryBuilder and Question…
…CreationService
- Loading branch information
1 parent
910ffb5
commit de870a1
Showing
15 changed files
with
213 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
avni-server-api/src/main/java/org/avni/server/domain/metabase/CollectionInfoResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.avni.server.domain.metabase; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class CollectionInfoResponse { | ||
|
||
private String name; | ||
private String id; | ||
private boolean isPersonal; | ||
|
||
public CollectionInfoResponse() { | ||
} | ||
|
||
public CollectionInfoResponse(@JsonProperty("name") String name, | ||
@JsonProperty("id") Object id, | ||
@JsonProperty("is_personal") boolean isPersonal) { | ||
this.name = name; | ||
this.isPersonal = isPersonal; | ||
|
||
if (id instanceof Integer) { | ||
this.id = String.valueOf(id); | ||
} else { | ||
this.id = id.toString(); | ||
} | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public int getIdAsInt() { | ||
try { | ||
return Integer.parseInt(id); | ||
} catch (NumberFormatException e) { | ||
throw new RuntimeException("Failed to convert id to integer: " + id, e); | ||
} | ||
} | ||
|
||
public boolean isPersonal() { | ||
return isPersonal; | ||
} | ||
|
||
public void setPersonal(boolean personal) { | ||
isPersonal = personal; | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...ni/server/domain/metabase/Collection.java → ...ain/metabase/CreateCollectionRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
avni-server-api/src/main/java/org/avni/server/domain/metabase/Database.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
avni-server-api/src/main/java/org/avni/server/domain/metabase/DatabaseDetails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 0 additions & 37 deletions
37
avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseCollectionInfo.java
This file was deleted.
Oops, something went wrong.
22 changes: 7 additions & 15 deletions
22
avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,20 @@ | ||
package org.avni.server.domain.metabase; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
||
public class MetabaseQuery { | ||
private final Database database; | ||
private final ArrayNode joins; | ||
private final int databaseId; | ||
private final ObjectNode queryNode; | ||
|
||
public MetabaseQuery(Database database, ArrayNode joins) { | ||
this.database = database; | ||
this.joins = joins; | ||
public MetabaseQuery(int databaseId, ObjectNode queryNode) { | ||
this.databaseId = databaseId; | ||
this.queryNode = queryNode; | ||
} | ||
|
||
public int getDatabaseId() { | ||
return database.getId(); | ||
return databaseId; | ||
} | ||
|
||
|
||
public ObjectNode toJson(ObjectMapper objectMapper) { | ||
ObjectNode queryNode = objectMapper.createObjectNode(); | ||
queryNode.put("database", database.getId()); | ||
queryNode.set("joins", joins); | ||
queryNode.put("type", "query"); | ||
public ObjectNode toJson() { | ||
return queryNode; | ||
} | ||
} |
54 changes: 35 additions & 19 deletions
54
avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseQueryBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,57 @@ | ||
// to be completed | ||
package org.avni.server.domain.metabase; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
||
public class MetabaseQueryBuilder { | ||
private final Database database; | ||
private ArrayNode joins; | ||
private final ArrayNode joinsArray; | ||
private final ObjectMapper objectMapper; | ||
private ObjectNode queryNode; | ||
|
||
public MetabaseQueryBuilder(Database database, ArrayNode joins) { | ||
public MetabaseQueryBuilder(Database database, ArrayNode joinsArray, ObjectMapper objectMapper) { | ||
this.database = database; | ||
this.joins = joins; | ||
this.joinsArray = joinsArray; | ||
this.objectMapper = objectMapper; | ||
this.queryNode = objectMapper.createObjectNode(); | ||
} | ||
|
||
public MetabaseQueryBuilder forTable(TableDetails tableDetails) { | ||
// code to be added | ||
queryNode.put("source-table", tableDetails.getId()); | ||
queryNode.put("database", database.getId()); | ||
return this; | ||
} | ||
|
||
|
||
public MetabaseQueryBuilder joinWith(TableDetails joinTable, FieldDetails originField, FieldDetails destinationField) { | ||
// Build the join condition and add to the joins array | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
ArrayNode joinCondition = objectMapper.createArrayNode(); | ||
|
||
joinCondition.add(ConditionType.EQUAL.getOperator()); | ||
joinCondition.add(objectMapper.createArrayNode().add("field").add(originField.getId())); | ||
joinCondition.add(objectMapper.createArrayNode().add("field").add(destinationField.getId())); | ||
|
||
joins.add(joinCondition); | ||
public MetabaseQueryBuilder joinWith(TableDetails addressTable, FieldDetails joinField1, FieldDetails joinField2) { | ||
ObjectNode joinNode = objectMapper.createObjectNode(); | ||
joinNode.put("fields", "all"); | ||
joinNode.put("alias", addressTable.getName()); | ||
joinNode.put("source-table", addressTable.getId()); | ||
|
||
ArrayNode conditionArray = objectMapper.createArrayNode(); | ||
conditionArray.add("="); | ||
|
||
ArrayNode leftField = objectMapper.createArrayNode(); | ||
leftField.add("field"); | ||
leftField.add(joinField1.getId()); | ||
leftField.add(objectMapper.createObjectNode().put("base-type", "type/Integer")); | ||
conditionArray.add(leftField); | ||
|
||
ArrayNode rightField = objectMapper.createArrayNode(); | ||
rightField.add("field"); | ||
rightField.add(joinField2.getId()); | ||
rightField.add(objectMapper.createObjectNode().put("base-type", "type/Integer").put("join-alias", addressTable.getName())); | ||
conditionArray.add(rightField); | ||
|
||
joinNode.set("condition", conditionArray); | ||
joinsArray.add(joinNode); | ||
queryNode.set("joins", joinsArray); | ||
return this; | ||
} | ||
|
||
|
||
public MetabaseQuery build() { | ||
return new MetabaseQuery(database, joins); | ||
queryNode.put("type", "query"); | ||
return new MetabaseQuery(database.getId(), queryNode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.