Merge branch 'upstream' into tizen
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / model / AutomationSettingHelper.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package oic.simulator.serviceprovider.model;
18
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22
23 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
24
25 import oic.simulator.serviceprovider.utils.Constants;
26
27 /**
28  * This is a helper class for providing the automation settings information to
29  * UI.
30  */
31 public class AutomationSettingHelper {
32     private String       settingID;
33     private String       settingValue;
34     private List<String> allowedValues;
35
36     public String getSettingID() {
37         return settingID;
38     }
39
40     public void setSettingID(String settingID) {
41         this.settingID = settingID;
42     }
43
44     public String getSettingValue() {
45         return settingValue;
46     }
47
48     public void setSettingValue(String settingValue) {
49         this.settingValue = settingValue;
50     }
51
52     public List<String> getAllowedValues() {
53         return allowedValues;
54     }
55
56     public void setAllowedValues(List<String> allowedValues) {
57         this.allowedValues = allowedValues;
58     }
59
60     public void addAllowedValue(String newText) {
61         if (null != allowedValues) {
62             allowedValues.add(newText);
63         }
64     }
65
66     public static List<AutomationSettingHelper> getAutomationSettings(
67             AttributeElement attribute) {
68         boolean invalidSetting;
69         List<AutomationSettingHelper> settingList = new ArrayList<AutomationSettingHelper>();
70         for (int count = 0; count < Constants.AUTOMATION_SETTINGS_COUNT; count++) {
71             invalidSetting = false;
72             AutomationSettingHelper setting = new AutomationSettingHelper();
73             if (Constants.AUTOMATION_SETTINGS[count]
74                     .equals(Constants.AUTOMATION_TYPE)) {
75                 setting.setSettingID(Constants.AUTOMATION_TYPE);
76                 if (null != attribute) {
77                     setting.setSettingValue(attribute.getAutoUpdateType()
78                             .toString());
79                 } else {
80                     setting.setSettingValue(AutoUpdateType.ONE_TIME.toString());
81                 }
82                 List<String> valueList = new ArrayList<String>();
83                 valueList.add(AutoUpdateType.ONE_TIME.toString());
84                 valueList.add(AutoUpdateType.REPEAT.toString());
85                 setting.setAllowedValues(valueList);
86             } else if (Constants.AUTOMATION_SETTINGS[count]
87                     .equals(Constants.UPDATE_INTERVAL_IN_MS)) {
88                 setting.setSettingID(Constants.UPDATE_INTERVAL_IN_MS);
89                 if (null != attribute) {
90                     setting.setSettingValue(String.valueOf(attribute
91                             .getAutoUpdateInterval()));
92                 } else {
93                     setting.setSettingValue(String
94                             .valueOf(Constants.DEFAULT_AUTOMATION_INTERVAL));
95                 }
96                 List<String> valueList = new ArrayList<String>();
97                 for (int index = 1; index <= 10; index++) {
98                     valueList.add(String.valueOf(index * 500));
99                 }
100                 setting.setAllowedValues(valueList);
101             } else {
102                 invalidSetting = true;
103             }
104             if (!invalidSetting) {
105                 settingList.add(setting);
106             }
107         }
108         return settingList;
109     }
110
111     public static void updateAutomationStatus(
112             List<AutomationSettingHelper> localSettingList, String status) {
113         if (null != localSettingList && null != status) {
114             Iterator<AutomationSettingHelper> settingItr = localSettingList
115                     .iterator();
116             AutomationSettingHelper setting;
117             while (settingItr.hasNext()) {
118                 setting = settingItr.next();
119                 if (null != setting) {
120                     if (setting.getSettingID().equals(Constants.AUTOMATION)) {
121                         setting.setSettingValue(status);
122                         break;
123                     }
124                 }
125             }
126         }
127     }
128 }