Skip to content
Snippets Groups Projects
Commit 76e497fd authored by Piotr Gawron's avatar Piotr Gawron
Browse files

rest API for units

parent cdf52aec
No related branches found
No related tags found
1 merge request!186Resolve "upload of sbml"
......@@ -124,4 +124,8 @@ public class SbmlUnitTypeFactor implements Serializable {
return new SbmlUnitTypeFactor(this);
}
public int getId() {
return id;
}
}
package lcsb.mapviewer.api.projects.models.units;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.services.SecurityException;
@RestController
public class UnitsController extends BaseController {
@Autowired
private UnitsRestImpl unitController;
@RequestMapping(value = "/projects/{projectId}/models/{modelId}/units/{unitId}", method = {
RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public Map<String, Object> getUnit(//
@PathVariable(value = "projectId") String projectId, //
@PathVariable(value = "modelId") String modelId, //
@PathVariable(value = "unitId") String unitId, //
@CookieValue(value = Configuration.AUTH_TOKEN) String token //
) throws QueryException, SecurityException {
return unitController.getUnit(projectId, modelId, token, unitId);
}
@RequestMapping(value = "/projects/{projectId}/models/{modelId}/units/", method = {
RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<Map<String, Object>> getUnits(//
@PathVariable(value = "projectId") String projectId, //
@PathVariable(value = "modelId") String modelId, //
@CookieValue(value = Configuration.AUTH_TOKEN) String token //
) throws QueryException, SecurityException {
return unitController.getUnits(projectId, modelId, token);
}
}
\ No newline at end of file
package lcsb.mapviewer.api.projects.models.units;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.ObjectNotFoundException;
import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.model.map.kinetics.SbmlUnit;
import lcsb.mapviewer.model.map.kinetics.SbmlUnitTypeFactor;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.services.SecurityException;
@Transactional(value = "txManager")
public class UnitsRestImpl extends BaseRestImpl {
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(UnitsRestImpl.class);
public Map<String, Object> getUnit(String projectId, String modelId, String token, String unitId)
throws SecurityException, QueryException {
List<Model> models = getModels(projectId, modelId, token);
int id = Integer.valueOf(unitId);
for (Model model : models) {
for (SbmlUnit unit : model.getUnits()) {
if (unit.getId() == id) {
return unitToMap(unit);
}
}
}
throw new ObjectNotFoundException("Unit with given id doesn't exist");
}
private Map<String, Object> unitToMap(SbmlUnit unit) {
Map<String, Object> result = new HashMap<>();
result.put("id", unit.getId());
result.put("unitId", unit.getUnitId());
result.put("name", unit.getName());
List<Map<String, Object>> factors = new ArrayList<>();
for (SbmlUnitTypeFactor factor : unit.getUnitTypeFactors()) {
factors.add(factorToMap(factor));
}
result.put("unitTypeFactors", factors);
return result;
}
private Map<String, Object> factorToMap(SbmlUnitTypeFactor factor) {
Map<String, Object> result = new HashMap<>();
result.put("id", factor.getId());
result.put("exponent", factor.getExponent());
result.put("multiplier", factor.getMultiplier());
result.put("scale", factor.getScale());
result.put("unitType", factor.getUnitType());
return result;
}
public List<Map<String, Object>> getUnits(String projectId, String modelId, String token) throws SecurityException {
List<Map<String, Object>> result = new ArrayList<>();
List<Model> models = getModels(projectId, modelId, token);
for (Model model : models) {
for (SbmlUnit unit : model.getUnits()) {
result.add(unitToMap(unit));
}
}
return result;
}
}
......@@ -23,6 +23,7 @@
<bean id="ElementsRestImpl" class="lcsb.mapviewer.api.projects.models.bioEntities.elements.ElementsRestImpl"/>
<bean id="FunctionsRestImpl" class="lcsb.mapviewer.api.projects.models.functions.FunctionsRestImpl"/>
<bean id="ParametersRestImpl" class="lcsb.mapviewer.api.projects.models.parameters.ParametersRestImpl"/>
<bean id="UnitsRestImpl" class="lcsb.mapviewer.api.projects.models.units.UnitsRestImpl"/>
<bean id="DrugRestImpl" class="lcsb.mapviewer.api.projects.drugs.DrugRestImpl"/>
<bean id="MiRnaRestImpl" class="lcsb.mapviewer.api.projects.mirnas.MiRnaRestImpl"/>
<bean id="OverlayRestImpl" class="lcsb.mapviewer.api.projects.overlays.OverlayRestImpl"/>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment