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

TRUNK-3621 #330

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,49 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.customdatatype.datatype;

import org.apache.commons.lang.StringUtils;
import org.openmrs.Concept;
import org.openmrs.api.context.Context;
import org.openmrs.customdatatype.SerializingCustomDatatype;
import org.springframework.stereotype.Component;

/**
* Datatype for Concept, represented by org.openmrs.Concept.
*
* @since 1.10
*/
@Component
public class ConceptDatatype extends SerializingCustomDatatype<Concept> {
Copy link
Member

Choose a reason for hiding this comment

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

You need a test for this class. See org.openmrs.customdatatype.datatype.DateDatatypeTest for an example.


/**
* @see org.openmrs.customdatatype.SerializingCustomDatatype#serialize(java.lang.Object)
*/
@Override
public String serialize(Concept typedValue) {
if (typedValue == null)
return null;
return typedValue.getUuid();
}

/**
* @see org.openmrs.customdatatype.SerializingCustomDatatype#deserialize(java.lang.String)
*/
@Override
public Concept deserialize(String serializedValue) {
if (StringUtils.isEmpty(serializedValue))
return null;
return Context.getConceptService().getConceptByUuid(serializedValue);
}
}
@@ -0,0 +1,97 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.web.attribute.handler;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.openmrs.Concept;
import org.openmrs.api.context.Context;
import org.openmrs.customdatatype.CustomDatatype;
import org.openmrs.customdatatype.InvalidCustomValueException;
import org.openmrs.customdatatype.datatype.ConceptDatatype;
import org.openmrs.messagesource.MessageSourceService;
import org.springframework.stereotype.Component;

/**
* Handler for the concept custom datatype
*/
@Component
public class ConceptFieldGenDatatypeHandler implements FieldGenDatatypeHandler<ConceptDatatype, Concept> {

/**
* @see org.openmrs.customdatatype.CustomDatatypeHandler#setHandlerConfiguration(java.lang.String)
*/
@Override
public void setHandlerConfiguration(String arg0) {
// not used
}

/**
* @see org.openmrs.web.attribute.handler.FieldGenDatatypeHandler#getWidgetName()
*/
@Override
public String getWidgetName() {
return "concept";
}

/**
* @see org.openmrs.web.attribute.handler.FieldGenDatatypeHandler#getValue(org.openmrs.customdatatype.CustomDatatype,
* javax.servlet.http.HttpServletRequest, java.lang.String)
*/
@Override
public Concept getValue(org.openmrs.customdatatype.datatype.ConceptDatatype datatype, HttpServletRequest request,
String formFieldName) throws InvalidCustomValueException {
String result = request.getParameter(formFieldName);
if (StringUtils.isBlank(result))
return null;
try {
return Context.getConceptService().getConceptByUuid(result);
Copy link
Member

Choose a reason for hiding this comment

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

Do other datatype handlers use UUIDs rather than internal primary keys?

Copy link
Member Author

Choose a reason for hiding this comment

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

They actually do not use UUIDs, at this point!!!

}
catch (Exception e) {
throw new InvalidCustomValueException("Invalid concept: " + result);
}
}

/**
* @see org.openmrs.web.attribute.handler.HtmlDisplayableDatatypeHandler#toHtmlSummary(org.openmrs.customdatatype.CustomDatatype,
* java.lang.String)
*/
@Override
public CustomDatatype.Summary toHtmlSummary(CustomDatatype<Concept> datatype, String valueReference) {
return new CustomDatatype.Summary(valueReference, true);
Copy link
Member

Choose a reason for hiding this comment

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

I think you should get the actual concept value, and display its name here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @djazayeri , thanks a lot for reviewing this
On this comment, i am planning to get the actual concept value using;
Concept c = new Concept();
c.getName();
but am not sure exactly which way do you intend to have me display it?

Copy link
Member

Choose a reason for hiding this comment

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

This method, toHtmlSummary, is supposed to return a value to be displayed to the user when the system has to render this datatype in a summary view. So, the value you return is going to be displayed to the user.
You should not be returning the uuid, but rather the name of the concept itself.
You'd get the concept by doing: Context.getConceptService().getConceptByUuid(valueReference)

}

/**
* @see org.openmrs.web.attribute.handler.HtmlDisplayableDatatypeHandler#toHtml(org.openmrs.customdatatype.CustomDatatype,
* java.lang.String)
*/
@Override
public String toHtml(CustomDatatype<Concept> datatype, String valueReference) {
return valueReference;
Copy link
Member

Choose a reason for hiding this comment

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

This definitely needs to return the concept's preferred name in the current locale. (Not just the uuid, like you're returning now.)

Copy link
Member Author

Choose a reason for hiding this comment

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

I intend to do this by adding these two code lines at to cater for this comment;
Concept c = new Concept();
return c.getPreferredName(new Locale(valueReference)).toString();

Copy link
Member

Choose a reason for hiding this comment

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

Same as for the above comment: you need to get the concept from the database based on the datatype's value (which is a concept uuid).
The locale should be Context.getLocale().

}

@Override
public Map<String, Object> getWidgetConfiguration() {
Copy link
Member

Choose a reason for hiding this comment

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

This method look right -- are these really the settings that you need to pass to the concept fieldgen?

Copy link
Member Author

Choose a reason for hiding this comment

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

I have modified that whole method to some thing like;
@OverRide
public Map<String, Object> getWidgetConfiguration() {
ConceptService cs = Context.getConceptService();
Map<String, Object> ret = new HashMap<String, Object>();
ret.put("isNullable", "false");
ret.put("trueLabel", cs.getConcept(""));
ret.put("falseLabel", cs.getConcept(getWidgetName()));
return ret;
}

Copy link
Member

Choose a reason for hiding this comment

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

This is not correct. This widget configuration consists of the parameters that will be passed to the "concept.jsp" fieldgen widget. You have just copied the values for the "boolean" fieldgen widget, and they aren't correct here.

Copy link
Member

Choose a reason for hiding this comment

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

Darius, is correct, replace trueLabel and falseLabel with just one property named label, and this will be the label to display alongside the input element in the ui.

MessageSourceService mss = Context.getMessageSourceService();
Map<String, Object> ret = new HashMap<String, Object>();
ret.put("isNullable", "false");
ret.put("trueLabel", mss.getMessage("general.true"));
ret.put("falseLabel", mss.getMessage("general.false"));
return ret;
}
}