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 protected abstract String getEntityType(); 045 046 /** 047 * Writes the item in a JsonGenerator. 048 */ 049 protected abstract void writeItem(JsonGenerator jg, T item) throws IOException; 050 051 @Override 052 public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { 053 if (!List.class.isAssignableFrom(type)) { 054 return false; 055 } 056 057 // Verify the generic argument type 058 if (genericType instanceof ParameterizedType) { 059 ParameterizedType paramType = (ParameterizedType) genericType; 060 Type actualTypeArguments = paramType.getActualTypeArguments()[0]; 061 if (type == null) { 062 throw new RuntimeException("Invalid class parameter type."); 063 } 064 return ((Class<?>) actualTypeArguments).isAssignableFrom(getItemClass()); 065 066 } 067 return false; 068 } 069 070 private Class<?> getItemClass() { 071 return (Class<?>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; 072 } 073 074 @Override 075 protected void writeEntityBody(JsonGenerator jg, List<T> list) throws IOException { 076 writePaginableHeader(jg, list); 077 writeHeader(jg, list); 078 jg.writeArrayFieldStart("entries"); 079 for (T item : list) { 080 writeItem(jg, item); 081 } 082 jg.writeEndArray(); 083 } 084 085 protected void writePaginableHeader(JsonGenerator jg, List<T> list) throws IOException { 086 if (list instanceof Paginable) { 087 @SuppressWarnings("rawtypes") 088 Paginable paginable = (Paginable) list; 089 jg.writeBooleanField("isPaginable", true); 090 jg.writeNumberField("resultsCount", paginable.getResultsCount()); 091 jg.writeNumberField("pageSize", paginable.getPageSize()); 092 jg.writeNumberField("maxPageSize", paginable.getMaxPageSize()); 093 jg.writeNumberField("currentPageSize", paginable.getCurrentPageSize()); 094 jg.writeNumberField("currentPageIndex", paginable.getCurrentPageIndex()); 095 jg.writeNumberField("numberOfPages", paginable.getNumberOfPages()); 096 jg.writeBooleanField("isPreviousPageAvailable", paginable.isPreviousPageAvailable()); 097 jg.writeBooleanField("isNextPageAvailable", paginable.isNextPageAvailable()); 098 jg.writeBooleanField("isLastPageAvailable", paginable.isLastPageAvailable()); 099 jg.writeBooleanField("isSortable", paginable.isSortable()); 100 jg.writeBooleanField("hasError", paginable.hasError()); 101 jg.writeStringField("errorMessage", paginable.getErrorMessage()); 102 if (paginable.hasAggregateSupport() && paginable.getAggregates() != null 103 && !paginable.getAggregates().isEmpty()) { 104 jg.writeObjectField("aggregations", paginable.getAggregates()); 105 } 106 } 107 } 108 109 /** 110 * Override this method to write into list header 111 */ 112 protected void writeHeader(JsonGenerator jg, List<T> list) throws IOException { 113 } 114 115}