001/** 002 * Copyright 2014 Tampere University of Technology, Pori Department 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package service.tut.pori.contentanalysis; 017 018import java.util.Map; 019 020import javax.xml.bind.annotation.XmlAccessType; 021import javax.xml.bind.annotation.XmlAccessorType; 022import javax.xml.bind.annotation.XmlElement; 023import javax.xml.bind.annotation.XmlElementRef; 024import javax.xml.bind.annotation.XmlRootElement; 025 026import org.apache.commons.lang3.ArrayUtils; 027 028import service.tut.pori.contentanalysis.AsyncTask.TaskType; 029import core.tut.pori.http.ResponseData; 030import core.tut.pori.users.UserIdentity; 031 032 033/** 034 * The base class for task details (task workload). Contains basic members and functionality needed for task submission. 035 * 036 * Note: remember to define \@XmlRootElement(name=ELEMENT_NAME) for your inherited class 037 * 038 * <h2>Optional Elements</h2> 039 * <ul> 040 * <li>{@value core.tut.pori.users.Definitions#ELEMENT_USER_ID}, missing if task is generated by the system, or the task is anonymous.</li> 041 * <li>{@value service.tut.pori.contentanalysis.Definitions#ELEMENT_TASK_PARAMETERS}. The parameters depend on the task in question. If the element is missing the back-ends should run back-end specific default operation. </li> 042 * </ul> 043 * 044 * @see service.tut.pori.contentanalysis.BackendStatusList 045 */ 046@XmlAccessorType(XmlAccessType.NONE) 047public abstract class AbstractTaskDetails extends ResponseData{ 048 @XmlElement(name = Definitions.ELEMENT_BACKEND_ID) 049 private Integer _backendId = null; 050 private String _callbackUri = null; 051 @XmlElement(name = Definitions.ELEMENT_TASK_ID) 052 private Long _taskId = null; 053 @XmlElement(name = Definitions.ELEMENT_TASK_TYPE) 054 private TaskType _taskType = null; 055 private UserIdentity _userId = null; 056 @XmlElement(name = Definitions.ELEMENT_BACKEND_STATUS_LIST) 057 private BackendStatusList _backends = null; // an optional list of back-ends participating in this task 058 private Map<String, String> _metadata = null; 059 060 /** 061 * 062 * @return the value of user identity 063 * @see #getUserId() 064 */ 065 @XmlElement(name = core.tut.pori.users.Definitions.ELEMENT_USER_ID) 066 public Long getUserIdValue(){ 067 return (_userId == null ? null : _userId.getUserId()); 068 } 069 070 /** 071 * for serialization 072 * @param value 073 * @see #setUserId(UserIdentity) 074 */ 075 @SuppressWarnings("unused") 076 private void setUserIdValue(Long value){ 077 if(value != null){ 078 _userId = new UserIdentity(value); 079 } 080 } 081 082 /** 083 * 084 * @return user identity 085 * @see #setUserId(UserIdentity) 086 */ 087 public UserIdentity getUserId() { 088 return _userId; 089 } 090 091 /** 092 * 093 * @param userId 094 * @see #getUserId() 095 */ 096 public void setUserId(UserIdentity userId) { 097 _userId = userId; 098 } 099 100 /** 101 * 102 * @param type 103 * @see #getTaskType() 104 */ 105 public void setTaskType(TaskType type){ 106 _taskType = type; 107 } 108 109 /** 110 * 111 * @return task type 112 * @see #setTaskType(service.tut.pori.contentanalysis.AsyncTask.TaskType) 113 */ 114 public TaskType getTaskType() { 115 return _taskType; 116 } 117 118 /** 119 * 120 * @return back-end id of the first back-end in the status list 121 * @see #getBackendId() 122 */ 123 public Integer getBackendId() { 124 return _backendId; 125 } 126 127 /** 128 * @param backendId 129 * @see #getBackendId() 130 */ 131 public void setBackendId(Integer backendId) { 132 _backendId = backendId; 133 } 134 135 /** 136 * 137 * @return task id 138 * @see #setTaskId(Long) 139 */ 140 public Long getTaskId() { 141 return _taskId; 142 } 143 144 /** 145 * 146 * @param taskId 147 * @see #getTaskId() 148 */ 149 public void setTaskId(Long taskId){ 150 _taskId = taskId; 151 } 152 153 /** 154 * 155 * @return callback uri 156 * @see #setCallbackUri(String) 157 */ 158 @XmlElement(name = Definitions.ELEMENT_CALLBACK_URI) 159 public String getCallbackUri() { 160 return _callbackUri; 161 } 162 163 /** 164 * Can be used to override the default, generated call back uri 165 * 166 * @param callbackUri 167 * @see #getCallbackUri() 168 */ 169 public void setCallbackUri(String callbackUri) { 170 _callbackUri = callbackUri; 171 } 172 173 /** 174 * Default data groups: {@value service.tut.pori.contentanalysis.Definitions#DATA_GROUP_BACKEND_STATUS} 175 * 176 * @return the backends 177 * @see #setBackends(BackendStatusList) 178 */ 179 public BackendStatusList getBackends() { 180 return _backends; 181 } 182 183 /** 184 * @param backends the backends to set 185 * @see #getBackends() 186 */ 187 public void setBackends(BackendStatusList backends) { 188 _backends = backends; 189 } 190 191 /** 192 * 193 * @param status 194 * @see #getBackends() 195 */ 196 public void setBackend(BackendStatus status) { 197 if(_backends == null){ 198 _backends = new BackendStatusList(); 199 } 200 _backends.setBackendStatus(status); 201 } 202 203 /** 204 * @return the metadata 205 * @see #setMetadata(Map) 206 */ 207 public Map<String, String> getMetadata() { 208 return _metadata; 209 } 210 211 /** 212 * @param metadata the metadata to set 213 * @see #getMetadata() 214 */ 215 public void setMetadata(Map<String, String> metadata) { 216 _metadata = metadata; 217 } 218 219 @Override 220 public Class<?>[] getDataClasses() { 221 Class<?>[] classes = super.getDataClasses(); 222 TaskParameters params = getTaskParameters(); 223 if(params != null){ 224 classes = ArrayUtils.add(classes, params.getClass()); 225 } 226 return classes; 227 } 228 229 /** 230 * @return the parameters 231 * @see #setTaskParameters(TaskParameters) 232 */ 233 @XmlElementRef 234 public abstract TaskParameters getTaskParameters(); 235 236 /** 237 * @param parameters the parameters to set 238 * @see #getTaskParameters() 239 */ 240 public abstract void setTaskParameters(TaskParameters parameters); 241 242 /** 243 * Abstract base class for optional task parameters. 244 * 245 * The class should have no-args constructor for database serialization. 246 * 247 */ 248 @XmlRootElement(name = Definitions.ELEMENT_TASK_PARAMETERS) 249 @XmlAccessorType(XmlAccessType.NONE) 250 public static abstract class TaskParameters { 251 /** 252 * initialize the the parameters from the given metadata map 253 * 254 * @param metadata 255 * @throws IllegalArgumentException on bad values 256 */ 257 public abstract void initialize(Map<String, String> metadata) throws IllegalArgumentException; 258 259 /** 260 * 261 * @return this object converted to metadata map or null if no content 262 */ 263 public abstract Map<String, String> toMetadata(); 264 265 /** 266 * For database serialization 267 */ 268 public TaskParameters(){ 269 // nothing needed 270 } 271 } // class TaskParameters 272}