Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / things-manager / sdk / java / src / org / iotivity / service / tm / ActionSet.java
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20 /**
21  * @file   ActionSet.java
22  *
23  * @brief  This file contains class which provides functions to retrieve ActionSet from plain text.
24  *
25  */
26 package org.iotivity.service.tm;
27
28 import java.util.StringTokenizer;
29 import java.util.Vector;
30
31 import android.util.Log;
32
33 /**
34  * This class provides functions to retrieve ActionSet from plain text.
35  * An ActionSet is a set of descriptions of actions needed by remote
36  * devices as members of a specific group. To create an ActionSet, one
37  * needs to know the Delimeter serialization.
38  *
39  */
40 public class ActionSet extends Time {
41     /**
42      * String Tag for Logging
43      */
44     private static final String LOG_TAG      = "ActionSet";
45     /**
46      * A first segment before the first asterisk(*) in ActionSet string is an
47      * actionset name
48      */
49     public String               actionsetName;
50     /**
51      * Action instance stored in listOfAction vector variable.
52      */
53     public Vector<Action>       listOfAction = new Vector<Action>();
54
55     /**
56      * This function generates an ActionSet String which contains ActionSet
57      * name, delay in seconds, ActionSetType which can be 0(NONE), 1(SCHEDULED)
58      * or 2(RECURSIVE) and a list of actions. (Example: movieTime*10
59      * 1*uri=coap://10.251.44.228:49858/a/light|power=
60      * OFF*uri=coap://10.251.44.228:49858). The first segment before the first
61      * asterisk(*) is an ActionSet name. The second segment goes before the next
62      * asterisk. In the above example, 10 is the second segment which signifies
63      * time delay in seconds followed by a space and ActionSetType which
64      * signifies whether an ActionSet has to be triggered Recursively(2) or
65      * Scheduled manner(1) or immediately(0).
66      * "uri=coap://10.251.44.228:49858/a/light|power=10" is the third segment.
67      * This can be also divided into two sub segments by a vertical bar(|): URI
68      * and a pair of attribute key and value.
69      *
70      * @return String for a specific action.
71      *
72      */
73     @Override
74     public String toString() {
75         StringBuilder result = new StringBuilder();
76
77         // Append action name
78         result.append(actionsetName + "*");
79
80         // Append delay and type
81         result.append(getDelay() + " " + getType().ordinal() + "*");
82
83         // Append list of actions
84         for (int i = 0; i < listOfAction.size(); i++) {
85             if (i != 0)
86                 result.append('*');
87             result.append(listOfAction.elementAt(i).toString());
88         }
89
90         return result.toString();
91     }
92
93     /**
94      * This function extracts a list of Actions from plain text and constructs
95      * an ActionSet.
96      *
97      * @param actionsetString
98      *            ActionSet in String format. (Example: movieTime*10
99      *            1*uri=coap://10.251.44.228:49858/a/light|power=
100      *            OFF*uri=coap://10.251.44.228:49858). The first segment before
101      *            the first asterisk(*) is an ActionSet name. The second segment
102      *            goes before the next asterisk. In the above example, 10 is the
103      *            second segment which signifies time delay in seconds followed
104      *            by a space and ActionSetType which signifies whether an
105      *            ActionSet has to be triggered Recursively(2) or Scheduled
106      *            manner(1) or immediately(0).
107      *            "uri=coap://10.251.44.228:49858/a/light|power=10" is the third
108      *            segment. This can be also divided into two sub segments by a
109      *            vertical bar(|): URI and a pair of attribute key and value).
110      *
111      * @return ActionSet which is a set of descriptions of actions needed by
112      *         remote devices as members of a specific group.
113      *
114      */
115     public static ActionSet toActionSet(String actionsetString) {
116         if (0 == actionsetString.length()) {
117             return null;
118         }
119
120         ActionSet result = new ActionSet();
121         StringTokenizer tokenizer = new StringTokenizer(actionsetString, "*");
122         boolean actionNameFlag = false;
123         while (tokenizer.hasMoreTokens()) {
124             String segment = tokenizer.nextToken();
125             if (false == actionNameFlag) {
126                 if (true == segment.contains("|")
127                         || true == segment.contains("=")) {
128                     Log.e(LOG_TAG, "Invalid actionset name string!");
129                     return null;
130                 }
131
132                 result.actionsetName = segment;
133                 actionNameFlag = true;
134             } else {
135                 Action action = Action.toAction(segment);
136                 if (null == action) {
137                     Log.e(LOG_TAG, "Failed to convert string to Action class!");
138                     return null;
139                 }
140
141                 result.listOfAction.add(action);
142             }
143         }
144
145         return result;
146     }
147 }