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.twitterjazz; 017 018import java.util.EnumSet; 019import java.util.HashMap; 020import java.util.Map; 021import java.util.Set; 022 023import javax.xml.bind.annotation.XmlAccessType; 024import javax.xml.bind.annotation.XmlAccessorType; 025import javax.xml.bind.annotation.XmlElement; 026import javax.xml.bind.annotation.XmlRootElement; 027 028import org.apache.commons.lang3.BooleanUtils; 029import org.apache.commons.lang3.StringUtils; 030import org.apache.log4j.Logger; 031 032import service.tut.pori.contentanalysis.AbstractTaskDetails; 033import service.tut.pori.contentanalysis.AsyncTask.TaskType; 034import service.tut.pori.twitterjazz.TwitterExtractor.ContentType; 035import core.tut.pori.context.ServiceInitializer; 036 037/** 038 * Details for a twitter summarization task. 039 * 040 * <h3>XML Example</h3> 041 * 042 * {@doc.restlet service="[service.tut.pori.twitterjazz.reference.Definitions#SERVICE_TJ_REFERENCE_EXAMPLE]" method="[service.tut.pori.contentanalysis.Definitions#ELEMENT_TASK_DETAILS]" type="GET" query="" body_uri=""} 043 * 044 * @see service.tut.pori.twitterjazz.TwitterProfile 045 */ 046@XmlRootElement(name=service.tut.pori.contentanalysis.Definitions.ELEMENT_TASK_DETAILS) 047@XmlAccessorType(XmlAccessType.NONE) 048public final class TwitterSummarizationTaskDetails extends AbstractTaskDetails{ 049 private static final Logger LOGGER = Logger.getLogger(TwitterSummarizationTaskDetails.class); 050 private static final String METADATA_CONTENT_TYPES = "contentTypes"; 051 private static final String METADATA_SCREEN_NAME = "screenName"; 052 private static final String METADATA_SUMMARIZE = "summarize"; 053 private static final String METADATA_SYNCHRONIZE = "synchronize"; 054 @XmlElement(name = Definitions.ELEMENT_TWITTER_PROFILE) 055 private TwitterProfile _profile = null; 056 057 /** 058 * 059 * @return profile 060 */ 061 public TwitterProfile getProfile() { 062 return _profile; 063 } 064 065 /** 066 * 067 * @param profile 068 */ 069 public void setProfile(TwitterProfile profile) { 070 _profile = profile; 071 } 072 073 /** 074 * 075 */ 076 public TwitterSummarizationTaskDetails(){ 077 setTaskType(TaskType.TWITTER_PROFILE_SUMMARIZATION); 078 } 079 080 @XmlElement(name = service.tut.pori.contentanalysis.Definitions.ELEMENT_CALLBACK_URI) 081 @Override 082 public String getCallbackUri() { 083 String callbackUri = super.getCallbackUri(); 084 return (StringUtils.isBlank(callbackUri) ? generateFinishedCallbackUri() : callbackUri); 085 } 086 087 /** 088 * 089 * @return the default task finished callback uri 090 */ 091 public static String generateFinishedCallbackUri(){ 092 return ServiceInitializer.getPropertyHandler().getRESTBindContext()+Definitions.SERVICE_TJ+"/"+service.tut.pori.contentanalysis.Definitions.METHOD_TASK_FINISHED; 093 } 094 095 /** 096 * 097 * @param screenName 098 */ 099 public void setScreenName(String screenName){ 100 Map<String, String> metadata = getMetadata(); 101 if(StringUtils.isBlank(screenName)){ 102 LOGGER.debug("No screen name."); 103 if(metadata != null){ 104 metadata.remove(METADATA_SCREEN_NAME); 105 if(metadata.isEmpty()){ 106 setMetadata(null); 107 } 108 } 109 }else{ 110 metadata.put(METADATA_SCREEN_NAME, screenName); 111 setMetadata(metadata); 112 } 113 } 114 115 /** 116 * 117 * @return twitter screen name 118 */ 119 public String getScreenName(){ 120 Map<String, String> metadata = getMetadata(); 121 if(metadata == null || metadata.isEmpty()){ 122 LOGGER.debug("No metadata."); 123 return null; 124 } 125 126 return metadata.get(METADATA_SCREEN_NAME); 127 } 128 129 /** 130 * @return the contentTypes 131 */ 132 public EnumSet<ContentType> getContentTypes() { 133 Map<String, String> metadata = getMetadata(); 134 if(metadata == null || metadata.isEmpty()){ 135 LOGGER.debug("No metadata."); 136 return null; 137 } 138 139 String[] types = StringUtils.split(metadata.get(METADATA_CONTENT_TYPES), core.tut.pori.http.Definitions.SEPARATOR_URI_QUERY_PARAM_VALUES); 140 if(types == null){ 141 LOGGER.debug("No content types."); 142 return null; 143 } 144 145 EnumSet<ContentType> ctypes = EnumSet.noneOf(ContentType.class); 146 ContentType[] tValues = ContentType.values(); 147 for(String type : types){ 148 for(ContentType t : tValues){ 149 if(t.name().equals(type)){ 150 ctypes.add(t); 151 } 152 } 153 } 154 155 if(ctypes.isEmpty()){ 156 LOGGER.warn("No valid content types."); 157 return null; 158 }else{ 159 return ctypes; 160 } 161 } 162 163 /** 164 * @param contentTypes the contentTypes to set 165 */ 166 public void setContentTypes(Set<ContentType> contentTypes) { 167 Map<String, String> metadata = getMetadata(); 168 if(contentTypes == null || contentTypes.isEmpty()){ 169 LOGGER.debug("No content types."); 170 if(metadata != null){ 171 metadata.remove(METADATA_CONTENT_TYPES); 172 if(metadata.isEmpty()){ 173 setMetadata(null); 174 } 175 } 176 return; 177 } 178 StringBuilder cb = new StringBuilder(); 179 for(ContentType t : contentTypes){ 180 cb.append(t.name()); 181 cb.append(core.tut.pori.http.Definitions.SEPARATOR_URI_QUERY_PARAM_VALUES); 182 } 183 if(metadata == null){ 184 metadata = new HashMap<>(1); 185 } 186 metadata.put(METADATA_CONTENT_TYPES, cb.substring(0, cb.length()-1)); 187 setMetadata(metadata); 188 } 189 190 /** 191 * 192 * @param summarize 193 */ 194 public void setSummarize(boolean summarize){ 195 Map<String, String> metadata = getMetadata(); 196 if(metadata == null){ 197 LOGGER.debug("No metadata, creating metadata."); 198 metadata = new HashMap<>(1); 199 setMetadata(metadata); 200 } 201 metadata.put(METADATA_SUMMARIZE, BooleanUtils.toStringTrueFalse(summarize)); 202 } 203 204 /** 205 * 206 * @return true if summarization is enabled for this task 207 */ 208 public boolean isSummarize(){ 209 Map<String, String> metadata = getMetadata(); 210 if(metadata == null || metadata.isEmpty()){ 211 LOGGER.debug("No metadata "+METADATA_SUMMARIZE+", returning default: false."); 212 return false; 213 } 214 return BooleanUtils.toBoolean(metadata.get(METADATA_SUMMARIZE)); 215 } 216 217 /** 218 * 219 * @param synchronize 220 */ 221 public void setSynchronize(boolean synchronize){ 222 Map<String, String> metadata = getMetadata(); 223 if(metadata == null){ 224 LOGGER.debug("No metadata, creating metadata."); 225 metadata = new HashMap<>(1); 226 setMetadata(metadata); 227 } 228 metadata.put(METADATA_SYNCHRONIZE, BooleanUtils.toStringTrueFalse(synchronize)); 229 } 230 231 /** 232 * 233 * @return true if synchronization is enabled for this task 234 */ 235 public boolean isSynchronize(){ 236 Map<String, String> metadata = getMetadata(); 237 if(metadata == null || metadata.isEmpty()){ 238 LOGGER.debug("No metadata "+METADATA_SYNCHRONIZE+", returning default: false."); 239 return false; 240 } 241 return BooleanUtils.toBoolean(metadata.get(METADATA_SYNCHRONIZE)); 242 } 243 244 @Override 245 public TaskParameters getTaskParameters() { 246 return null; 247 } 248 249 @Override 250 public void setTaskParameters(TaskParameters parameters) { 251 throw new UnsupportedOperationException("Method not supported."); 252 } 253}