001/**
002 *
003 * Copyright 2020-2021 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.xdata.form;
018
019import org.jivesoftware.smack.util.Objects;
020import org.jivesoftware.smack.util.StringUtils;
021
022import org.jivesoftware.smackx.xdata.FormField;
023import org.jivesoftware.smackx.xdata.TextSingleFormField;
024import org.jivesoftware.smackx.xdata.packet.DataForm;
025
026public abstract class FilledForm implements FormReader {
027
028    private final DataForm dataForm;
029
030    protected final TextSingleFormField formTypeFormField;
031
032    public FilledForm(DataForm dataForm) {
033        this.dataForm = Objects.requireNonNull(dataForm);
034        String formType = dataForm.getFormType();
035        if (StringUtils.isNullOrEmpty(formType)) {
036            throw new IllegalArgumentException("The provided data form has no hidden FROM_TYPE field.");
037        }
038        if (dataForm.getType() == DataForm.Type.cancel) {
039            throw new IllegalArgumentException("Forms of type 'cancel' are not filled nor fillable");
040        }
041        formTypeFormField = dataForm.getHiddenFormTypeField();
042    }
043
044    @Override
045    public FormField getField(String fieldName) {
046        return dataForm.getField(fieldName);
047    }
048
049    public String getTitle() {
050        return dataForm.getTitle();
051    }
052
053    public StringBuilder getInstructions() {
054        StringBuilder sb = new StringBuilder();
055        for (String instruction : dataForm.getInstructions()) {
056            sb.append(instruction).append('\n');
057        }
058        return sb;
059    }
060
061    public DataForm getDataForm() {
062        return dataForm;
063    }
064
065    public String getFormType() {
066        if (formTypeFormField == null) {
067            return null;
068        }
069        return formTypeFormField.getValue();
070    }
071
072    public boolean hasField(String fieldName) {
073        return dataForm.hasField(fieldName);
074    }
075
076    protected FormField getFieldOrThrow(String fieldName) {
077        FormField formField = getField(fieldName);
078        if (formField == null) {
079            throw new IllegalArgumentException("No field named " + fieldName);
080        }
081        return formField;
082    }
083
084    protected static void ensureFormType(DataForm dataForm, String formType) {
085        String dataFormType = dataForm.getFormType();
086        if (!formType.equals(dataFormType)) {
087            throw new IllegalArgumentException("The provided data form must be of type '" + formType
088                            + "', this one was of type '" + dataFormType + '\'');
089        }
090    }
091
092}