Commit 98119b8e authored by Piotr Gawron's avatar Piotr Gawron
Browse files

color serializer implemented

parent 0ed86847
package lcsb.mapviewer.common.geometry; package lcsb.mapviewer.common.geometry;
import java.awt.Color; import java.awt.Color;
import java.util.HashMap;
import java.util.Map;
import lcsb.mapviewer.common.exception.InvalidArgumentException; import lcsb.mapviewer.common.exception.InvalidArgumentException;
...@@ -131,20 +129,6 @@ public class ColorParser { ...@@ -131,20 +129,6 @@ public class ColorParser {
} }
} }
/**
* Converts color into list of attributes.
*
* @param color
* color to convert
* @return map with list of color attributes
*/
public Map<String, Object> colorToMap(Color color) {
Map<String, Object> result = new HashMap<>();
result.put("alpha", color.getAlpha());
result.put("rgb", color.getRGB());
return result;
}
/** /**
* Transforms {@link Color} into html RGB representation: #RRGGBB * Transforms {@link Color} into html RGB representation: #RRGGBB
* *
......
package lcsb.mapviewer.modelutils.serializer;
import java.awt.Color;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
public class ColorSerializer extends JsonSerializer<Color> {
@Override
public void serialize(final Color color, final JsonGenerator gen,
final SerializerProvider serializers)
throws IOException {
gen.writeStartObject();
gen.writeNumberField("alpha", color.getAlpha());
gen.writeNumberField("rgb", color.getRGB());
gen.writeEndObject();
}
}
\ No newline at end of file
...@@ -324,7 +324,7 @@ public class OverlayRestImpl extends BaseRestImpl { ...@@ -324,7 +324,7 @@ public class OverlayRestImpl extends BaseRestImpl {
break; break;
case "color": case "color":
if (overlayEntry.getColor() != null) { if (overlayEntry.getColor() != null) {
value = colorParser.colorToMap(overlayEntry.getColor()); value = overlayEntry.getColor();
} }
break; break;
case "description": case "description":
......
...@@ -523,4 +523,9 @@ public class DataOverlayService implements IDataOverlayService { ...@@ -523,4 +523,9 @@ public class DataOverlayService implements IDataOverlayService {
return result; return result;
} }
@Override
public void add(DataOverlay overlay) {
dataOverlayDao.add(overlay);
}
} }
...@@ -162,4 +162,6 @@ public interface IDataOverlayService { ...@@ -162,4 +162,6 @@ public interface IDataOverlayService {
List<Pair<Reaction, DataOverlayEntry>> getReactionsForDataOverlay(String projectId, int overlayId) List<Pair<Reaction, DataOverlayEntry>> getReactionsForDataOverlay(String projectId, int overlayId)
throws QueryException; throws QueryException;
void add(DataOverlay overlay);
} }
package lcsb.mapviewer.web.config; package lcsb.mapviewer.web.config;
import java.awt.Color;
import java.util.Calendar; import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.Function;
...@@ -38,6 +39,7 @@ import lcsb.mapviewer.model.map.MiriamData; ...@@ -38,6 +39,7 @@ import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.reaction.Reaction; import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element; import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.modelutils.serializer.CalendarSerializer; import lcsb.mapviewer.modelutils.serializer.CalendarSerializer;
import lcsb.mapviewer.modelutils.serializer.ColorSerializer;
import lcsb.mapviewer.modelutils.serializer.model.map.CommentSerializer; import lcsb.mapviewer.modelutils.serializer.model.map.CommentSerializer;
import lcsb.mapviewer.modelutils.serializer.model.map.MiriamDataSerializer; import lcsb.mapviewer.modelutils.serializer.model.map.MiriamDataSerializer;
import lcsb.mapviewer.services.interfaces.IElementService; import lcsb.mapviewer.services.interfaces.IElementService;
...@@ -77,6 +79,7 @@ public class SpringWebConfig implements WebMvcConfigurer { ...@@ -77,6 +79,7 @@ public class SpringWebConfig implements WebMvcConfigurer {
MappingJackson2HttpMessageConverter m = (MappingJackson2HttpMessageConverter) converter; MappingJackson2HttpMessageConverter m = (MappingJackson2HttpMessageConverter) converter;
SimpleModule module = new SimpleModule(); SimpleModule module = new SimpleModule();
module.addSerializer(Calendar.class, new CalendarSerializer()); module.addSerializer(Calendar.class, new CalendarSerializer());
module.addSerializer(Color.class, new ColorSerializer());
module.addSerializer(MiriamData.class, new MiriamDataSerializer( module.addSerializer(MiriamData.class, new MiriamDataSerializer(
new Function<MiriamData, String>() { new Function<MiriamData, String>() {
@Override @Override
......
...@@ -106,6 +106,7 @@ import lcsb.mapviewer.persist.dao.map.DataOverlayDao; ...@@ -106,6 +106,7 @@ import lcsb.mapviewer.persist.dao.map.DataOverlayDao;
import lcsb.mapviewer.persist.dao.map.ModelDao; import lcsb.mapviewer.persist.dao.map.ModelDao;
import lcsb.mapviewer.persist.dao.user.ResetPasswordTokenDao; import lcsb.mapviewer.persist.dao.user.ResetPasswordTokenDao;
import lcsb.mapviewer.persist.dao.user.UserDao; import lcsb.mapviewer.persist.dao.user.UserDao;
import lcsb.mapviewer.services.interfaces.IDataOverlayService;
import lcsb.mapviewer.services.interfaces.IProjectService; import lcsb.mapviewer.services.interfaces.IProjectService;
import lcsb.mapviewer.services.interfaces.IUserService; import lcsb.mapviewer.services.interfaces.IUserService;
import lcsb.mapviewer.web.config.SpringWebConfig; import lcsb.mapviewer.web.config.SpringWebConfig;
...@@ -163,6 +164,9 @@ abstract public class ControllerIntegrationTest { ...@@ -163,6 +164,9 @@ abstract public class ControllerIntegrationTest {
@Autowired @Autowired
private DataOverlayDao dataOverlayDao; private DataOverlayDao dataOverlayDao;
@Autowired
private IDataOverlayService dataOverlayService;
@Autowired @Autowired
private ResetPasswordTokenDao resetPasswordTokenDao; private ResetPasswordTokenDao resetPasswordTokenDao;
...@@ -648,6 +652,10 @@ abstract public class ControllerIntegrationTest { ...@@ -648,6 +652,10 @@ abstract public class ControllerIntegrationTest {
return createOverlay(project, admin, "element_identifier\tvalue\n\t-1"); return createOverlay(project, admin, "element_identifier\tvalue\n\t-1");
} }
protected DataOverlay createOverlayWithColor(Project project, User admin) throws Exception {
return createOverlay(project, admin, "element_identifier\tcolor\n\t#aa00cc");
}
protected DataOverlay createOverlay(Project project, User admin, String content) throws Exception { protected DataOverlay createOverlay(Project project, User admin, String content) throws Exception {
return createOverlay(project, admin, content, DataOverlayType.GENERIC); return createOverlay(project, admin, content, DataOverlayType.GENERIC);
} }
...@@ -672,7 +680,7 @@ abstract public class ControllerIntegrationTest { ...@@ -672,7 +680,7 @@ abstract public class ControllerIntegrationTest {
headers.put(TextFileUtils.COLUMN_COUNT_PARAM, "2"); headers.put(TextFileUtils.COLUMN_COUNT_PARAM, "2");
headers.put(ZipEntryFileFactory.LAYOUT_HEADER_PARAM_TYPE, type.toString()); headers.put(ZipEntryFileFactory.LAYOUT_HEADER_PARAM_TYPE, type.toString());
overlay.addEntries(reader.readColorSchema(new ByteArrayInputStream(content.getBytes()), headers)); overlay.addEntries(reader.readColorSchema(new ByteArrayInputStream(content.getBytes()), headers));
dataOverlayDao.add(overlay); dataOverlayService.add(overlay);
return overlay; return overlay;
} }
......
...@@ -17,6 +17,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. ...@@ -17,6 +17,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -35,6 +36,7 @@ import org.springframework.mock.web.MockHttpSession; ...@@ -35,6 +36,7 @@ import org.springframework.mock.web.MockHttpSession;
import org.springframework.restdocs.payload.FieldDescriptor; import org.springframework.restdocs.payload.FieldDescriptor;
import org.springframework.restdocs.payload.JsonFieldType; import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.restdocs.payload.ResponseFieldsSnippet; import org.springframework.restdocs.payload.ResponseFieldsSnippet;
import org.springframework.restdocs.request.PathParametersSnippet;
import org.springframework.restdocs.request.RequestParametersSnippet; import org.springframework.restdocs.request.RequestParametersSnippet;
import org.springframework.restdocs.snippet.Snippet; import org.springframework.restdocs.snippet.Snippet;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
...@@ -410,7 +412,30 @@ public class OverlayControllerIntegrationTest extends ControllerIntegrationTest ...@@ -410,7 +412,30 @@ public class OverlayControllerIntegrationTest extends ControllerIntegrationTest
mockMvc.perform(request) mockMvc.perform(request)
.andDo(document("projects/project_overlays/get_reaction", .andDo(document("projects/project_overlays/get_reaction",
getOverlayMapPathParameters().and(parameterWithName("reactionId").description("reaction identifier")), getOverlayReactionPathParameters(),
listOfOverlayBioEntitiesFields()))
.andExpect(status().is2xxSuccessful())
.andReturn().getResponse().getContentAsString();
}
private PathParametersSnippet getOverlayReactionPathParameters() {
return getOverlayMapPathParameters().and(parameterWithName("reactionId").description("reaction identifier"));
}
@Test
public void testGetReactionForOverlayWithColor() throws Exception {
DataOverlay overlay = createOverlayWithColor(project, user);
MockHttpSession session = createSession(TEST_USER_LOGIN, TEST_USER_PASSWORD);
RequestBuilder request = get(
"/api/projects/{projectId}/overlays/{overlayId}/models/{mapId}/bioEntities/reactions/{reactionId}/",
TEST_PROJECT, overlay.getId(), map.getId(), reaction.getId())
.session(session);
mockMvc.perform(request)
.andDo(document("{method-name}",
getOverlayReactionPathParameters(),
listOfOverlayBioEntitiesFields())) listOfOverlayBioEntitiesFields()))
.andExpect(status().is2xxSuccessful()) .andExpect(status().is2xxSuccessful())
.andReturn().getResponse().getContentAsString(); .andReturn().getResponse().getContentAsString();
...@@ -1352,43 +1377,71 @@ public class OverlayControllerIntegrationTest extends ControllerIntegrationTest ...@@ -1352,43 +1377,71 @@ public class OverlayControllerIntegrationTest extends ControllerIntegrationTest
} }
private List<FieldDescriptor> overlayBioEntityFields() { private List<FieldDescriptor> overlayBioEntityFields() {
return Arrays.asList( List<FieldDescriptor> result = new ArrayList<>(Arrays.asList(
fieldWithPath("type") fieldWithPath("type")
.description("type of bioEntity (ALIAS/REACTION)") .description("type of bioEntity (ALIAS/REACTION)")
.type("string"), .type(JsonFieldType.STRING),
fieldWithPath("overlayContent.idObject") fieldWithPath("overlayContent.idObject")
.description("identifier of the bioEntity") .description("identifier of the bioEntity")
.type("number"), .type(JsonFieldType.STRING),
fieldWithPath("overlayContent.uniqueId") fieldWithPath("overlayContent.uniqueId")
.description("identifier of the bioEntity") .description("identifier of the bioEntity")
.type("number") .type(JsonFieldType.NUMBER)
.ignored(), .ignored(),
fieldWithPath("overlayContent.modelId") fieldWithPath("overlayContent.modelId")
.description("map identifier where bioEntity is located") .description("map identifier where bioEntity is located")
.type("number"), .type(JsonFieldType.NUMBER),
fieldWithPath("overlayContent.value") fieldWithPath("overlayContent.value")
.description("normalized value assigned to the bioEntity from overlay") .description("normalized value assigned to the bioEntity from overlay")
.type("number"), .type(JsonFieldType.NUMBER)
.optional(),
fieldWithPath("overlayContent.color") fieldWithPath("overlayContent.color")
.description("color assigned to the bioEntity from overlay") .description("color assigned to the bioEntity from overlay")
.type("string"), .optional()
.type(JsonFieldType.OBJECT),
fieldWithPath("overlayContent.description") fieldWithPath("overlayContent.description")
.description("description assigned to the bioEntity from overlay") .description("description assigned to the bioEntity from overlay")
.type("string") .type(JsonFieldType.STRING)
.optional(), .optional(),
fieldWithPath("overlayContent.geneVariations") fieldWithPath("overlayContent.geneVariations")
.description("list of gene variants assigned to the bioEntity from overlay") .description("list of gene variants assigned to the bioEntity from overlay")
.type("string") .type(JsonFieldType.ARRAY)
.optional(), .optional(),
fieldWithPath("overlayContent.type") fieldWithPath("overlayContent.type")
.description("type of data overlay") .description("type of data overlay")
.type("string") .type(JsonFieldType.STRING)
.optional() .optional()
.ignored(), .ignored(),
fieldWithPath("overlayContent.width") fieldWithPath("overlayContent.width")
.description("line width of reaction bioEntity assigned to the bioEntity from overlay") .description("line width of reaction bioEntity assigned to the bioEntity from overlay")
.type("number") .type(JsonFieldType.NUMBER)
.optional()); .optional()));
result.addAll(colorField("overlayContent.color", true, false));
return result;
}
private Collection<FieldDescriptor> colorField(String prefix, boolean ignore, boolean required) {
if (!prefix.isEmpty() && !prefix.endsWith(".")) {
prefix = prefix + ".";
}
List<FieldDescriptor> result = Arrays.asList(
fieldWithPath(prefix + "alpha")
.description("alpha value (0-255)")
.type(JsonFieldType.NUMBER),
fieldWithPath(prefix + "rgb")
.description("RED-GREEN-BLUE value as integer #RRGGBB")
.type(JsonFieldType.NUMBER));
if (ignore) {
for (FieldDescriptor fieldDescriptor : result) {
fieldDescriptor.ignored();
}
}
if (!required) {
for (FieldDescriptor fieldDescriptor : result) {
fieldDescriptor.optional();
}
}
return result;
} }
@Test @Test
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment