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