package be.redtree.sql; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.criterion.Restrictions; import be.redtree.model.Field; import be.redtree.model.FieldResult; import be.redtree.model.Form; import be.redtree.model.FormResult; import be.redtree.model.MultiValue; public class FormController implements Serializable { private static Log sLog = LogFactory.getLog(FormController.class); public Long addForm(Form form) { Session session = HibernateUtil.getSession(); Long id = new Long(0); try { id = (Long) session.save(form); } catch (Exception e) { sLog.error("failed to query sql", e); } finally { HibernateUtil.shutDown(session); } return id; } public Long addFormResult(FormResult formResult) { Session session = HibernateUtil.getSession(); Long id = new Long(0); try { id = (Long) session.save(formResult); } catch (Exception e) { sLog.error("failed to query sql", e); } finally { HibernateUtil.shutDown(session); } return id; } public void deleteForm(Form form) { Session session = HibernateUtil.getSession(); try { session.delete(form); } catch (Exception e) { sLog.error("failed to query sql", e); } finally { HibernateUtil.shutDown(session); } } public void editForm(Form form) { Form currentForm = getForm(form.getId()); for (Field currentfield : currentForm.getFields()) { Boolean found = false; for (Field field : form.getFields()) { if (field.getUuid().equals(currentfield.getUuid())) { found = true; Boolean foundValue = false; for (MultiValue multiValue : currentfield.getValues()) { for (MultiValue multiValue_ : field.getValues()) { if (multiValue.getId().equals(multiValue_.getId())) { foundValue = true; } } if(!foundValue){ deleteValue(multiValue); } } } } if (!found) { deleteField(currentfield); } } Session session = HibernateUtil.getSession(); try { session.saveOrUpdate(form); } catch (Exception e) { sLog.error("failed to query sql", e); } finally { HibernateUtil.shutDown(session); } } private void deleteValue(MultiValue multiValue) { Session session = HibernateUtil.getSession(); try { session.delete(multiValue); } catch (Exception e) { sLog.error("failed to query sql", e); } finally { HibernateUtil.shutDown(session); } } public void deleteField(Field field) { Session session = HibernateUtil.getSession(); try { session.delete(field); } catch (Exception e) { sLog.error("failed to query sql", e); } finally { HibernateUtil.shutDown(session); } } public Form getForm(Long id) { Session session = HibernateUtil.getSession(); Criteria criteria = session.createCriteria(Form.class); criteria.add(Restrictions.eq("id", id)); Form form = null; try { form = (Form) criteria.uniqueResult(); } catch (Exception e) { sLog.error("failed to query sql", e); } finally { HibernateUtil.shutDown(session); } return form; } public Field getField(Long id) { Session session = HibernateUtil.getSession(); Criteria criteria = session.createCriteria(Field.class); criteria.add(Restrictions.eq("id", id)); Field field = null; try { field = (Field) criteria.uniqueResult(); } catch (Exception e) { sLog.error("failed to query sql", e); } finally { HibernateUtil.shutDown(session); } return field; } public List<Form> getForms(Long companyId, Boolean isTemplate) { Session session = HibernateUtil.getSession(); List<Form> forms = new ArrayList<Form>(); try { forms = session.createQuery("from webform where companyId=" + companyId + " and template=" + isTemplate).list(); } catch (Exception e) { sLog.error("failed to query sql", e); } finally { HibernateUtil.shutDown(session); } return forms; } /* * Results */ public List<FormResult> getFormResults(Long formId) { Session session = HibernateUtil.getSession(); List<FormResult> results = new ArrayList<FormResult>(); try { results = session.createQuery("from webform_form_result where formId=" + formId).list(); } catch (Exception e) { sLog.error("failed to query sql", e); } finally { HibernateUtil.shutDown(session); } return results; } public List<FieldResult> getFieldResults(Long fieldId) { Session session = HibernateUtil.getSession(); List<FieldResult> results = new ArrayList<FieldResult>(); try { results = session.createQuery("from webform_field_result where fieldId=" + fieldId).list(); } catch (Exception e) { sLog.error("failed to query sql", e); } finally { HibernateUtil.shutDown(session); } return results; } public List<FormResult> getUserResults(Long formId, Long userId) { Session session = HibernateUtil.getSession(); Criteria criteria = session.createCriteria(FormResult.class); criteria.add(Restrictions.eq("userId", userId)); criteria.add(Restrictions.eq("formId", formId)); List<FormResult> results = new ArrayList<FormResult>(); try { results = criteria.list(); } catch (Exception e) { sLog.error("failed to query sql", e); } finally { HibernateUtil.shutDown(session); } return results; } }