Skip to content

Commit

Permalink
#2058 moved unit measurement definitions to backend (#2905)
Browse files Browse the repository at this point in the history
* #2058 moved unit measurement definitions to backend

* #2058 moved unit measurement definitions to backend

* #2058 cleanup

* #2058 removed manual serialization
  • Loading branch information
IsaakKrut authored May 31, 2024
1 parent 489f382 commit 1a8c6d2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2,176 deletions.
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 List<UnitDescription> getAllUnitDescriptions(){
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 unitDescriptionList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@
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;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/v2/connect/master/unit")
public class UnitResource extends AbstractAdapterResource<UnitMasterManagement> {
Expand All @@ -55,4 +58,11 @@ public ResponseEntity<?> getFittingUnits(@RequestBody UnitDescription unitDescri
}
}

@GetMapping(path = "/units",
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<UnitDescription>> getAllUnits(){
List<UnitDescription> unitDescriptions = managementService.getAllUnitDescriptions();
return ok(unitDescriptions);
}

}
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

0 comments on commit 1a8c6d2

Please sign in to comment.