I'm trying to setup a prototype for using graphql across multiple java microservices, which requires me to join multiple graphql schema's into one.
I'm using 2 java-services and the ApolloServer with ApolloGateway; which shows the following schema in the playground:
type Client {
id: ID!
name: String
linkeduser: User
}
type Query {
user(id: ID!): User
users: [User]
client(id: ID!): Client
clients: [Client]
}
type User {
id: ID!
name: String
}
When running the simple query:
query client {
client(id: 1) {
id
name
linkeduser {
id
name
}
}
}
What I expect this to return is a client with a linkeduser; When debugging the client service gets queried, the user service gets queried, yet the response is:
{
"data": {
"client": {
"id": "1",
"name": "Bob",
"linkeduser": null
}
}
}
How do I get a linked user response in my client?
I've tried returning lists of users, a new client object with a list of linkedusers, a single user.
The example of https://github.com/apollographql/federation-jvm is the base of this code, though I've yet to see this working.
Code:
Service 1: Client
#WebServlet(loadOnStartup = 1, urlPatterns = "/graphql")
public class GraphQLService extends GraphQLHttpServlet {
#Override
protected GraphQLConfiguration getConfiguration() {
return GraphQLConfiguration.with(getGraphQLSchema()).build();
}
private static GraphQLSchema getGraphQLSchema() {
InputStream inputStream = client.GraphQLService.class
.getClassLoader().getResourceAsStream("schema.graphqls");
TypeDefinitionRegistry parse = new SchemaParser().parse(inputStream);
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
.type("Query", builder -> builder.defaultDataFetcher(GraphQLService::getClient))
.build();
return com.apollographql.federation.graphqljava.Federation.transform(parse, runtimeWiring)
.fetchEntities(env -> env.<List<Map<String, Object>>>getArgument(_Entity.argumentName)
.stream()
.map(values -> {
if ("Client".equals(values.get("__typename"))) {
final Object id = values.get("id");
if (id instanceof String) {
return getSingleClient((String) id);
}
}
return null;
})
.collect(Collectors.toList()))
.resolveEntityType(env -> {
final Object src = env.getObject();
if (src instanceof Client) {
return env.getSchema().getObjectType("Client");
}
return null;
}).build();
}
private static Object getClient(DataFetchingEnvironment environment) {
switch (environment.getFieldDefinition().getName()) {
case "client":
return getSingleClient(environment.getArgument("id"));
case "clients":
return getAllClients();
default:
return null;
}
}
//... extra code with simple getters
}
With this schema :
extend type Query {
client(id: ID!): Client
clients: [Client]
}
type Client #key(fields: "id"){
id: ID!
name: String
}
Service 2: User
#WebServlet(loadOnStartup = 1, urlPatterns = "/graphql")
public class GraphQLService extends GraphQLHttpServlet {
#Override
protected GraphQLConfiguration getConfiguration() {
return GraphQLConfiguration.with(getGraphQLSchema()).build();
}
private static GraphQLSchema getGraphQLSchema() {
InputStream inputStream = user.GraphQLService.class
.getClassLoader().getResourceAsStream("schema.graphqls");
TypeDefinitionRegistry parse = new SchemaParser().parse(inputStream);
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
.type("Query", builder -> builder.defaultDataFetcher(GraphQLService::getUser))
.build();
return com.apollographql.federation.graphqljava.Federation.transform(parse, runtimeWiring)
.fetchEntities(env -> env.<List<Map<String, Object>>>getArgument(_Entity.argumentName)
.stream()
.map(values -> {
if ("Client".equals(values.get("__typename"))) {
final Object id = values.get("id");
if (id instanceof String) {
return getSingleUser((String) id);
}
}
return null;
})
.collect(Collectors.toList()))
.resolveEntityType(env -> {
final Object src = env.getObject();
if (src instanceof User) {
return env.getSchema().getObjectType("User");
}
return null;
})
.build();
}
private static Object getUser(DataFetchingEnvironment environment) {
switch (environment.getFieldDefinition().getName()) {
case "user":
return getSingleUser(environment.getArgument("id"));
case "users":
return getAllUsers();
default:
return null;
}
}
//... extra code with simple getters
}
With this schema :
type Query #extends{
user (id: ID!): User
users: [User]
}
type User #key(fields: "id") {
id: ID!
name: String
}
type Client #key(fields: "id") #extends{
id: ID! #external
linkeduser : User
}
Version in POM.xml
<graphql.version>14.0</graphql.version>
<graphql-tools.version>5.2.4</graphql-tools.version>
<graphql-servlet.version>9.0.1</graphql-servlet.version>
<graphql-federation-support.version>0.4.0</graphql-federation-support.version>
In user service, you need to return a pojo of the type client, with a getter for a linkeduser (only the extends fields need to be present):
if ("Client".equals(values.get("__typename"))) {
final Object id = values.get("id");
if (id instanceof String) {
return new Client((String) id, getSingleUser((String) id));
}
}
Also the resolveTypeEntity needs to resolve to said client
Related
I'm developing a Flutter plugin to implements an iOS SDK and an Android SDK in Flutter. In both native SDKs, there is an object called Peripheral, which is a complexe object extending and implementing other objects. If I want to use theses Objects, do I have to implement them in Flutter too ? Or can I just create an manipulate instances of those objects from dart.
Right now, I'm trying to manipulate instances by have a PeripheralObject that calls a function in constructor that will create an instance in native Java (for Android) of a peripheral, place it in a hash map, and return it's memory adress to dart. In dart, I keep the memory adress of the Java object and when I call a function, like getName, I pass to the method channel the java memory adress and with that, I can retrieve from the map my instance of the native object, call the method and send back the answer. Is it a good way of resolving the problem or is there other better way to do so ?
Here is my dart object:
class Peripheral {
late String _objectReference;
late String _localName, _uuid;
Peripheral({required String localName, required String uuid}) {
_uuid = uuid;
_localName = localName;
_newPeripheralInstance(localName, uuid);
}
Future<void> _newPeripheralInstance(String localName, String uuid) async {
_objectReference = (await PeripheralPlatform.instance.newPeripheralInstance(localName, uuid))!;
return;
}
String get objectReference => _objectReference;
Future<String?> getModelName() async {
return PeripheralPlatform.instance.getModelName(_objectReference);
}
Future<String?> getUuid() async {
return PeripheralPlatform.instance.getUuid(_objectReference);
}
}
Here is my Dart Method Channel :
class MethodChannelPeripheral extends PeripheralPlatform {
/// The method channel used to interact with the native platform.
#visibleForTesting
final methodChannel = const MethodChannel('channel');
#override
Future<String?> newPeripheralInstance(String localName, String uuid) async {
String? instance = await methodChannel.invokeMethod<String>('Peripheral-newPeripheralInstance', <String, String>{
'localName': localName,
'uuid': uuid
});
return instance;
}
#override
Future<String?> getModelName(String peripheralReference) {
return methodChannel.invokeMethod<String>('Peripheral-getModelName', <String, String>{
'peripheralReference': peripheralReference
});
}
#override
Future<String?> getUuid(String peripheralReference) {
return methodChannel.invokeMethod<String>('Peripheral-getUuid', <String, String>{
'peripheralReference': peripheralReference
});
}
}
And here is my Android Java file :
public class PluginPeripheral {
private static Map<String, Peripheral> peripheralMap = new HashMap<>();
public static void handleMethodCall(String method, MethodCall call, MethodChannel.Result result) {
method = method.replace("Peripheral-", "");
switch (method) {
case "newPeripheralInstance":
newPeripheralInstance(call, result);
break;
case "getModelName":
getModelName(call, result);
break;
case "getUuid":
getUuid(call, result);
break;
default:
result.notImplemented();
break;
}
}
private static void newPeripheralInstance(MethodCall call, MethodChannel.Result result) {
if (call.hasArgument("uuid") && call.hasArgument("localName")) {
String uuid = call.argument("uuid");
String localName = call.argument("localName");
if (localName == null || uuid == null) {
result.error("Missing argument", "Missing argument 'uuid' or 'localName'", null);
return;
}
Peripheral peripheral = new Peripheral(localName, uuid);
peripheralMap.put(peripheral.toString(), peripheral);
result.success(peripheral.toString());
}
}
private static void getModelName(MethodCall call, MethodChannel.Result result) {
if (call.hasArgument("peripheralReference")) {
String peripheralString = call.argument("peripheralReference");
if (peripheralString == null) {
result.error("Missing argument", "Missing argument 'peripheral'", null);
return;
}
Peripheral peripheral = peripheralMap.get(peripheralString);
if (peripheral == null) {
result.error("Invalid peripheral", "Invalid peripheral", null);
return;
}
result.success(peripheral.getModelName());
} else {
result.error("Missing argument", "Missing argument 'peripheralReference'", null);
}
}
private static void getUuid(MethodCall call, MethodChannel.Result result) {
if (call.hasArgument("peripheralReference")) {
String peripheralString = call.argument("peripheralReference");
if (peripheralString == null) {
result.error("Missing argument", "Missing argument 'peripheral'", null);
return;
}
Peripheral peripheral = peripheralMap.get(peripheralString);
if (peripheral == null) {
result.error("Invalid peripheral", "Invalid peripheral", null);
return;
}
result.success(peripheral.getUuid());
} else {
result.error("Missing argument", "Missing argument 'peripheralReference'", null);
}
}
}
The alternative way is to convert an object to a map in Android and back to an object in Flutter. Something like this:
Flutter/Dart:
class Device {
String? id;
String? name;
...
Device.fromMap(Map<String, dynamic> map) {
id = map['id'];
name = map['name'];
}
}
final map = await methodChannel.invokeMethod('requestDevice');
final device = Device.fromMap(map.cast<String, dynamic>());
Android/Kotlin:
data class Device(
val id: String,
val name: String?
) {
...
fun toMap(): Map<String, Any?> {
return mapOf(
"id" to id,
"name" to name
)
}
}
override fun onMethodCall(#NonNull call: MethodCall, #NonNull result: Result) {
...
result.success(device.toMap())
...
}
I want to add a new group in ProductMetadataAutoConfiguration through a new class that extends ProductMetadataAutoConfiguration. I add the group in the CreateEntityView and it works fine. I do the same with UpdateEntityView but when I press "Save" to save the edited form the entity remains the same in the DB.
#EnableConfigurationProperties({CatalogMetadataProperties.class})
public class CustomProductMetadataConfig extends ProductMetadataAutoConfiguration {
public CustomProductMetadataConfig(CatalogMetadataProperties properties) {
super(properties);
}
#Bean
public ComponentSource characteristics() {
return registry -> {
for (DefaultProductType type : getAvailableProductTypes()) {
CreateEntityView<?> productCreate = (CreateEntityView<?>) registry
.get(String.format(ProductIds.CREATE, type.name()));
Group<?> prodGroup = productCreate.getGeneralForm()
.getGroup(ProductGroups.BASIC_INFORMATION)
.addGroup("Test Properties", getTestPropertiesGroup());
prodGroup.addField("custom", Fields.string()
.label("custom")
.order(1102));
prodGroup.addField("productSubtype", Fields.select()
.label("Product Sub Type")
.options(CharacteristicEnum.toOptions())
.order(1103));
UpdateEntityView<?> productUpdate = (UpdateEntityView<?>) registry
.get(String.format(ProductIds.UPDATE, type.name()));
Group<?> updateProdGroup = productUpdate.getGeneralForm()
.getGroup(ProductGroups.BASIC_INFORMATION);
updateProdGroup.addField("productSubtype", Fields.select()
.label("Product Sub Type")
.options(CharacteristicEnum.toOptions())
.order(1100));
updateProdGroup.addField("custom", Fields.string()
.label("custom")
.order(1101));
updateProdGroup.addGroup("Test Attributes", this.getTestPropertiesGroup())
.label("Additional Attributes")
.order(1101);
}
};
}
#NotNull
private Group<?> getTestPropertiesGroup() {
return Groups.basic()
.label("Test Properties")
.addGroup("testPropertiesSection", this.getDefaultInlineGroupTestProperties()
.order(3000))
.order((3003));
}
#NotNull
private Group<?> this.getDefaultInlineGroupTestProperties() {
return Groups.inline()
.label("testProperties")
.addField("testProperties.section1", Fields.string()
.label("section1")
.order(1000))
.addField("testProperties.section2", Fields.string()
.label("section2")
.order(1001))
.addField("testProperties.section3", Fields.string()
.label("section3")
.order(1002));
}
}
Below is the service method (JsonObjectBuilderService) that converts an object (FeatureCollectionForGeoJson) to a jsonStr. This service method is used in the Get RequestMapping to send a response to the front-end.
The FeatureCollectionForGeoJson object is a class mapped for GeoJson FeatureCollection.
The GeometryForGeoJson is another class that contains the string type with "Point" value and the array that contains the latitude and longitude for the point.
The PropertyForGeoJson class contains information/properties about that pin that will be displayed in the pop-up when the pin is clicked on on the map.
#Getter
#Setter
#ToString
#NoArgsConstructor
#AllArgsConstructor
public class FeatureForGeoJson {
private final String type = "Feature";
private GeometryForGeoJson geometry;
private PropertyForGeoJson properties;
}
#Service
public class JsonObjectBuilderService {
public String transformObjectToGeoJson(FeatureCollectionForGeoJson featureCollectionForGeoJson){
ObjectMapper Obj = new ObjectMapper();
String jsonStr = null;
try {
jsonStr = Obj.writeValueAsString(featureCollectionForGeoJson);
} catch (JsonProcessingException e) {
e.printStackTrace();
} //catch (IOException e) {
return jsonStr;
}
}
This is the GetMapping that sends the response to Angular
#GetMapping("/power-plants")
public ResponseEntity<String> getAllPowerPlants() {
try {
FeatureCollectionForGeoJson powerPlantsToFeatureCollectionForGeoJson ;
//jpa query for the database to return the information
List<PowerPlant> powerPlantList = powerPlantJpaService.findAll();
if (powerPlantList.isEmpty()) {
logger.info("The power plant list is empty.");
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
logger.info("The power plant list is populated and has been returned successfully.");
powerPlantsToFeatureCollectionForGeoJson = transformPowerPlantsToFeaturesCollection.transformPowerPlantToGeoJsonElements(powerPlantList);
String objectToGeoJson = jsonObjectBuilderService.transformObjectToGeoJson(powerPlantsToFeatureCollectionForGeoJson);
logger.info(objectToGeoJson);
return new ResponseEntity<>(objectToGeoJson, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
This is how the response looks like in the browser
This is the Angular method that fetches the response.
This is the Angular component where I call the service method that fetches the response and where I want to add the pins to the map with the pop-ups.
How do I take that response from the API (line 27 from Home.component.ts -right above- or the getAll() method from the PowerPlantService) and process it to extract the Point Geometry, to create a pin with it and extract the properties to add to a pop-up to the pin?
if you use angular you should use Observables and not Promises, also avoid to post images of code, now I can't copy/paste you code.
what you want to do is return an observable in getAll(), something like this:
// in component
this.powerPlantService.getAll$().subscribe(
res => this.featureCollection = res,
err => console.log(err)
);
// in service
getAll$(): Observable<any[]> {
return this.http.get(baseUrl).pipe(
map(data => {
// transform your data here, or remove this pipe if you don't need it
return data;
})
);
}
you can transform your features in a flat object like this:
return this.http.get(baseUrl).pipe(
map(features => {
return features.map(f => {
const pointGeometry: any = {
...f.geometry,
...f.properties
};
return pointGeometry;
});
})
);
If you want to know how the back end formats and sends the response, please check in the body of the question.
Below is the service method that performs a GET request to the back end.
export class PowerPlantService {
constructor(private http: HttpClient) { }
getAll() {
return this.http.get(baseUrl);
}
Below is the component method that subscribes to the answer and adds the elements to the map.
#Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
private latitude: number = 45.6427;
private longitude: number = 25.5887;
private map!: L.Map;
private centroid: L.LatLngExpression = [this.latitude, this.longitude];
ngOnInit(): void {
this.initMap();
}
constructor(private powerPlantService: PowerPlantService) {
}
private initMap(): void {
this.map = L.map('map', {
center: this.centroid,
zoom: 2.8
});
const tiles = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
{
minZoom: 2.8,
attribution: '© OpenStreetMap'
});
tiles.addTo(this.map);
this.powerPlantService.getAll().subscribe((data: any)=>{
console.log(data);
L.geoJSON(data).addTo(this.map)
})
I am using JPA to store data and faced two problems during implementation. I have two entities (Station and Commodity) that have many-to-many relationship with intermediate table so that I had to created the third one. When app receives message it converts its data to entites and should save but sometimes app throwing a ConstraintViolationException because there is null value at foreign key field referencing to Commodity entity.
I've tried simple approach: selecting needed commodity from database and saving it if there is no one. Then I started to use bulk searching all commodities of message and then putting it where are needed. None of them did a trick.
In my opinion the problem could be caused by multi-threading read\insert.
The second problem is that service stop running when exception is thrown. App can lost some of transactions that's not a big deal but it simply stops after rollback.
How can I resolve these conflicts?
Here is code of data handling class and diagram of entities :
#Service
#AllArgsConstructor
#Slf4j
public class ZeromqCommoditiesServiceImpl implements ZeromqCommoditesService {
private final CategoryTransactionHandler categoryHandler;
private final CommodityTransactionHandler commodityHandler;
private final EconomyTransactionHandler economyHandler;
private final StationTransactionHandler stationHandler;
private final SystemTransactionHandler systemHandler;
#Override
#Transactional(
isolation = Isolation.READ_COMMITTED,
propagation = Propagation.REQUIRES_NEW,
rollbackFor = Throwable.class)
#Modifying
public void saveData(ZeromqCommodityPayload payload) {
CommodityContent content = payload.getContent();
var station = stationHandler.createOrFindStation(content.getStationName());
var system = systemHandler.createOrFindSystem(content.getSystemName());
var commodityReferences = getMapOfCommodities(content);
station.setSystem(system);
updateEconomies(station, content);
updateProhibited(station, content, commodityReferences);
updateStationCommodities(station, content, commodityReferences);
try {
saveStation(station);
} catch (ConstraintViolationException | PersistentObjectException | DataAccessException e) {
log.error("Error saving commodity info \n" + content, e);
}
}
public void saveStation(StationEntity station) {
stationHandler.saveStation(station);
if (station.getId() != null) {
log.debug(String.format("Updated \"%s\" station info", station.getName()));
} else {
log.debug(String.format("Updated \"%s\" station info", station.getName()));
}
}
private void updateEconomies(StationEntity station, CommodityContent content) {
station.getEconomies().clear();
if (content.getEconomies() != null) {
var economies = content.getEconomies()
.stream()
.map(economy -> {
var stationEconomyEntity = economyHandler.createOrFindEconomy(economy.getName());
Double proportion = economy.getProportion();
stationEconomyEntity.setProportion(proportion != null ? proportion : 1.0);
return stationEconomyEntity;
})
.peek(economy -> economy.setStation(station))
.toList();
station.getEconomies().addAll(economies);
}
}
private void updateProhibited(
StationEntity station,
CommodityContent content,
Map<String, CommodityEntity> commodityEntityMap) {
station.getProhibited().clear();
if (content.getProhibited() != null) {
var prohibitedCommodityEntities = content.getProhibited()
.stream()
.map(prohibited -> {
String eddnName = prohibited.toLowerCase(Locale.ROOT);
CommodityEntity commodityReference = getCommodityEntity(commodityEntityMap, eddnName);
return new ProhibitedCommodityEntity(station, commodityReference);
}
)
.toList();
station.getProhibited().addAll(prohibitedCommodityEntities);
}
}
private void updateStationCommodities(
StationEntity station,
CommodityContent content,
Map<String, CommodityEntity> commodityEntityMap) {
station.getCommodities().clear();
if (content.getCommodities() != null) {
var commodities = content.getCommodities()
.stream()
.map(commodity -> {
CommodityEntity commodityReference = getCommodityEntity(
commodityEntityMap,
commodity.getEddnName());
return StationCommodityEntity.builder()
.commodity(commodityReference)
.buyPrice(commodity.getBuyPrice())
.sellPrice(commodity.getSellPrice())
.demand(commodity.getDemand())
.stock(commodity.getStock())
.station(station)
.build();
})
.toList();
station.getCommodities().addAll(commodities);
}
}
private CommodityEntity getCommodityEntity(Map<String, CommodityEntity> commodityEntityMap, String eddnName) {
return commodityEntityMap.get(eddnName);
}
private Map<String, CommodityEntity> getMapOfCommodities(#NotNull CommodityContent content) {
Set<String> commodities = content.getCommodities()
.stream()
.map(Commodity::getEddnName)
.collect(Collectors.toSet());
if (content.getProhibited() != null && content.getProhibited().size() > 0) {
commodities.addAll(content.getProhibited().
stream()
.map(item -> item.toLowerCase(Locale.ROOT))
.collect(Collectors.toSet()));
}
var commodityReferencesMap = commodityHandler.findAllByEddnName(commodities)
.stream()
.collect(Collectors.toMap(
CommodityEntity::getEddnName,
item -> item
));
commodities.forEach(commodity -> {
if (commodityReferencesMap.get(commodity.toLowerCase()) == null) {
CommodityCategoryEntity category = categoryHandler.createOrFindCategory("Unknown");
CommodityEntity newCommodity = new CommodityEntity(commodity, commodity, category);
CommodityEntity managedCommodity = commodityHandler.saveCommodity(newCommodity);
commodityReferencesMap.put(managedCommodity.getEddnName(), managedCommodity);
}
});
return commodityReferencesMap;
}
}
Thanks in advance
I am working on a JHipster project with an AngularJS front-end and a Java back-end. I am using Spring data with the MongoDb database.
I did a grouping operation on the field budgetCode. So, for each budgetCode, I succeded to have the list of all the linked taskCodes.
Here, the method aggregateAllTaskCodes which does the grouping operation:
Repository layer
public class ClarityResourceAffectationRepositoryImpl implements ClarityResourceAffectationRepositoryCustom {
#Override
public List<ClarityResourceAffectationReport> aggregateAllTaskCodes() {
Aggregation aggregation = newAggregation(
group("budgetCode").addToSet("budgetCode").as("budgetCode").addToSet("taskCode").as("taskCode"),
sort(Sort.Direction.ASC, previousOperation(),"budgetCode"));
AggregationResults groupResults = mongoTemplate.aggregate(aggregation, ClarityResourceAffectation.class,
ClarityResourceAffectationReport.class);
List<ClarityResourceAffectationReport> clarityResourceAffectationReports = groupResults.getMappedResults();
return clarityResourceAffectationReports;
}
}
Service layer
public class ClarityResourceAffectationServiceImpl implements ClarityResourceAffectationService{
#Override
public List<ClarityResourceAffectationReport> aggregateAllTaskCodes() {
log.debug("Request to aggregateByCodeBudgetForCodeTache : {}");
List<ClarityResourceAffectationReport> result = clarityResourceAffectationRepository
.aggregateAllTaskCodes();
return result;
}
}
REST API layer
public class ClarityResourceAffectationResource {
#GetMapping("/clarity-resource-affectations/list-task-codes")
#Timed
public ResponseEntity<List<ClarityResourceAffectationReport>> aggregateTabAllTaskCodes() {
log.debug("REST request to get aggregateTabAllTaskCodes : {}");
List<ClarityResourceAffectationReport> result = clarityResourceAffectationService.aggregateAllTaskCodes();
return new ResponseEntity<>(result, HttpStatus.OK);
}
}
ClarityResourceAffectation
#Document(collection = "clarity_resource_affectation")
public class ClarityResourceAffectation implements Serializable {
#Id
private String id;
#Field("budget_code")
private String budgetCode;
#Field("task_code")
private String taskCode;
public String getBudgetCode() {
return budgetCode;
}
public void setBudgetCode(String budgetCode) {
this.budgetCode = budgetCode;
}
public String getTaskCode() {
return taskCode;
}
public void setTaskCode(String taskCode) {
this.taskCode = taskCode;
}
}
ClarityResourceAffectationReport
public class ClarityResourceAffectationReport implements Serializable {
private static final long serialVersionUID = 1L;
private String budgetCode;
private String taskCode;
private String listTaskCode;
public String getBudgetCode() {
return budgetCode;
}
public void setBudgetCode(String budgetCode) {
this.budgetCode = budgetCode;
}
public String getTaskCode() {
return taskCode;
}
public void setTaskCode(String taskCode) {
this.taskCode = taskCode;
}
public String[] getListTaskCode() {
return listTaskCode;
}
public void setListTaskCode(String[] listTaskCode) {
this.listTaskCode = listTaskCode;
}
}
clarity-resource-affectation.service.js
(function() {
'use strict';
angular
.module('dashboardApp')
.factory('ClarityResourceAffectation', ClarityResourceAffectation);
ClarityResourceAffectation.$inject = ['$resource'];
function ClarityResourceAffectation ($resource) {
var resourceUrl = 'clarity/' + 'api/clarity-resource-affectations/:id';
return $resource(resourceUrl, {}, {
'query': { method: 'GET', isArray: true},
'aggregateAllTaskCodes': {
method: 'GET',
isArray: true,
url: 'clarity/api/clarity-resource-affectations/list-task-codes'
}
});
}
})();
When I call the function in the AngularJS front-end and I display that on a table, for each budgetCode, I have the list of the taskCodes in an array of one element. For example, for the budgetCode [ "P231P00"] I can have this list of taskCodes: [ "61985" , "43606" , "60671" , "43602"]
Well, I would like to have the list of the linked taskCodes, not in an array of one element but in an array of several elements like that:
[ ["61985"] , ["43606"] , ["60671"] , ["43602"] ]
What do I have to change in my code in order to do that?
Just for information, my javascript code which create the array based on the aggregate function:
clarity-resource-affectation-list-task-codes.controller.js
(function() {
'use strict';
angular
.module('dashboardApp')
.controller('ClarityResourceAffectationTableauBordNbCollaborateursController', ClarityResourceAffectationTableauBordNbCollaborateursController);
ClarityResourceAffectationTableauBordNbCollaborateursController.$inject = ['$timeout', '$scope', '$stateParams', 'DataUtils', 'ClarityResourceAffectation'];
function ClarityResourceAffectationTableauBordNbCollaborateursController ($timeout, $scope, $stateParams, DataUtils, ClarityResourceAffectation) {
var vm = this;
//Call of the function
allTaskCodes()
function allTaskCodes()
{
ClarityResourceAffectation.aggregateAllTaskCodes(function(readings) {
var dataAllTaskCodes;
dataAllTaskCodes = [];
alert(readings);
readings.forEach(function (item) {
dataAllTaskCodes.push({
label: item.budgetCode,
value: item.taskCode,
listvalue: item.listTaskCode
});
});
vm.dataAllTaskCodes = dataAllTaskCodes;
});
}
}
})();
Temporary solution:
Actually, I found a temporary solution by completing the method I created in the Service Layer:
#Override
public List<ClarityResourceAffectationReport> aggregateAllTaskCodes() {
log.debug("Request to aggregateAllTaskCodes : {}");
List<ClarityResourceAffectationReport> result = clarityResourceAffectationRepository
.aggregateAllTaskCodes();
Iterator<ClarityResourceAffectationReport> iterator = result.iterator();
while (iterator.hasNext())
{
ClarityResourceAffectationReport resAffectationReport = iterator.next();
String taskCodes = resAffectationReport.getTaskCode();
//Delete all exept letters, numbers and comma
taskCodes = taskCodes.replaceAll("[^a-zA-Z0-9,]","");
String[] listTaskCodes = taskCodes.split(",");
resAffectationReport.setListTaskCodes(listTaskCodes);
}
return result;
}
Also, I added an additional field to ClarityResourceAffectationReport which is listTaskCode. I updated the report class above. Finally, when I do an alert:
alert(readings[1].listvalue[0]), I have a result like 2630. So, I succeeded to have the first taskCode of a particular budgetCode.
I understood that what is important here is not the fact that as I told above for a budgetCode like [ "P231P00"], I must have a list like: [ "61985" , "43606" , "60671" , "43602"] or [ ["61985"] , ["43606"] , ["60671"] , ["43602"] ]. I just must have an array, not a string.
When I display alert(readings[1].listvalue), I have["2630","61297","61296","61299"] which is clearly an array because I can access each of the elements by calling alert(readings[1].listvalue[0]), alert(readings[1].listvalue[1]], etc...
I tried what you advised me
But, it is still not working. Here, my repository code:
#Override
public List<ClarityResourceAffectationReport> aggregateAllTaskCode() {
AggregationOperation project = new AggregationOperation() {
#Override
public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
return new BasicDBObject("$project", new BasicDBObject("budgetCode", "$budget_code").append("taskCode", Arrays.asList("$task_code")));
}
};
Aggregation aggregation = newAggregation(project,
group("budgetCode").addToSet("budgetCode").as("budgetCode").addToSet("taskCode").as("taskCode"),
sort(Sort.Direction.ASC, previousOperation(),"budgetCode"));
AggregationResults groupResults = mongoTemplate.aggregate(aggregation, ClarityResourceAffectation.class,
ClarityResourceAffectationReport.class);
List<ClarityResourceAffectationReport> clarityResourceAffectationReports = groupResults.getMappedResults();
log.debug("clarityResourceAffectationReports.size() => " + clarityResourceAffectationReports.size());
log.debug("aggregation.toString() => " + aggregation.toString());
return clarityResourceAffectationReports;
}
Here, you can find the logs:
clarityResourceAffectationReports.size() => 1
aggregation.toString() => {"aggregate" : "__collection__" , "pipeline" : [ { "$project" : { "budgetCode" : "$budget_code" , "taskCode" : [ "$task_code"]}} , { "$group" : { "_id" : "$budgetCode" , "budgetCode" : { "$addToSet" : "$budgetCode"} , "taskCode" : { "$addToSet" : "$taskCode"}}} , { "$sort" : { "_id" : 1 , "budgetCode" : 1}}]}
Thanks in advance
You need to use $project to change the taskCodes value into array of single value before $group.
I don't see any hook in the api to address this.
You can use AggregationOperation to create $project stage using mongodb (BasicDBObject) types.
AggregationOperation project = new AggregationOperation() {
#Override
public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
return new BasicDBObject("$project", new BasicDBObject("budgetCode", 1).append("taskCode", Arrays.asList("$taskCode")));
}
};
Something like
Aggregation aggregation = newAggregation(project,
group("budgetCode").addToSet("budgetCode").as("budgetCode").addToSet("taskCode").as("taskCode"),
sort(Sort.Direction.ASC, previousOperation(), "budgetCode"));
Using lambda
Aggregation aggregation = newAggregation(
aggregationOperationContext -> new BasicDBObject("$project", new BasicDBObject("budgetCode", 1).append("taskCode", Arrays.asList("$taskCode"))),
group("budgetCode").addToSet("budgetCode").as("budgetCode").addToSet("taskCode").as("taskCode"),
sort(Sort.Direction.ASC, previousOperation(), "budgetCode"));