Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2058 moved unit measurement definitions to backend #2905

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,22 @@ public String getFittingUnits(UnitDescription unitDescription) throws AdapterExc
return gson.toJson(unitDescriptionList);
}

public String getAllUnits(){
List<UnitDescription> unitDescriptionList = new LinkedList<>();

List<Unit> units = UnitProvider.INSTANCE.getAvailableUnits();

for (Unit unit : units) {
try {
UnitDescription unitDescriptionTmp =
new UnitDescription(unit.getResource().toString(), unit.getLabel());
unitDescriptionList.add(unitDescriptionTmp);
} catch (NullPointerException e) {
logger.error("Unit has no resource and/or Label");
}
}

return gson.toJson(unitDescriptionList);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if there is a reason why we return a serialized string instead of a List<UnitDescription>?
Then the REST API could serialize the result value automatically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, the other units endpoint is implemented like that. Would you like me to remove manual serialization for this one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this would be great!

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -55,4 +56,11 @@ public ResponseEntity<?> getFittingUnits(@RequestBody UnitDescription unitDescri
}
}

@GetMapping(path = "/units",
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> getAllUnits(){
String resultingJson = managementService.getAllUnits();
return ok(resultingJson);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { Observable } from 'rxjs';
import { map, startWith } from 'rxjs/operators';
import { UnitDescription } from '../../../../model/UnitDescription';
import { RestService } from '../../../../services/rest.service';
import { UnitProviderService } from '../../../../services/unit-provider.service';
import { EventPropertyPrimitive } from '@streampipes/platform-services';

@Component({
Expand Down Expand Up @@ -50,19 +49,23 @@ export class EditUnitTransformationComponent implements OnInit {
newUnitStateCtrl = new UntypedFormControl();
filteredUnits: Observable<UnitDescription[]>;

constructor(
private restService: RestService,
private unitProviderService: UnitProviderService,
) {
this.allUnits = this.unitProviderService
.getUnits()
.sort((a, b) => a.label.localeCompare(b.label));
this.filteredUnits = this.currentUnitStateCtrl.valueChanges.pipe(
startWith(''),
map(unit =>
unit ? this._filteredUnits(unit) : this.allUnits.slice(),
),
);
constructor(private restService: RestService) {
this.restService
.getAllUnitDescriptions()
.subscribe((unitDescriptions: UnitDescription[]) => {
unitDescriptions.sort((a, b) => a.label.localeCompare(b.label));
this.allUnits = unitDescriptions;
this.filteredUnits =
this.currentUnitStateCtrl.valueChanges.pipe(
startWith(''),
map(unit =>
unit
? this._filteredUnits(unit)
: this.allUnits.slice(),
),
);
});

this.currentUnitStateCtrl.valueChanges.subscribe(val => {
const unitResource =
val === '' ? undefined : this.findUnitByLabel(val).resource;
Expand Down
9 changes: 9 additions & 0 deletions ui/src/app/connect/services/rest.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,13 @@ export class RestService {
}),
);
}

getAllUnitDescriptions(): Observable<UnitDescription[]> {
return this.http.get(`${this.connectPath}/master/unit/units`).pipe(
map(response => {
const descriptions = response as UnitDescription[];
return descriptions;
}),
);
}
}
Loading
Loading