org.openxdata.server.service.impl.FormServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.openxdata.server.service.impl.FormServiceImpl.java

Source

/*
 *  Licensed to the OpenXdata Foundation (OXDF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The OXDF licenses this file to You under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, 
 *  software distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and limitations under the License.
 *
 *  Copyright 2010 http://www.openxdata.org.
 */
/*
 *  Licensed to the OpenXdata Foundation (OXDF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The OXDF licenses this file to You under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, 
 *  software distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and limitations under the License.
 *
 *  Copyright 2010 http://www.openxdata.org.
 */
package org.openxdata.server.service.impl;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.openxdata.server.admin.model.ExportedFormData;
import org.openxdata.server.admin.model.ExportedFormDataList;
import org.openxdata.server.admin.model.FormData;
import org.openxdata.server.admin.model.FormDataHeader;
import org.openxdata.server.admin.model.FormDef;
import org.openxdata.server.admin.model.TaskDef;
import org.openxdata.server.dao.FormDAO;
import org.openxdata.server.dao.FormDataDAO;
import org.openxdata.server.dao.EditableDAO;
import org.openxdata.server.dao.UserFormMapDAO;
import org.openxdata.server.export.rdbms.task.RdmsDataExportTask;
import org.openxdata.server.service.FormService;
import org.openxdata.server.service.TaskService;
import org.openxdata.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service("formService")
public class FormServiceImpl implements FormService {

    @Autowired
    private FormDAO formDAO;

    @Autowired
    private FormDataDAO formDataDAO;

    @Autowired
    private UserFormMapDAO userFormMapDAO;

    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private EditableDAO studyDAO;

    @Autowired
    private UserService userService;

    @Autowired
    private TaskService taskService;

    @Override
    public Integer getFormResponseCount(int formDefVersionId) {
        return studyDAO.getFormDataCount(formDefVersionId);
    }

    @Override
    @Transactional(readOnly = true)
    public List<FormDef> getForms() {
        return formDAO.getForms();
    }

    @Override
    @Transactional(readOnly = true)
    public List<FormDef> getFormsForCurrentUser() {
        List<FormDef> forms = userFormMapDAO.getFormsForUser(userService.getLoggedInUser());
        return forms;
    }

    @Override
    public FormData saveFormData(FormData formData) {
        formData.setExported(0); // make sure the exported flag is reset
        if (formData.getFormDataId() != 0) {
            formDataDAO.saveFormDataVersion(formData);
        }

        formDataDAO.saveFormData(formData);
        exportFormDataToRDMS(formData);
        return formData; // lucky this formData id is updated
    }

    private void exportFormDataToRDMS(FormData data) {
        TaskDef task = taskService.getTask("data export");
        if (task != null) {
            String continuous = task.getParamValue("continuous");
            if (continuous != null && continuous.equalsIgnoreCase("true")) {
                RdmsDataExportTask exportTask = new RdmsDataExportTask(task);
                exportTask.exportFormData(data, true);
            }
        }
    }

    @Override
    @Transactional(readOnly = true)
    public List<FormData> getFormData(int formDefVersionId) {
        List<FormData> formData = new ArrayList<FormData>();
        List<FormDataHeader> headers = studyDAO.getFormData(formDefVersionId, null, null, null);
        for (FormDataHeader header : headers) {
            formData.add(formDataDAO.getFormData(header.getFormDataId()));
            log.debug("Loaded form data with id " + header.getFormDataId());
        }
        return formData;
    }

    @Override
    @Transactional(readOnly = true)
    public ExportedFormDataList getFormDataList(String formBinding, String[] questionBindings, int offset,
            int limit, String sortField, boolean ascending) {
        // find out the total size
        BigInteger count = studyDAO.getNumberOfResponses(formBinding);
        log.debug("total number of responses " + count + " for form " + formBinding);

        // create sql statement
        List<Object[]> data = studyDAO.getResponseData(formBinding, questionBindings, offset, limit, sortField,
                ascending);
        log.debug("loading exported form data. #items:" + data.size());

        // process results
        ExportedFormDataList dataList = new ExportedFormDataList();
        if (data != null) {
            dataList.setFromIndex(offset);
            int requestedToIndex = offset + limit;
            dataList.setToIndex(requestedToIndex > data.size() ? data.size() : requestedToIndex);
            dataList.setTotalSize(count.intValue());
            List<ExportedFormData> exportedFormData = getExportedFormData(questionBindings, data);
            dataList.setExportedFormData(exportedFormData);
        }

        return dataList;
    }

    List<ExportedFormData> getExportedFormData(String[] questionBindings, List<Object[]> data) {
        List<ExportedFormData> exportedFormDataList = new ArrayList<ExportedFormData>();
        for (Object[] d : data) {
            String formDataId = (String) d[0];
            FormData formData = formDataDAO.getFormData(Integer.parseInt(formDataId));
            ExportedFormData exportedFormData = new ExportedFormData(formData);
            exportedFormData.putExportedField("openxdata_form_data_id", formDataId);
            for (int i = 1, j = d.length; i < j; i++) {
                Object dataElement = d[i];
                String binding = questionBindings[i - 1];
                populateFormData(exportedFormData, dataElement, binding);

            }
            exportedFormDataList.add(exportedFormData);
        }
        return exportedFormDataList;
    }

    void populateFormData(ExportedFormData exportedFormData, Object data, String binding) {
        if (data == null) {
            exportedFormData.putExportedField(binding, null);
        } else {
            if (data instanceof Serializable) {
                data = adaptUnsupportedDataTypes(data);
                exportedFormData.putExportedField(binding, (Serializable) data);
            } else {
                log.warn("Retrieved value '" + data + "' for question '" + binding
                        + "' is not Serializable, using default toString representation");
                exportedFormData.putExportedField(binding, data.toString());
            }
        }
    }

    /**
     * Adapt data types not supported by GWT. These conversions can result in <a
      * href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363"><i>narrowing
      * primitive conversion</i></a>.
      * 
     * @param data Object to be converted
     * @return the adapted object
     */
    Object adaptUnsupportedDataTypes(Object data) {
        if (data instanceof BigDecimal) {
            data = ((BigDecimal) data).doubleValue();
        } else if (data instanceof BigInteger) {
            data = ((BigInteger) data).intValue();
        }
        return data;
    }

    public void setDao(EditableDAO dao) {
        this.studyDAO = dao;
    }

    public void setFormDataDAO(FormDataDAO formDataDAO) {
        this.formDataDAO = formDataDAO;
    }

    public void setFormDAO(FormDAO formDAO) {
        this.formDAO = formDAO;
    }

    public void setStudyDAO(EditableDAO studyDAO) {
        this.studyDAO = studyDAO;
    }
}