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 */
017
018package org.jivesoftware.smackx.workgroup.settings;
019
020import java.io.IOException;
021
022import org.jivesoftware.smack.packet.IqData;
023import org.jivesoftware.smack.packet.SimpleIQ;
024import org.jivesoftware.smack.packet.XmlEnvironment;
025import org.jivesoftware.smack.provider.IqProvider;
026import org.jivesoftware.smack.util.StringUtils;
027import org.jivesoftware.smack.xml.XmlPullParser;
028import org.jivesoftware.smack.xml.XmlPullParserException;
029
030public class OfflineSettings extends SimpleIQ {
031    private String redirectURL;
032
033    private String offlineText;
034    private String emailAddress;
035    private String subject;
036
037    public String getRedirectURL() {
038        if (!StringUtils.isNotEmpty(redirectURL)) {
039            return "";
040        }
041        return redirectURL;
042    }
043
044    public void setRedirectURL(String redirectURL) {
045        this.redirectURL = redirectURL;
046    }
047
048    public String getOfflineText() {
049        if (!StringUtils.isNotEmpty(offlineText)) {
050            return "";
051        }
052        return offlineText;
053    }
054
055    public void setOfflineText(String offlineText) {
056        this.offlineText = offlineText;
057    }
058
059    public String getEmailAddress() {
060        if (!StringUtils.isNotEmpty(emailAddress)) {
061            return "";
062        }
063        return emailAddress;
064    }
065
066    public void setEmailAddress(String emailAddress) {
067        this.emailAddress = emailAddress;
068    }
069
070    public String getSubject() {
071        if (!StringUtils.isNotEmpty(subject)) {
072            return "";
073        }
074        return subject;
075    }
076
077    public void setSubject(String subject) {
078        this.subject = subject;
079    }
080
081    public boolean redirects() {
082        return StringUtils.isNotEmpty(getRedirectURL());
083    }
084
085    public boolean isConfigured() {
086        return StringUtils.isNotEmpty(getEmailAddress()) &&
087               StringUtils.isNotEmpty(getSubject()) &&
088               StringUtils.isNotEmpty(getOfflineText());
089    }
090
091    /**
092     * Element name of the stanza extension.
093     */
094    public static final String ELEMENT_NAME = "offline-settings";
095
096    /**
097     * Namespace of the stanza extension.
098     */
099    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
100
101    public OfflineSettings() {
102        super(ELEMENT_NAME, NAMESPACE);
103    }
104
105    /**
106     * Stanza extension provider for AgentStatusRequest packets.
107     */
108    public static class InternalProvider extends IqProvider<OfflineSettings> {
109
110        @Override
111        public OfflineSettings parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
112            OfflineSettings offlineSettings = new OfflineSettings();
113
114            boolean done = false;
115            String redirectPage = null;
116            String subject = null;
117            String offlineText = null;
118            String emailAddress = null;
119
120            while (!done) {
121                XmlPullParser.Event eventType = parser.next();
122                if (eventType == XmlPullParser.Event.START_ELEMENT && "redirectPage".equals(parser.getName())) {
123                    redirectPage = parser.nextText();
124                }
125                else if (eventType == XmlPullParser.Event.START_ELEMENT && "subject".equals(parser.getName())) {
126                    subject = parser.nextText();
127                }
128                else if (eventType == XmlPullParser.Event.START_ELEMENT && "offlineText".equals(parser.getName())) {
129                    offlineText = parser.nextText();
130                }
131                else if (eventType == XmlPullParser.Event.START_ELEMENT && "emailAddress".equals(parser.getName())) {
132                    emailAddress = parser.nextText();
133                }
134                else if (eventType == XmlPullParser.Event.END_ELEMENT && "offline-settings".equals(parser.getName())) {
135                    done = true;
136                }
137            }
138
139            offlineSettings.setEmailAddress(emailAddress);
140            offlineSettings.setRedirectURL(redirectPage);
141            offlineSettings.setSubject(subject);
142            offlineSettings.setOfflineText(offlineText);
143            return offlineSettings;
144        }
145    }
146}
147