Search in sources :

Example 16 with MotechDataService

use of org.motechproject.mds.service.MotechDataService in project motech by motech.

the class ActionHandlerServiceImpl method update.

@Override
public void update(Map<String, Object> parameters) throws ActionHandlerException {
    LOGGER.debug("Action UPDATE: params: {}", parameters);
    String entityClassName = getEntityClassName(parameters);
    MotechDataService dataService = getEntityDataService(entityClassName);
    Entity entity = getEntity(entityClassName);
    Long instanceId = getInstanceId(parameters, true);
    Object instance = retrieveEntityInstance(dataService, instanceId);
    setInstanceProperties(instance, entity.getFields(), parameters);
    dataService.update(instance);
}
Also used : Entity(org.motechproject.mds.domain.Entity) MotechDataService(org.motechproject.mds.service.MotechDataService)

Example 17 with MotechDataService

use of org.motechproject.mds.service.MotechDataService in project motech by motech.

the class MdsLookupServiceImpl method buildLookupExecutor.

private LookupExecutor buildLookupExecutor(String entityClassName, String lookupName) {
    String fullyQualifiedEntityClassName;
    if (entityClassName.contains(".")) {
        fullyQualifiedEntityClassName = entityClassName;
    } else {
        fullyQualifiedEntityClassName = Constants.PackagesGenerated.ENTITY + "." + entityClassName;
    }
    MotechDataService dataService = OSGiServiceUtils.findService(bundleContext, MotechClassPool.getInterfaceName(fullyQualifiedEntityClassName));
    EntityDto entity = entityService.getEntityByClassName(fullyQualifiedEntityClassName);
    LookupDto lookup = entityService.getLookupByName(entity.getId(), lookupName);
    return new LookupExecutor(dataService, lookup, entityService.getLookupFieldsMapping(entity.getId(), lookupName));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) LookupDto(org.motechproject.mds.dto.LookupDto) LookupExecutor(org.motechproject.mds.lookup.LookupExecutor) MotechDataService(org.motechproject.mds.service.MotechDataService)

Example 18 with MotechDataService

use of org.motechproject.mds.service.MotechDataService in project motech by motech.

the class AbstractMdsExporter method exportData.

protected long exportData(EntityInfo entityInfo, TableWriter writer, String lookupName, QueryParams params, List<String> headers, Map<String, Object> lookupFields, CsvExportCustomizer exportCustomizer) {
    final MotechDataService dataService = DataServiceHelper.getDataService(bundleContext, entityInfo.getClassName());
    final Map<String, FieldDto> fieldMap = new HashMap<>();
    for (FieldDto field : entityInfo.getFieldDtos()) {
        fieldMap.put(exportCustomizer.exportDisplayName(field), field);
    }
    // we must respect field ordering
    String[] orderedHeaders = orderHeaders(entityInfo.getAdvancedSettings().getBrowsing(), headers == null ? fieldsToHeaders(entityInfo.getFieldDtos(), exportCustomizer) : headers.toArray(new String[headers.size()]), entityInfo.getFieldDtos(), exportCustomizer);
    try {
        writer.writeHeader(orderedHeaders);
        long rowsExported = 0;
        Map<String, String> row = new HashMap<>();
        List<Object> instances = StringUtils.isBlank(lookupName) ? dataService.retrieveAll(params) : mdsLookupService.findMany(entityInfo.getClassName(), lookupName, lookupFields, params);
        for (Object instance : instances) {
            buildCsvRow(row, fieldMap, instance, orderedHeaders, exportCustomizer);
            writer.writeRow(row, orderedHeaders);
            rowsExported++;
        }
        return rowsExported;
    } catch (IOException e) {
        throw new DataExportException("IO Error when writing data", e);
    }
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) DataExportException(org.motechproject.mds.exception.csv.DataExportException) MotechDataService(org.motechproject.mds.service.MotechDataService) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 19 with MotechDataService

use of org.motechproject.mds.service.MotechDataService in project motech by motech.

the class MDSInterfaceResolver method processMDSInterfaces.

/**
 * This method scans the given bundle and registers all interfaces extending
 * {@link MotechDataService} interface in the {@link MotechClassPool}.
 *
 * @param bundle A bundle to scan in.
 */
public static void processMDSInterfaces(Bundle bundle) {
    LOGGER.debug("Starting to look for MotechDataService implementations in bundle {}", bundle.getSymbolicName());
    List<Class<? extends MotechDataService>> allBundleInterfaces = ReflectionsUtil.getMdsInterfaces(bundle);
    for (Class clazz : allBundleInterfaces) {
        LOGGER.debug("Processing {} class", clazz.getName());
        Type[] interfaces = clazz.getGenericInterfaces();
        ParameterizedType parameterizedType = findMDSInterface(interfaces);
        LOGGER.info("Registering {} service in MotechClassPool", clazz.getName());
        MotechClassPool.registerServiceInterface(discoverEntityClassName(parameterizedType), clazz.getName());
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) MotechDataService(org.motechproject.mds.service.MotechDataService)

Example 20 with MotechDataService

use of org.motechproject.mds.service.MotechDataService in project motech by motech.

the class InstanceServiceImpl method getInstanceHistory.

@Override
public List<BasicHistoryRecord> getInstanceHistory(Long entityId, Long instanceId, QueryParams queryParams) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);
    MotechDataService service = getServiceForEntity(entity);
    Object instance = service.retrieve(ID_FIELD_NAME, instanceId);
    List history = historyService.getHistoryForInstance(instance, queryParams);
    updateGridSize(entityId, queryParams);
    List<BasicHistoryRecord> result = new ArrayList<>();
    for (Object o : history) {
        result.add(convertToBasicHistoryRecord(o, entity, instanceId, service));
    }
    return result;
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) BasicHistoryRecord(org.motechproject.mds.web.domain.BasicHistoryRecord) MotechDataService(org.motechproject.mds.service.MotechDataService)

Aggregations

MotechDataService (org.motechproject.mds.service.MotechDataService)52 EntityDto (org.motechproject.mds.dto.EntityDto)26 FieldDto (org.motechproject.mds.dto.FieldDto)14 ArrayList (java.util.ArrayList)13 List (java.util.List)9 Test (org.junit.Test)8 Entity (org.motechproject.mds.domain.Entity)4 LookupDto (org.motechproject.mds.dto.LookupDto)4 ObjectNotFoundException (org.motechproject.mds.exception.object.ObjectNotFoundException)4 LookupExecutor (org.motechproject.mds.lookup.LookupExecutor)4 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)4 Bundle (org.osgi.framework.Bundle)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Collection (java.util.Collection)3 LookupExecutionException (org.motechproject.mds.exception.lookup.LookupExecutionException)3 ObjectReadException (org.motechproject.mds.exception.object.ObjectReadException)3 DefaultMotechDataService (org.motechproject.mds.service.DefaultMotechDataService)3 EntityRecord (org.motechproject.mds.web.domain.EntityRecord)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2