Java tutorial
/* * Copyright (C) 2016 FormKiQ Inc. * * Licensed 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. */ package com.formkiq.core.form; import static com.formkiq.core.util.Strings.extractLabelAndValue; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.UUID; import java.util.stream.Collectors; import java.util.stream.Stream; import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.tuple.Pair; import com.formkiq.core.form.dto.ArchiveDTO; import com.formkiq.core.form.dto.FormJSON; import com.formkiq.core.form.dto.FormJSONField; import com.formkiq.core.form.dto.FormJSONFieldType; import com.formkiq.core.form.dto.FormJSONSection; import com.formkiq.core.form.dto.Workflow; import com.formkiq.core.form.dto.WorkflowOutputDocument; import com.formkiq.core.form.dto.WorkflowOutputFormField; /** * Helper Finder method for Form objects. * */ public final class FormFinder { /** * Find {@link FormJSONField}. * @param archive {@link ArchiveDTO} * @param documentfieldname {@link String} * @return {@link Optional} {@link FormJSONField} */ public static Optional<FormJSONField> findField(final ArchiveDTO archive, final String documentfieldname) { Optional<FormJSONField> op = Optional.empty(); List<WorkflowOutputFormField> fields = stream(archive.getWorkflow()); Optional<WorkflowOutputFormField> o = fields.stream() .filter(s -> s.getDocumentfieldname().equals(documentfieldname)).findFirst(); if (o.isPresent()) { String fuuid = extractLabelAndValue(o.get().getForm()).getRight(); String fid = extractLabelAndValue(o.get().getField()).getRight(); FormJSON form = archive.getForm(fuuid); op = FormFinder.findField(form, NumberUtils.toInt(fid, -1)); } return op; } /** * Find {@link FormJSONField} by field id. * @param form {@link FormJSON} * @param id int * @return {@link Optional} of {@link FormJSONField} */ public static Optional<FormJSONField> findField(final FormJSON form, final int id) { Optional<Pair<FormJSONSection, FormJSONField>> op = findSectionAndField(form, id); return op.isPresent() ? Optional.of(op.get().getRight()) : Optional.empty(); } /** * Get {@link FormJSONField} from {@link FormJSONSection}. * @param section {@link FormJSONSection} * @param index int * @return {@link Optional} of {@link FormJSONSection} */ public static Optional<FormJSONField> findField(final FormJSONSection section, final int index) { try { return section != null ? Optional.of(section.getFields().get(index)) : Optional.empty(); } catch (IndexOutOfBoundsException e) { return Optional.empty(); } } /** * Find {@link FormJSONField} by type. * * @param form {@link FormJSON} * @param type {@link FormJSONFieldType} * @return {@link Optional} of {@link FormJSONField} */ public static List<FormJSONField> findFieldsByType(final FormJSON form, final FormJSONFieldType type) { List<FormJSONField> fields = new ArrayList<>(); for (FormJSONSection section : form.getSections()) { fields.addAll(findFieldsByType(section, type)); } return fields; } /** * Find {@link FormJSONField} by type. * * @param section {@link FormJSONSection} * @param type {@link FormJSONFieldType} * @return {@link Optional} of {@link FormJSONField} */ public static List<FormJSONField> findFieldsByType(final FormJSONSection section, final FormJSONFieldType type) { return section.getFields().stream().filter(s -> type.equals(s.getType())).collect(Collectors.toList()); } /** * Find {@link FormJSONField} option value. * @param field {@link FormJSONField} * @param option {@link String} * @return {@link Optional} {@link String} */ public static Optional<String> findOption(final FormJSONField field, final String option) { return field.getOptions().stream() .filter(o -> extractLabelAndValue(o).getRight().equals(extractLabelAndValue(option).getRight())) .findFirst(); } /** * Get {@link FormJSONSection} from {@link FormJSON}. * @param form {@link FormJSON} * @param index int * @return {@link Optional} of {@link FormJSONSection} */ public static Optional<FormJSONSection> findSection(final FormJSON form, final int index) { try { return Optional.of(form.getSections().get(index)); } catch (IndexOutOfBoundsException e) { return Optional.empty(); } } /** * Find {@link FormJSONSection} by section {@link UUID}. * @param form {@link FormJSON} * @param uuid {@link String} * @return {@link Optional} {@link FormJSONSection} */ public static Optional<FormJSONSection> findSection(final FormJSON form, final String uuid) { return form.getSections().stream().filter(s -> s.getUUID().equals(uuid)).findFirst(); } /** * Finds Value by Key. * * @param form {@link FormJSON} * @param id int * @return {@link Optional} */ public static Optional<Pair<FormJSONSection, FormJSONField>> findSectionAndField(final FormJSON form, final int id) { for (FormJSONSection section : form.getSections()) { for (FormJSONField field : section.getFields()) { if (field.getId() == id) { return Optional.of(Pair.of(section, field)); } } } return Optional.empty(); } /** * Finds Value by Key of visible fields. * * @param form {@link FormJSON} * @param valuekey {@link FormJSONField} * @return {@link Optional} */ public static Optional<FormJSONField> findValueByKey(final FormJSON form, final String valuekey) { for (FormJSONSection section : form.getSections()) { for (FormJSONField field : section.getFields()) { if (valuekey.equals(field.getValuekey())) { return Optional.of(field); } } } return Optional.empty(); } /** * Find {@link WorkflowOutputFormField}. * * @param workflow {@link Workflow} * @param form {@link FormJSON} * @param field {@link FormJSONField} * @return {@link Optional} of {@link WorkflowOutputFormField} */ public static Optional<WorkflowOutputFormField> findWorkflowOutputDocument(final Workflow workflow, final FormJSON form, final FormJSONField field) { List<WorkflowOutputFormField> fields = stream(workflow); return fields.stream().filter(s -> { String formUUID = extractLabelAndValue(s.getForm()).getRight(); int id = NumberUtils.toInt(extractLabelAndValue(s.getField()).getRight(), -1); return formUUID.equals(form.getUUID()) && field.getId() == id; }).findFirst(); } /** * Find {@link FormJSONField} by type. * * @param form {@link FormJSON} * @param type {@link FormJSONFieldType} * @return {@link Optional} of {@link FormJSONField} */ public static boolean hasFieldType(final FormJSON form, final FormJSONFieldType type) { return form.getSections().stream() .filter(s -> s.getFields().stream().anyMatch(t -> type.equals(t.getType()))).findFirst() .isPresent(); } /** * Create {@link Stream} of {@link FormJSONField}. * @param form {@link FormJSON} * @return {@link Stream} of {@link FormJSONField} */ public static Stream<FormJSONField> stream(final FormJSON form) { return form.getSections().stream().flatMap(s -> s.getFields().stream()); } /** * Transforms {@link Workflow} to {@link WorkflowOutputFormField}. * * @param workflow {@link Workflow} * @return {@link List} of {@link WorkflowOutputFormField} */ private static List<WorkflowOutputFormField> stream(final Workflow workflow) { List<WorkflowOutputFormField> fields = workflow.getOutputs().stream() .filter(s -> s instanceof WorkflowOutputDocument).map(s -> (WorkflowOutputDocument) s) .flatMap(s -> s.getFields().stream()).collect(Collectors.toList()); return fields; } /** * private constructor. */ private FormFinder() { } }