diff --git a/src/main/java/cz/zcu/kiv/eegdatabase/logic/xml/XMLTransformer.java b/src/main/java/cz/zcu/kiv/eegdatabase/logic/xml/XMLTransformer.java index 580bbea3..020f233f 100644 --- a/src/main/java/cz/zcu/kiv/eegdatabase/logic/xml/XMLTransformer.java +++ b/src/main/java/cz/zcu/kiv/eegdatabase/logic/xml/XMLTransformer.java @@ -26,29 +26,43 @@ */ package cz.zcu.kiv.eegdatabase.logic.xml; -import cz.zcu.kiv.eegdatabase.data.pojo.*; -import cz.zcu.kiv.eegdatabase.data.xmlObjects.*; -import cz.zcu.kiv.eegdatabase.logic.controller.experiment.MetadataCommand; -import cz.zcu.kiv.eegdatabase.wui.components.utils.ResourceUtils; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import com.ibm.icu.util.Calendar; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBElement; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; - import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.sql.Timestamp; -import java.util.Date; import java.util.List; import java.util.Set; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import cz.zcu.kiv.eegdatabase.data.pojo.DataFile; +import cz.zcu.kiv.eegdatabase.data.pojo.Experiment; +import cz.zcu.kiv.eegdatabase.data.pojo.ExperimentOptParamVal; +import cz.zcu.kiv.eegdatabase.data.pojo.FileMetadataParamVal; +import cz.zcu.kiv.eegdatabase.data.pojo.Hardware; +import cz.zcu.kiv.eegdatabase.data.pojo.Person; +import cz.zcu.kiv.eegdatabase.data.pojo.PersonOptParamVal; +import cz.zcu.kiv.eegdatabase.data.pojo.Scenario; +import cz.zcu.kiv.eegdatabase.data.xmlObjects.DataType; +import cz.zcu.kiv.eegdatabase.data.xmlObjects.FileMetadataType; +import cz.zcu.kiv.eegdatabase.data.xmlObjects.HardwareType; +import cz.zcu.kiv.eegdatabase.data.xmlObjects.MeasurationAddParam; +import cz.zcu.kiv.eegdatabase.data.xmlObjects.MeasurationType; +import cz.zcu.kiv.eegdatabase.data.xmlObjects.ObjectFactory; +import cz.zcu.kiv.eegdatabase.data.xmlObjects.PersonAddParam; +import cz.zcu.kiv.eegdatabase.data.xmlObjects.PersonType; +import cz.zcu.kiv.eegdatabase.data.xmlObjects.ScenarioType; +import cz.zcu.kiv.eegdatabase.data.xmlObjects.WeatherType; +import cz.zcu.kiv.eegdatabase.logic.controller.experiment.MetadataCommand; +import cz.zcu.kiv.eegdatabase.wui.components.utils.DateUtils; +import cz.zcu.kiv.eegdatabase.wui.components.utils.ResourceUtils; + /** * * @author Jan Štěbeták @@ -174,20 +188,7 @@ protected void writePerson(List perType, Person per, if (mc.isBirth()) { if (per.getDateOfBirth() != null && scenarioStartTime != null) { - Calendar toTime = Calendar.getInstance(); - toTime.setTimeInMillis(scenarioStartTime.getTime()); - - Calendar birthDate = Calendar.getInstance(); - birthDate.setTimeInMillis(per.getDateOfBirth().getTime()); - - int years = toTime.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR); - int currMonth = toTime.get(Calendar.MONTH) + 1; - int birthMonth = birthDate.get(Calendar.MONTH) + 1; - int months = currMonth - birthMonth; - - if (months < 0) - years--; - + int years = DateUtils.getPersonAge(per, scenarioStartTime); pert.setAge(Integer.toString(years)); } diff --git a/src/main/java/cz/zcu/kiv/eegdatabase/wui/components/utils/DateUtils.java b/src/main/java/cz/zcu/kiv/eegdatabase/wui/components/utils/DateUtils.java new file mode 100644 index 00000000..e6b1ef66 --- /dev/null +++ b/src/main/java/cz/zcu/kiv/eegdatabase/wui/components/utils/DateUtils.java @@ -0,0 +1,27 @@ +package cz.zcu.kiv.eegdatabase.wui.components.utils; + +import java.sql.Timestamp; + +import com.ibm.icu.util.Calendar; + +import cz.zcu.kiv.eegdatabase.data.pojo.Person; + +public class DateUtils { + + public static int getPersonAge(Person per, Timestamp scenarioStartTime) { + Calendar toTime = Calendar.getInstance(); + toTime.setTimeInMillis(scenarioStartTime.getTime()); + + Calendar birthDate = Calendar.getInstance(); + birthDate.setTimeInMillis(per.getDateOfBirth().getTime()); + + int years = toTime.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR); + int currMonth = toTime.get(Calendar.MONTH) + 1; + int birthMonth = birthDate.get(Calendar.MONTH) + 1; + int months = currMonth - birthMonth; + + if (months < 0) + years--; + return years; + } +} diff --git a/src/main/java/cz/zcu/kiv/eegdatabase/wui/components/utils/StringUtils.java b/src/main/java/cz/zcu/kiv/eegdatabase/wui/components/utils/StringUtils.java index 106645df..59604b21 100644 --- a/src/main/java/cz/zcu/kiv/eegdatabase/wui/components/utils/StringUtils.java +++ b/src/main/java/cz/zcu/kiv/eegdatabase/wui/components/utils/StringUtils.java @@ -22,6 +22,12 @@ ******************************************************************************/ package cz.zcu.kiv.eegdatabase.wui.components.utils; +import java.sql.Timestamp; + +import com.ibm.icu.util.Calendar; + +import cz.zcu.kiv.eegdatabase.data.pojo.Person; + /** * Utilities class for string. * @@ -52,4 +58,5 @@ public static String randomString(int min, int max) public static String getCaptchaString(){ return randomString(3, 8); } + } diff --git a/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/ExperimentToODMLMapper.java b/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/ExperimentToODMLMapper.java new file mode 100644 index 00000000..cb96a1bf --- /dev/null +++ b/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/ExperimentToODMLMapper.java @@ -0,0 +1,446 @@ +package cz.zcu.kiv.eegdatabase.wui.core.experiments.metadata; + +import odml.core.Property; +import odml.core.Section; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import cz.zcu.kiv.eegdatabase.data.pojo.Artifact; +import cz.zcu.kiv.eegdatabase.data.pojo.ArtifactRemoveMethod; +import cz.zcu.kiv.eegdatabase.data.pojo.Digitization; +import cz.zcu.kiv.eegdatabase.data.pojo.Disease; +import cz.zcu.kiv.eegdatabase.data.pojo.Experiment; +import cz.zcu.kiv.eegdatabase.data.pojo.ExperimentOptParamVal; +import cz.zcu.kiv.eegdatabase.data.pojo.Hardware; +import cz.zcu.kiv.eegdatabase.data.pojo.Person; +import cz.zcu.kiv.eegdatabase.data.pojo.Pharmaceutical; +import cz.zcu.kiv.eegdatabase.data.pojo.ProjectType; +import cz.zcu.kiv.eegdatabase.data.pojo.Software; +import cz.zcu.kiv.eegdatabase.data.pojo.Weather; +import cz.zcu.kiv.eegdatabase.wui.core.UserRole; + +public class ExperimentToODMLMapper { + + protected static Log log = LogFactory.getLog(ExperimentToODMLMapper.class); + + public static Section convertExperimentToSection(Experiment exp) { + + Section root = new Section(); + Section experiment = null; + try { + + experiment = new Section(); + experiment.setName("Experiment"); + experiment.setType("experiment"); + root.add(experiment); + + Property prop = new Property("private-experiment", exp.isPrivateExperiment()); + experiment.add(prop); + + Property prop1 = new Property("start-time", exp.getStartTime()); + experiment.add(prop1); + + Property prop4 = new Property("end-time", exp.getEndTime()); + experiment.add(prop4); + + Property prop2 = new Property("temperature", exp.getTemperature()); + experiment.add(prop2); + + Property prop3 = new Property("environment-note", exp.getEnvironmentNote()); + experiment.add(prop3); + + if (exp.getResearchGroup() != null) { + Property prop5 = new Property("research-group", exp.getResearchGroup().getTitle()); + experiment.add(prop5); + } + + if (exp.getScenario() != null) { + Property prop6 = new Property("scenario-title", exp.getScenario().getTitle()); + experiment.add(prop6); + } + + if (exp.getPersonBySubjectPersonId() != null) { + experiment.add(convertPersonToSection(exp.getPersonBySubjectPersonId(), true)); + } + + if (exp.getWeather() != null) { + experiment.add(convertWeatherToSection(exp.getWeather())); + } + + if (exp.getArtifact() != null) { + experiment.add(convertArtifactToSection(exp.getArtifact())); + } + + if (exp.getDigitization() != null) { + experiment.add(convertDigitizationToSection(exp.getDigitization())); + } + + if (exp.getHardwares().size() != 0) { + + Section hardwares = new Section(); + hardwares.setName("Hardwares"); + hardwares.setType("collection"); + experiment.add(hardwares); + + for (Hardware hw : exp.getHardwares()) { + hardwares.add(convertHardwareToSection(hw)); + } + + } + + if (exp.getSoftwares() != null && exp.getSoftwares().size() != 0) { + + Section softwares = new Section(); + softwares.setName("Softwares"); + softwares.setType("collection"); + experiment.add(softwares); + + for (Software sw : exp.getSoftwares()) { + softwares.add(convertSoftwareToSection(sw)); + } + + } + + if (exp.getPharmaceuticals().size() != 0) { + + Section pharmaceuticals = new Section(); + pharmaceuticals.setName("Pharmaceuticals"); + pharmaceuticals.setType("collection"); + experiment.add(pharmaceuticals); + + for (Pharmaceutical ph : exp.getPharmaceuticals()) { + pharmaceuticals.add(convertPharmaceuticalToSection(ph)); + } + + } + + if (exp.getDiseases().size() != 0) { + + Section diseases = new Section(); + diseases.setName("Diseases"); + diseases.setType("collection"); + experiment.add(diseases); + + for (Disease tmp : exp.getDiseases()) { + diseases.add(convertDiseaseToSection(tmp)); + } + + } + + if (exp.getProjectTypes().size() != 0) { + + Section projectTypes = new Section(); + projectTypes.setName("ProjectTypes"); + projectTypes.setType("collection"); + experiment.add(projectTypes); + + for (ProjectType tmp : exp.getProjectTypes()) { + projectTypes.add(convertProjectTypeToSection(tmp)); + } + + } + + if (exp.getArtifactRemoveMethods().size() != 0) { + + Section artifactRemoveMethod = new Section(); + artifactRemoveMethod.setName("ArtifactRemoveMethods"); + artifactRemoveMethod.setType("collection"); + experiment.add(artifactRemoveMethod); + + for (ArtifactRemoveMethod tmp : exp.getArtifactRemoveMethods()) { + artifactRemoveMethod.add(convertArtifactRemoveMethodToSection(tmp)); + } + + } + + if (exp.getExperimentOptParamVals().size() != 0) { + + Section experimentOptParameters = new Section(); + experimentOptParameters.setName("ExperimentOptParameters"); + experimentOptParameters.setType("collection"); + experiment.add(experimentOptParameters); + + for (ExperimentOptParamVal tmp : exp.getExperimentOptParamVals()) { + experimentOptParameters.add(convertExperimentOptParamValToSection(tmp)); + } + + } + + if (exp.getPersons().size() != 0) { + + Section experimentators = new Section(); + experimentators.setName("Experimentators"); + experimentators.setType("collection"); + experiment.add(experimentators); + + for (Person tmp : exp.getPersons()) { + experimentators.add(convertPersonToSection(tmp, false)); + } + + } + + } catch (Exception e) { + log.error(e.getMessage(), e); + } + return experiment; + } + + public static Section convertHardwareToSection(Hardware hw) { + + Section hardware = new Section(); + hardware.setType("hardware"); + hardware.setName("Hardware"); + + try { + + Property prop1 = new Property("title", hw.getTitle()); + hardware.add(prop1); + + Property prop2 = new Property("type", hw.getType()); + hardware.add(prop2); + + Property prop3 = new Property("description", hw.getDescription()); + hardware.add(prop3); + + } catch (Exception e) { + log.error(e.getMessage(), e); + } + + return hardware; + } + + public static Section convertSoftwareToSection(Software sw) { + + Section software = new Section(); + software.setType("software"); + software.setName("Software"); + + try { + + Property prop1 = new Property("title", sw.getTitle()); + software.add(prop1); + + Property prop2 = new Property("description", sw.getDescription()); + software.add(prop2); + + } catch (Exception e) { + log.error(e.getMessage(), e); + } + + return software; + } + + public static Section convertPharmaceuticalToSection(Pharmaceutical ph) { + + Section pharmaceutical = new Section(); + pharmaceutical.setType("pharmaceutical"); + pharmaceutical.setName("Pharmaceutical"); + + try { + + Property prop1 = new Property("title", ph.getTitle()); + pharmaceutical.add(prop1); + + Property prop2 = new Property("description", ph.getDescription()); + pharmaceutical.add(prop2); + + } catch (Exception e) { + log.error(e.getMessage(), e); + } + + return pharmaceutical; + } + + public static Section convertDiseaseToSection(Disease ds) { + + Section disease = new Section(); + disease.setType("disease"); + disease.setName("Disease"); + + try { + + Property prop1 = new Property("title", ds.getTitle()); + disease.add(prop1); + + Property prop2 = new Property("description", ds.getDescription()); + disease.add(prop2); + + } catch (Exception e) { + log.error(e.getMessage(), e); + } + + return disease; + } + + public static Section convertDigitizationToSection(Digitization dg) { + + Section digitization = new Section(); + digitization.setType("digitization"); + digitization.setName("Digitization"); + + try { + + Property prop1 = new Property("filter", dg.getFilter()); + digitization.add(prop1); + + Property prop2 = new Property("gain", dg.getGain()); + digitization.add(prop2); + + Property prop3 = new Property("sampling-rate", dg.getSamplingRate()); + digitization.add(prop3); + + } catch (Exception e) { + log.error(e.getMessage(), e); + } + + return digitization; + } + + public static Section convertArtifactToSection(Artifact ar) { + + Section artifact = new Section(); + artifact.setType("artifact"); + artifact.setName("Artifact"); + + try { + + Property prop1 = new Property("compensation", ar.getCompensation()); + artifact.add(prop1); + + Property prop2 = new Property("reject-condition", ar.getRejectCondition()); + artifact.add(prop2); + + } catch (Exception e) { + log.error(e.getMessage(), e); + } + + return artifact; + } + + public static Section convertArtifactRemoveMethodToSection(ArtifactRemoveMethod arm) { + + Section artifactRemoveMethod = new Section(); + artifactRemoveMethod.setType("artifactRemoveMethod"); + artifactRemoveMethod.setName("ArtifactRemoveMethod"); + + try { + + Property prop1 = new Property("title", arm.getTitle()); + artifactRemoveMethod.add(prop1); + + Property prop2 = new Property("description", arm.getDescription()); + artifactRemoveMethod.add(prop2); + + } catch (Exception e) { + log.error(e.getMessage(), e); + } + + return artifactRemoveMethod; + } + + public static Section convertWeatherToSection(Weather wh) { + + Section weather = new Section(); + weather.setType("weather"); + weather.setName("Weather"); + + try { + + Property prop1 = new Property("title", wh.getTitle()); + weather.add(prop1); + + Property prop3 = new Property("description", wh.getDescription()); + weather.add(prop3); + + } catch (Exception e) { + log.error(e.getMessage(), e); + } + + return weather; + } + + public static Section convertProjectTypeToSection(ProjectType pt) { + + Section projectType = new Section(); + projectType.setType("projectType"); + projectType.setName("ProjectType"); + + try { + + Property prop1 = new Property("title", pt.getTitle()); + projectType.add(prop1); + + Property prop3 = new Property("description", pt.getDescription()); + projectType.add(prop3); + + } catch (Exception e) { + log.error(e.getMessage(), e); + } + + return projectType; + } + + public static Section convertExperimentOptParamValToSection(ExperimentOptParamVal eopv) { + + Section experimentOptParam = new Section(); + experimentOptParam.setType("experimentOptParam"); + experimentOptParam.setName("ExperimentOptParam"); + + try { + + Property prop1 = new Property("param-name", eopv.getExperimentOptParamDef().getParamName()); + experimentOptParam.add(prop1); + + Property prop2 = new Property("param-type", eopv.getExperimentOptParamDef().getParamDataType()); + experimentOptParam.add(prop2); + + Property prop3 = new Property("param-value", eopv.getParamValue()); + experimentOptParam.add(prop3); + + } catch (Exception e) { + log.error(e.getMessage(), e); + } + + return experimentOptParam; + } + + public static Section convertPersonToSection(Person pr, boolean subject) { + + Section person = new Section(); + person.setType("person"); + + if (subject) { + person.setName("Subject"); + } else { + person.setName("Person"); + } + + try { + + Property prop1 = new Property("username", pr.getUsername()); + person.add(prop1); + + Property prop2 = new Property("gender", pr.getGender()); + person.add(prop2); + + Property prop3 = new Property("surname", pr.getSurname()); + person.add(prop3); + + Property prop4 = new Property("givenname", pr.getGivenname()); + person.add(prop4); + + Property prop5 = new Property("birth", pr.getDateOfBirth()); + person.add(prop5); + + Property prop6 = new Property("role", UserRole.valueOf(pr.getAuthority()).name()); + person.add(prop6); + + } catch (Exception e) { + log.error(e.getMessage(), e); + } + + return person; + } + +} diff --git a/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/ODMLSectionDeserializer.java b/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/ODMLSectionDeserializer.java index 78bb7c3d..df21b246 100644 --- a/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/ODMLSectionDeserializer.java +++ b/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/ODMLSectionDeserializer.java @@ -2,6 +2,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStreamReader; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; @@ -26,6 +27,7 @@ import org.codehaus.jackson.map.JsonDeserializer; import org.w3c.dom.Document; import org.w3c.dom.Element; +import org.xml.sax.InputSource; public class ODMLSectionDeserializer extends JsonDeserializer
{ @@ -41,11 +43,14 @@ public Section deserialize(JsonParser jp, DeserializationContext ctxt) throws IO JSONObject jsonObject = new JSONObject(jsonString); String xmlString = XML.toString(jsonObject); Reader reader = new Reader(); - - ByteArrayInputStream stream = new ByteArrayInputStream(xmlString.getBytes()); + + ByteArrayInputStream stream = new ByteArrayInputStream(xmlString.getBytes("UTF-8")); // encoding is necessary + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder dbuilder = dbf.newDocumentBuilder(); - Document dom = dbuilder.parse(stream); + InputSource source = new InputSource(stream); + source.setEncoding("UTF-8"); // encoding is necessary + Document dom = dbuilder.parse(source); Element rootElement = dom.getDocumentElement(); rootElement.setAttribute("version", "1.0"); diff --git a/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/ODMLSectionSerializer.java b/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/ODMLSectionSerializer.java index 1a17f06b..54fad800 100644 --- a/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/ODMLSectionSerializer.java +++ b/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/ODMLSectionSerializer.java @@ -29,9 +29,10 @@ public void serialize(Section value, JsonGenerator jgen, SerializerProvider prov ByteArrayOutputStream stream = new ByteArrayOutputStream(); wr.write(stream, false, false); - String xmlString = stream.toString("UTF-8"); + String xmlString = stream.toString("UTF-8"); // encoding is necessary JSONObject jsonObject = XML.toJSONObject(xmlString); - String jsonString = jsonObject.toString(); +// String jsonString = jsonObject.toString(); /// XXX remove this - UTF8 encoding problem. + String jsonString = new String(jsonObject.toString().getBytes("UTF-8")); // encoding is necessary jgen.writeRawValue(jsonString); diff --git a/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/TemplateFacade.java b/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/TemplateFacade.java index 6327abc1..dfa33295 100644 --- a/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/TemplateFacade.java +++ b/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/TemplateFacade.java @@ -52,4 +52,6 @@ public interface TemplateFacade extends GenericFacade { public boolean canSaveName(String name, int personId); public List
getListOfAvailableODMLSections(); + + public void migrateSQLToES(); } diff --git a/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/TemplateFacadeImpl.java b/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/TemplateFacadeImpl.java index 505d2510..65c65e58 100644 --- a/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/TemplateFacadeImpl.java +++ b/src/main/java/cz/zcu/kiv/eegdatabase/wui/core/experiments/metadata/TemplateFacadeImpl.java @@ -1,19 +1,29 @@ package cz.zcu.kiv.eegdatabase.wui.core.experiments.metadata; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import odml.core.Reader; import odml.core.Section; +import odml.core.Writer; +import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.wicket.ajax.json.JSONException; +import org.apache.wicket.ajax.json.JSONObject; +import org.apache.wicket.ajax.json.XML; import org.springframework.beans.factory.annotation.Required; import org.springframework.core.io.ClassPathResource; +import org.springframework.transaction.annotation.Transactional; +import cz.zcu.kiv.eegdatabase.data.pojo.Experiment; import cz.zcu.kiv.eegdatabase.data.pojo.Template; +import cz.zcu.kiv.eegdatabase.wui.core.experiments.ExperimentsFacade; /** * ******************************************************************************************************************** @@ -45,11 +55,18 @@ public class TemplateFacadeImpl implements TemplateFacade { private TemplateService service; + private ExperimentsFacade facade; + @Required public void setService(TemplateService service) { this.service = service; } + @Required + public void setFacade(ExperimentsFacade facade) { + this.facade = facade; + } + @Override public List