001/* 002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others. 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 * 016 * Contributors: 017 * dmetzler 018 */ 019package org.nuxeo.ecm.automation.jaxrs.io; 020 021import java.io.IOException; 022import java.lang.annotation.Annotation; 023import java.lang.reflect.ParameterizedType; 024import java.lang.reflect.Type; 025import java.util.List; 026 027import javax.ws.rs.core.MediaType; 028 029import org.nuxeo.ecm.automation.core.util.Paginable; 030 031import com.fasterxml.jackson.core.JsonGenerator; 032 033/** 034 * Abstract class that knows how to serialize List of nuxeo entities. The implementing classes should only implement 035 * {@link #getEntityType()} and {@link #writeItem(JsonGenerator, Object)} 036 * 037 * @since 5.7.3 038 */ 039public abstract class EntityListWriter<T> extends EntityWriter<List<T>> { 040 041 /** 042 * Returns the entity-type value of the list (ie: users, groups....) 043 */ 044 @Override 045 protected abstract String getEntityType(); 046 047 /** 048 * Writes the item in a JsonGenerator. 049 */ 050 protected abstract void writeItem(JsonGenerator jg, T item) throws IOException; 051 052 @Override 053 public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { 054 if (!List.class.isAssignableFrom(type)) { 055 return false; 056 } 057 058 // Verify the generic argument type 059 if (genericType instanceof ParameterizedType) { 060 ParameterizedType paramType = (ParameterizedType) genericType; 061 Type actualTypeArguments = paramType.getActualTypeArguments()[0]; 062 if (type == null) { 063 throw new RuntimeException("Invalid class parameter type."); 064 } 065 return ((Class<?>) actualTypeArguments).isAssignableFrom(getItemClass()); 066 067 } 068 return false; 069 } 070 071 private Class<?> getItemClass() { 072 return (Class<?>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; 073 } 074 075 @Override 076 protected void writeEntityBody(JsonGenerator jg, List<T> list) throws IOException { 077 writePaginableHeader(jg, list); 078 writeHeader(jg, list); 079 jg.writeArrayFieldStart("entries"); 080 for (T item : list) { 081 writeItem(jg, item); 082 } 083 jg.writeEndArray(); 084 } 085 086 protected void writePaginableHeader(JsonGenerator jg, List<T> list) throws IOException { 087 if (list instanceof Paginable) { 088 @SuppressWarnings("rawtypes") 089 Paginable paginable = (Paginable) list; 090 jg.writeBooleanField("isPaginable", true); 091 jg.writeNumberField("resultsCount", paginable.getResultsCount()); 092 jg.writeNumberField("pageSize", paginable.getPageSize()); 093 jg.writeNumberField("maxPageSize", paginable.getMaxPageSize()); 094 jg.writeNumberField("currentPageSize", paginable.getCurrentPageSize()); 095 jg.writeNumberField("currentPageIndex", paginable.getCurrentPageIndex()); 096 jg.writeNumberField("numberOfPages", paginable.getNumberOfPages()); 097 jg.writeBooleanField("isPreviousPageAvailable", paginable.isPreviousPageAvailable()); 098 jg.writeBooleanField("isNextPageAvailable", paginable.isNextPageAvailable()); 099 jg.writeBooleanField("isLastPageAvailable", paginable.isLastPageAvailable()); 100 jg.writeBooleanField("isSortable", paginable.isSortable()); 101 jg.writeBooleanField("hasError", paginable.hasError()); 102 jg.writeStringField("errorMessage", paginable.getErrorMessage()); 103 if (paginable.hasAggregateSupport() && paginable.getAggregates() != null 104 && !paginable.getAggregates().isEmpty()) { 105 jg.writeObjectField("aggregations", paginable.getAggregates()); 106 } 107 } 108 } 109 110 /** 111 * Override this method to write into list header 112 */ 113 protected void writeHeader(JsonGenerator jg, List<T> list) throws IOException { 114 } 115 116}