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