001/**
002 *
003 * Copyright 2003-2007 Jive Software.
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.workgroup.settings;
018
019import java.io.IOException;
020
021import org.jivesoftware.smack.packet.IqData;
022import org.jivesoftware.smack.packet.SimpleIQ;
023import org.jivesoftware.smack.packet.XmlEnvironment;
024import org.jivesoftware.smack.provider.IqProvider;
025import org.jivesoftware.smack.util.StringUtils;
026import org.jivesoftware.smack.xml.XmlPullParser;
027import org.jivesoftware.smack.xml.XmlPullParserException;
028
029public class SearchSettings extends SimpleIQ {
030    private String forumsLocation;
031    private String kbLocation;
032
033    public boolean isSearchEnabled() {
034        return StringUtils.isNotEmpty(getForumsLocation()) && StringUtils.isNotEmpty(getKbLocation());
035    }
036
037    public String getForumsLocation() {
038        return forumsLocation;
039    }
040
041    public void setForumsLocation(String forumsLocation) {
042        this.forumsLocation = forumsLocation;
043    }
044
045    public String getKbLocation() {
046        return kbLocation;
047    }
048
049    public void setKbLocation(String kbLocation) {
050        this.kbLocation = kbLocation;
051    }
052
053    public boolean hasKB() {
054        return StringUtils.isNotEmpty(getKbLocation());
055    }
056
057    public boolean hasForums() {
058        return StringUtils.isNotEmpty(getForumsLocation());
059    }
060
061
062    /**
063     * Element name of the stanza extension.
064     */
065    public static final String ELEMENT_NAME = "search-settings";
066
067    /**
068     * Namespace of the stanza extension.
069     */
070    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
071
072    public SearchSettings() {
073        super(ELEMENT_NAME, NAMESPACE);
074    }
075
076    /**
077     * Stanza extension provider for AgentStatusRequest packets.
078     */
079    public static class InternalProvider extends IqProvider<SearchSettings> {
080
081        @Override
082        public SearchSettings parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
083            SearchSettings settings = new SearchSettings();
084
085            boolean done = false;
086            String kb = null;
087            String forums = null;
088
089            while (!done) {
090                XmlPullParser.Event eventType = parser.next();
091                if (eventType == XmlPullParser.Event.START_ELEMENT && "forums".equals(parser.getName())) {
092                    forums = parser.nextText();
093                }
094                else if ((eventType == XmlPullParser.Event.START_ELEMENT) && "kb".equals(parser.getName())) {
095                    kb = parser.nextText();
096                }
097                else if (eventType == XmlPullParser.Event.END_ELEMENT && "search-settings".equals(parser.getName())) {
098                    done = true;
099                }
100            }
101
102            settings.setForumsLocation(forums);
103            settings.setKbLocation(kb);
104            return settings;
105        }
106    }
107}