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 org.jivesoftware.smackx.workgroup.util.ModelUtil;
021import org.jivesoftware.smack.packet.IQ;
022import org.jivesoftware.smack.provider.IQProvider;
023import org.xmlpull.v1.XmlPullParser;
024
025public class OfflineSettings extends IQ {
026    private String redirectURL;
027
028    private String offlineText;
029    private String emailAddress;
030    private String subject;
031
032    public String getRedirectURL() {
033        if (!ModelUtil.hasLength(redirectURL)) {
034            return "";
035        }
036        return redirectURL;
037    }
038
039    public void setRedirectURL(String redirectURL) {
040        this.redirectURL = redirectURL;
041    }
042
043    public String getOfflineText() {
044        if (!ModelUtil.hasLength(offlineText)) {
045            return "";
046        }
047        return offlineText;
048    }
049
050    public void setOfflineText(String offlineText) {
051        this.offlineText = offlineText;
052    }
053
054    public String getEmailAddress() {
055        if (!ModelUtil.hasLength(emailAddress)) {
056            return "";
057        }
058        return emailAddress;
059    }
060
061    public void setEmailAddress(String emailAddress) {
062        this.emailAddress = emailAddress;
063    }
064
065    public String getSubject() {
066        if (!ModelUtil.hasLength(subject)) {
067            return "";
068        }
069        return subject;
070    }
071
072    public void setSubject(String subject) {
073        this.subject = subject;
074    }
075
076    public boolean redirects() {
077        return (ModelUtil.hasLength(getRedirectURL()));
078    }
079
080    public boolean isConfigured(){
081        return ModelUtil.hasLength(getEmailAddress()) &&
082               ModelUtil.hasLength(getSubject()) &&
083               ModelUtil.hasLength(getOfflineText());
084    }
085
086    /**
087     * Element name of the packet extension.
088     */
089    public static final String ELEMENT_NAME = "offline-settings";
090
091    /**
092     * Namespace of the packet extension.
093     */
094    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
095
096    public String getChildElementXML() {
097        StringBuilder buf = new StringBuilder();
098
099        buf.append("<").append(ELEMENT_NAME).append(" xmlns=");
100        buf.append('"');
101        buf.append(NAMESPACE);
102        buf.append('"');
103        buf.append("></").append(ELEMENT_NAME).append("> ");
104        return buf.toString();
105    }
106
107
108    /**
109     * Packet extension provider for AgentStatusRequest packets.
110     */
111    public static class InternalProvider implements IQProvider {
112
113        public IQ parseIQ(XmlPullParser parser) throws Exception {
114            if (parser.getEventType() != XmlPullParser.START_TAG) {
115                throw new IllegalStateException("Parser not in proper position, or bad XML.");
116            }
117
118            OfflineSettings offlineSettings = new OfflineSettings();
119
120            boolean done = false;
121            String redirectPage = null;
122            String subject = null;
123            String offlineText = null;
124            String emailAddress = null;
125
126            while (!done) {
127                int eventType = parser.next();
128                if ((eventType == XmlPullParser.START_TAG) && ("redirectPage".equals(parser.getName()))) {
129                    redirectPage = parser.nextText();
130                }
131                else if ((eventType == XmlPullParser.START_TAG) && ("subject".equals(parser.getName()))) {
132                    subject = parser.nextText();
133                }
134                else if ((eventType == XmlPullParser.START_TAG) && ("offlineText".equals(parser.getName()))) {
135                    offlineText = parser.nextText();
136                }
137                else if ((eventType == XmlPullParser.START_TAG) && ("emailAddress".equals(parser.getName()))) {
138                    emailAddress = parser.nextText();
139                }
140                else if (eventType == XmlPullParser.END_TAG && "offline-settings".equals(parser.getName())) {
141                    done = true;
142                }
143            }
144
145            offlineSettings.setEmailAddress(emailAddress);
146            offlineSettings.setRedirectURL(redirectPage);
147            offlineSettings.setSubject(subject);
148            offlineSettings.setOfflineText(offlineText);
149            return offlineSettings;
150        }
151    }
152}
153