001/**
002 *
003 * Copyright © 2016 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.iot.data.element;
018
019import java.util.Collections;
020import java.util.Date;
021import java.util.List;
022
023import org.jivesoftware.smack.packet.ExtensionElement;
024import org.jivesoftware.smack.packet.Message;
025import org.jivesoftware.smack.util.XmlStringBuilder;
026
027import org.jivesoftware.smackx.iot.element.NodeInfo;
028
029public class IoTFieldsExtension implements ExtensionElement {
030
031    public static final String ELEMENT = "fields";
032    public static final String NAMESPACE = Constants.IOT_SENSORDATA_NAMESPACE;
033
034    private final int seqNr;
035    private final boolean done;
036    private final List<NodeElement> nodes;
037
038    public IoTFieldsExtension(int seqNr, boolean done, NodeElement node) {
039        this(seqNr, done, Collections.singletonList(node));
040    }
041
042    public IoTFieldsExtension(int seqNr, boolean done, List<NodeElement> nodes) {
043        this.seqNr = seqNr;
044        this.done = done;
045        this.nodes = Collections.unmodifiableList(nodes);
046    }
047
048    public int getSequenceNr() {
049        return seqNr;
050    }
051
052    public boolean isDone() {
053        return done;
054    }
055
056    public List<NodeElement> getNodes() {
057        return nodes;
058    }
059
060    @Override
061    public String getElementName() {
062        return ELEMENT;
063    }
064
065    @Override
066    public String getNamespace() {
067        return NAMESPACE;
068    }
069
070    @Override
071    public XmlStringBuilder toXML(String enclosingNamespace) {
072        XmlStringBuilder xml = new XmlStringBuilder(this);
073        xml.attribute("seqnr", Integer.toString(seqNr));
074        xml.attribute("done", done);
075        xml.rightAngleBracket();
076
077        xml.append(nodes);
078
079        xml.closeElement(this);
080        return xml;
081    }
082
083    public static IoTFieldsExtension buildFor(int seqNr, boolean done, NodeInfo nodeInfo,
084                    List<? extends IoTDataField> data) {
085        TimestampElement timestampElement = new TimestampElement(new Date(), data);
086        NodeElement nodeElement = new NodeElement(nodeInfo, timestampElement);
087        return new IoTFieldsExtension(seqNr, done, nodeElement);
088    }
089
090    public static IoTFieldsExtension from(Message message) {
091        return message.getExtension(ELEMENT, NAMESPACE);
092    }
093}