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;
020
021import org.jivesoftware.smackx.xdata.FormField;
022import org.jivesoftware.smackx.xdata.TextSingleFormField;
023import org.jivesoftware.smackx.xdata.packet.DataForm;
024
025public abstract class FilledForm implements FormReader {
026
027    private final DataForm dataForm;
028
029    protected final TextSingleFormField formTypeFormField;
030
031    public FilledForm(DataForm dataForm) {
032        this.dataForm = Objects.requireNonNull(dataForm);
033        if (dataForm.getType() == DataForm.Type.cancel) {
034            throw new IllegalArgumentException("Forms of type 'cancel' are not filled nor fillable");
035        }
036        formTypeFormField = dataForm.getHiddenFormTypeField();
037    }
038
039    @Override
040    public FormField getField(String fieldName) {
041        return dataForm.getField(fieldName);
042    }
043
044    public String getTitle() {
045        return dataForm.getTitle();
046    }
047
048    public StringBuilder getInstructions() {
049        StringBuilder sb = new StringBuilder();
050        for (String instruction : dataForm.getInstructions()) {
051            sb.append(instruction).append('\n');
052        }
053        return sb;
054    }
055
056    public DataForm getDataForm() {
057        return dataForm;
058    }
059
060    public String getFormType() {
061        if (formTypeFormField == null) {
062            return null;
063        }
064        return formTypeFormField.getValue();
065    }
066
067    public boolean hasField(String fieldName) {
068        return dataForm.hasField(fieldName);
069    }
070
071    protected FormField getFieldOrThrow(String fieldName) {
072        FormField formField = getField(fieldName);
073        if (formField == null) {
074            throw new IllegalArgumentException("No field named " + fieldName);
075        }
076        return formField;
077    }
078
079    protected static void ensureFormType(DataForm dataForm, String formType) {
080        String dataFormType = dataForm.getFormType();
081        if (!formType.equals(dataFormType)) {
082            throw new IllegalArgumentException("The provided data form must be of type '" + formType
083                            + "', this one was of type '" + dataFormType + '\'');
084        }
085    }
086
087}