use of org.openmrs.api.APIException in project openmrs-core by openmrs.
the class ServiceContext method setService.
/**
* Allow other services to be added to our service layer
*
* @param cls Interface to proxy
* @param classInstance the actual instance of the <code>cls</code> interface
*/
public void setService(Class cls, Object classInstance) {
log.debug("Setting service: " + cls);
if (cls != null && classInstance != null) {
try {
Advised cachedService = (Advised) services.get(cls);
boolean noExistingService = cachedService == null;
boolean replacingService = cachedService != null && cachedService != classInstance;
boolean serviceAdvised = classInstance instanceof Advised;
if (noExistingService || replacingService) {
Advised advisedService;
if (!serviceAdvised) {
// Adding a bare service, wrap with AOP proxy
Class[] interfaces = { cls };
ProxyFactory factory = new ProxyFactory(interfaces);
factory.setTarget(classInstance);
advisedService = (Advised) factory.getProxy(OpenmrsClassLoader.getInstance());
} else {
advisedService = (Advised) classInstance;
}
if (replacingService) {
moveAddedAOP(cachedService, advisedService);
}
services.put(cls, advisedService);
}
log.debug("Service: " + cls + " set successfully");
} catch (Exception e) {
throw new APIException("service.unable.create.proxy.factory", new Object[] { classInstance.getClass().getName() }, e);
}
}
}
use of org.openmrs.api.APIException in project openmrs-core by openmrs.
the class PersonName method getFullName.
/**
* Convenience method to get all the names of this PersonName and concatenating them together
* with spaces in between. If any part of {@link #getPrefix()}, {@link #getGivenName()},
* {@link #getMiddleName()}, etc are null, they are not included in the returned name
*
* @return all of the parts of this {@link PersonName} joined with spaces
* @should not put spaces around an empty middle name
*/
public String getFullName() {
NameTemplate nameTemplate = null;
try {
nameTemplate = NameSupport.getInstance().getDefaultLayoutTemplate();
} catch (APIException ex) {
log.warn("No name layout format set");
}
if (nameTemplate != null) {
return nameTemplate.format(this);
}
List<String> temp = new ArrayList<>();
if (StringUtils.hasText(getPrefix())) {
temp.add(getPrefix());
}
if (StringUtils.hasText(getGivenName())) {
temp.add(getGivenName());
}
if (StringUtils.hasText(getMiddleName())) {
temp.add(getMiddleName());
}
if (OpenmrsConstants.PERSON_NAME_FORMAT_LONG.equals(PersonName.getFormat())) {
if (StringUtils.hasText(getFamilyNamePrefix())) {
temp.add(getFamilyNamePrefix());
}
if (StringUtils.hasText(getFamilyName())) {
temp.add(getFamilyName());
}
if (StringUtils.hasText(getFamilyName2())) {
temp.add(getFamilyName2());
}
if (StringUtils.hasText(getFamilyNameSuffix())) {
temp.add(getFamilyNameSuffix());
}
if (StringUtils.hasText(getDegree())) {
temp.add(getDegree());
}
} else {
if (StringUtils.hasText(getFamilyName())) {
temp.add(getFamilyName());
}
}
String nameString = StringUtils.collectionToDelimitedString(temp, " ");
return nameString.trim();
}
use of org.openmrs.api.APIException in project openmrs-core by openmrs.
the class RequiredDataAdvice method getChildCollection.
/**
* This method gets a child attribute off of an OpenmrsObject. It usually uses the getter for
* the attribute, but can use the direct field (even if its private) if told to by the
* {@link AllowDirectAccess} annotation.
*
* @param openmrsObject the object to get the collection off of
* @param field the name of the field that is the collection
* @return the actual collection of objects that is on the given <code>openmrsObject</code>
* @should get value of given child collection on given field
* @should should be able to get annotated private fields
* @should throw APIException if getter method not found
*/
@SuppressWarnings("unchecked")
protected static Collection<OpenmrsObject> getChildCollection(OpenmrsObject openmrsObject, Field field) {
String fieldName = field.getName();
String getterName = "get" + StringUtils.capitalize(fieldName);
try {
// checks if direct access is allowed
if (field.isAnnotationPresent(AllowDirectAccess.class)) {
boolean previousFieldAccessibility = field.isAccessible();
field.setAccessible(true);
Collection<OpenmrsObject> childCollection = (Collection<OpenmrsObject>) field.get(openmrsObject);
field.setAccessible(previousFieldAccessibility);
return childCollection;
} else {
// access the field via its getter method
Class<? extends OpenmrsObject> openmrsObjectClass = openmrsObject.getClass();
Method getterMethod = openmrsObjectClass.getMethod(getterName, (Class[]) null);
return (Collection<OpenmrsObject>) getterMethod.invoke(openmrsObject, new Object[] {});
}
} catch (IllegalAccessException e) {
if (field.isAnnotationPresent(AllowDirectAccess.class)) {
throw new APIException("unable.get.field", new Object[] { fieldName, openmrsObject.getClass() });
} else {
throw new APIException("unable.getter.method", new Object[] { "use", getterName, fieldName, openmrsObject.getClass() });
}
} catch (InvocationTargetException e) {
throw new APIException("unable.getter.method", new Object[] { "run", getterName, fieldName, openmrsObject.getClass() });
} catch (NoSuchMethodException e) {
throw new APIException("unable.getter.method", new Object[] { "find", getterName, fieldName, openmrsObject.getClass() });
}
}
use of org.openmrs.api.APIException in project openmrs-core by openmrs.
the class BinaryStreamHandler method getObs.
/**
* Returns the same ComplexData for all views. The title is the original filename, and the data
* is the raw byte[] of data (If the view is set to "download", all commas and whitespace are
* stripped out of the filename to fix an issue where the browser wasn't handling a filename
* with whitespace properly) Note that if the method cannot find the file associated with the
* obs, it returns the obs with the ComplexData = null
*
* @see ComplexObsHandler#getObs(Obs, String)
*/
@Override
public Obs getObs(Obs obs, String view) {
ComplexData complexData = null;
File file = null;
// Raw stream
if (ComplexObsHandler.RAW_VIEW.equals(view)) {
try {
file = getComplexDataFile(obs);
String[] names = obs.getValueComplex().split("\\|");
String originalFilename = names[0];
originalFilename = originalFilename.replace(",", "").replace(" ", "");
if (file.exists()) {
FileInputStream fileInputStream = new FileInputStream(file);
complexData = new ComplexData(originalFilename, fileInputStream);
} else {
log.error("Unable to find file associated with complex obs " + obs.getId());
}
} catch (Exception e) {
throw new APIException("Obs.error.while.trying.get.binary.complex", null, e);
}
} else {
// NOTE: if adding support for another view, don't forget to update supportedViews list above
return null;
}
Assert.notNull(complexData, "Complex data must not be null");
// Get the Mime Type and set it
String mimeType = OpenmrsUtil.getFileMimeType(file);
complexData.setMimeType(mimeType);
obs.setComplexData(complexData);
return obs;
}
use of org.openmrs.api.APIException in project openmrs-core by openmrs.
the class MediaHandler method saveObs.
/**
* @see org.openmrs.obs.ComplexObsHandler#saveObs(org.openmrs.Obs)
*/
@Override
public Obs saveObs(Obs obs) throws APIException {
try {
// Write the File to the File System
String fileName = obs.getComplexData().getTitle();
File outfile = getOutputFileToWrite(obs);
OutputStream out = new FileOutputStream(outfile, false);
FileInputStream mediaStream = (FileInputStream) obs.getComplexData().getData();
OpenmrsUtil.copyFile(mediaStream, out);
// Store the filename in the Obs
obs.setComplexData(null);
obs.setValueComplex(fileName + "|" + outfile.getName());
// close the stream
out.close();
} catch (IOException ioe) {
throw new APIException("Obs.error.trying.write.complex", null, ioe);
}
return obs;
}
Aggregations