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