public String generateNewComponentId() {

    if (lastComponentId == 0) {

      Set<String> allIds = FormManagerUtil.getAllComponentsIds(getXformsDocument());

      for (String id : allIds) {
        int idNr = FormManagerUtil.parseIdNumber(id);

        if (idNr > lastComponentId) lastComponentId = idNr;
      }
    }

    return FormManagerUtil.CTID + (++lastComponentId);
  }
  public Locale getDefaultLocale() {

    if (defaultLocale == null) {
      defaultLocale = FormManagerUtil.getDefaultFormLocale(getXformsDocument());
    }

    return defaultLocale;
  }
  public void setFormErrorMsg(LocalizedStringBean formError) {

    if (formError != null) {

      FormManagerUtil.setFormErrorMsg(getXformsDocument(), formError);
      this.formErrorMsg = formError;
    } else {
      Logger.getLogger(getClass().getName())
          .log(Level.WARNING, "Tried to set form error message, but no formError provided");
    }
  }
  public void setFormTitle(LocalizedStringBean formTitle) {

    if (formTitle != null) {

      FormManagerUtil.setFormTitle(getXformsDocument(), formTitle);
      this.formTitle = formTitle;
    } else {
      Logger.getLogger(getClass().getName())
          .log(Level.WARNING, "Tried to set form title, but no formTitle provided");
    }
  }
  public void lazyLoadDocument(PersistedFormDocument persistedForm) {

    if (persistedForm != null) {
      setFormId(persistedForm.getFormId());
      setFormType(persistedForm.getFormType());
      setFormVersion(persistedForm.getVersion());
    }

    setContainerElement(FormManagerUtil.getComponentsContainerElement(getXformsDocument()));

    persistedFormForLazyLoading = persistedForm;
  }
  public Document getComponentsXml(Locale locale) {

    Map<Locale, Document> localizedComponentsDocuments = getLocalizedComponentsDocuments();

    Document doc;

    if (!localizedComponentsDocuments.containsKey(locale)) {

      try {
        ComponentsGenerator componentsGenerator = getContext().getComponentsGenerator();
        Document xformClone = (Document) getXformsDocument().cloneNode(true);

        FormManagerUtil.modifyXFormsDocumentForViewing(xformClone);
        FormManagerUtil.setCurrentFormLocale(xformClone, locale);
        FormManagerUtil.modifyFormForLocalisationInFormbuilder(xformClone);

        doc = componentsGenerator.generateHtmlRepresentation(xformClone);

        localizedComponentsDocuments.put(locale, doc);
        setFormDocumentModified(false);

      } catch (Exception e) {

        Logger.getLogger(getClass().getName())
            .log(Level.SEVERE, "Exception while generating components html document", e);
        doc =
            localizedComponentsDocuments.isEmpty()
                ? null
                : localizedComponentsDocuments.values().iterator().next();
      }

    } else {
      doc = localizedComponentsDocuments.get(locale);
    }

    return doc;
  }
  private void loadDocumentInternal(PersistedFormDocument persistedForm) {

    Document xformsXmlDoc = getXformsDocument();

    if (persistedForm != null) {
      setFormId(persistedForm.getFormId());
      setFormType(persistedForm.getFormType());
      setFormVersion(persistedForm.getVersion());
    }

    setContainerElement(FormManagerUtil.getComponentsContainerElement(xformsXmlDoc));
    loadContainerComponents();
    setProperties();

    loaded = true;
  }
 public Element getSubmissionInstanceElement() {
   // TODO: this can be put to component data bean and lazy loaded there
   return FormManagerUtil.getFormSubmissionInstanceElement(getXformsDocument());
 }
  public LocalizedStringBean getFormErrorMsg() {

    if (formErrorMsg == null) formErrorMsg = FormManagerUtil.getFormErrorMsg(getXformsDocument());

    return formErrorMsg;
  }
  public LocalizedStringBean getFormTitle() {

    if (formTitle == null) formTitle = FormManagerUtil.getFormTitle(getXformsDocument());

    return formTitle;
  }
  public String getFormSourceCode() throws Exception {

    return FormManagerUtil.serializeDocument(getXformsDocument());
  }
  public void setDefaultLocale(Locale defaultLocale) {

    FormManagerUtil.setDefaultFormLocale(getXformsDocument(), defaultLocale);
    this.defaultLocale = defaultLocale;
  }
  public void setFormId(Long formId) {

    FormManagerUtil.setFormId(getXformsDocument(), String.valueOf(formId));
    this.formId = formId;
  }