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.facebookjazz; 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 core.tut.pori.context.ServiceInitializer; 033import service.tut.pori.contentanalysis.AbstractTaskDetails; 034import service.tut.pori.contentanalysis.AsyncTask.TaskType; 035import service.tut.pori.facebookjazz.FacebookExtractor.ContentType; 036 037/** 038 * Details of a Facebook Summarization Task. 039 * 040 * <h3>XML Example</h3> 041 * 042 * {@doc.restlet service="[service.tut.pori.facebookjazz.reference.Definitions#SERVICE_FBJ_REFERENCE_EXAMPLE]" method="[service.tut.pori.contentanalysis.Definitions#ELEMENT_TASK_DETAILS]" type="GET" query="[service.tut.pori.facebookjazz.reference.Definitions#PARAMETER_TASK_TYPE]=[service.tut.pori.contentanalysis.AsyncTask$TaskType#FACEBOOK_PROFILE_SUMMARIZATION]" body_uri=""} 043 * 044 * @see service.tut.pori.facebookjazz.FacebookProfile 045 */ 046@XmlRootElement(name=service.tut.pori.contentanalysis.Definitions.ELEMENT_TASK_DETAILS) 047@XmlAccessorType(XmlAccessType.NONE) 048public final class FBSummarizationTaskDetails extends AbstractTaskDetails{ 049 private static final Logger LOGGER = Logger.getLogger(FBSummarizationTaskDetails.class); 050 private static final String METADATA_CONTENT_TYPES = "contentTypes"; 051 private static final String METADATA_SYNCHRONIZE = "synchronize"; 052 @XmlElement(name = Definitions.ELEMENT_FACEBOOK_PROFILE) 053 private FacebookProfile _profile = null; 054 055 /** 056 * 057 * @return profile 058 * @see #setProfile(FacebookProfile) 059 */ 060 public FacebookProfile getProfile() { 061 return _profile; 062 } 063 064 /** 065 * 066 * @param profile 067 * @see #getProfile() 068 */ 069 public void setProfile(FacebookProfile profile) { 070 _profile = profile; 071 } 072 073 /** 074 * 075 */ 076 public FBSummarizationTaskDetails(){ 077 setTaskType(TaskType.FACEBOOK_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_FBJ+"/"+service.tut.pori.contentanalysis.Definitions.METHOD_TASK_FINISHED; 093 } 094 095 /** 096 * @return the contentTypes 097 */ 098 public EnumSet<ContentType> getContentTypes() { 099 Map<String, String> metadata = getMetadata(); 100 if(metadata == null || metadata.isEmpty()){ 101 LOGGER.debug("No metadata."); 102 return null; 103 } 104 105 String[] types = StringUtils.split(metadata.get(METADATA_CONTENT_TYPES), core.tut.pori.http.Definitions.SEPARATOR_URI_QUERY_PARAM_VALUES); 106 if(types == null){ 107 LOGGER.debug("No content types."); 108 return null; 109 } 110 111 EnumSet<ContentType> ctypes = EnumSet.noneOf(ContentType.class); 112 ContentType[] tValues = ContentType.values(); 113 for(String type : types){ 114 for(ContentType t : tValues){ 115 if(t.name().equals(type)){ 116 ctypes.add(t); 117 } 118 } 119 } 120 121 if(ctypes.isEmpty()){ 122 LOGGER.warn("No valid content types."); 123 return null; 124 }else{ 125 return ctypes; 126 } 127 } 128 129 /** 130 * @param contentTypes the contentTypes to set 131 */ 132 public void setContentTypes(Set<ContentType> contentTypes) { 133 Map<String, String> metadata = getMetadata(); 134 if(contentTypes == null || contentTypes.isEmpty()){ 135 LOGGER.debug("No content types."); 136 if(metadata != null){ 137 metadata.remove(METADATA_CONTENT_TYPES); 138 if(metadata.isEmpty()){ 139 setMetadata(null); 140 } 141 } 142 return; 143 } 144 StringBuilder cb = new StringBuilder(); 145 for(ContentType t : contentTypes){ 146 cb.append(t.name()); 147 cb.append(core.tut.pori.http.Definitions.SEPARATOR_URI_QUERY_PARAM_VALUES); 148 } 149 if(metadata == null){ 150 metadata = new HashMap<>(1); 151 } 152 metadata.put(METADATA_CONTENT_TYPES, cb.substring(0, cb.length()-1)); 153 setMetadata(metadata); 154 } 155 156 /** 157 * 158 * @param synchronize 159 */ 160 public void setSynchronize(boolean synchronize){ 161 Map<String, String> metadata = getMetadata(); 162 if(metadata == null){ 163 LOGGER.debug("No metadata, creating metadata."); 164 metadata = new HashMap<>(1); 165 setMetadata(metadata); 166 } 167 metadata.put(METADATA_SYNCHRONIZE, BooleanUtils.toStringTrueFalse(synchronize)); 168 } 169 170 /** 171 * 172 * @return true if synchronization is enabled for the task 173 */ 174 public boolean isSynchronize(){ 175 Map<String, String> metadata = getMetadata(); 176 if(metadata == null || metadata.isEmpty()){ 177 LOGGER.debug("No metadata "+METADATA_SYNCHRONIZE+", returning default: false."); 178 return false; 179 } 180 return BooleanUtils.toBoolean(metadata.get(METADATA_SYNCHRONIZE)); 181 } 182 183 @Override 184 public TaskParameters getTaskParameters() { 185 return null; 186 } 187 188 @Override 189 public void setTaskParameters(TaskParameters parameters) { 190 throw new UnsupportedOperationException("Method not supported."); 191 } 192}