119c56471SVladimir Kotal /* 219c56471SVladimir Kotal * CDDL HEADER START 319c56471SVladimir Kotal * 419c56471SVladimir Kotal * The contents of this file are subject to the terms of the 519c56471SVladimir Kotal * Common Development and Distribution License (the "License"). 619c56471SVladimir Kotal * You may not use this file except in compliance with the License. 719c56471SVladimir Kotal * 819c56471SVladimir Kotal * See LICENSE.txt included in this distribution for the specific 919c56471SVladimir Kotal * language governing permissions and limitations under the License. 1019c56471SVladimir Kotal * 1119c56471SVladimir Kotal * When distributing Covered Code, include this CDDL HEADER in each 1219c56471SVladimir Kotal * file and include the License file at LICENSE.txt. 1319c56471SVladimir Kotal * If applicable, add the following below this CDDL HEADER, with the 1419c56471SVladimir Kotal * fields enclosed by brackets "[]" replaced with your own identifying 1519c56471SVladimir Kotal * information: Portions Copyright [yyyy] [name of copyright owner] 1619c56471SVladimir Kotal * 1719c56471SVladimir Kotal * CDDL HEADER END 1819c56471SVladimir Kotal */ 1919c56471SVladimir Kotal 2019c56471SVladimir Kotal /* 21*5d9f3aa0SAdam Hornáček * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 2219c56471SVladimir Kotal */ 2319c56471SVladimir Kotal package org.opengrok.web.util; 2419c56471SVladimir Kotal 2519c56471SVladimir Kotal import net.sf.cglib.beans.BeanGenerator; 2619c56471SVladimir Kotal import org.modelmapper.ModelMapper; 2719c56471SVladimir Kotal import org.opengrok.indexer.util.DTOElement; 2819c56471SVladimir Kotal 2919c56471SVladimir Kotal import java.lang.reflect.Field; 3019c56471SVladimir Kotal 3119c56471SVladimir Kotal public class DTOUtil { DTOUtil()3219c56471SVladimir Kotal private DTOUtil() { 3319c56471SVladimir Kotal // private to ensure static 3419c56471SVladimir Kotal } 3519c56471SVladimir Kotal 3619c56471SVladimir Kotal // ModelMapper is thread-safe and we only need to convert different object types for now 3719c56471SVladimir Kotal // so it should be safe to reuse its instance. 3819c56471SVladimir Kotal private static final ModelMapper modelMapper = new ModelMapper(); 3919c56471SVladimir Kotal 4019c56471SVladimir Kotal /** 4119c56471SVladimir Kotal * Generate Data Transfer Object from an object. Any field in the input object 4219c56471SVladimir Kotal * that is annotated with <code>DTOElement</code> will be brought along. 4319c56471SVladimir Kotal * @param object object to use as input 4419c56471SVladimir Kotal * @return DTO with values and generated getters/setters from input object 4519c56471SVladimir Kotal */ createDTO(Object object)4619c56471SVladimir Kotal public static Object createDTO(Object object) { 4719c56471SVladimir Kotal // ModelMapper assumes getters/setters so use BeanGenerator to provide them. 4819c56471SVladimir Kotal BeanGenerator beanGenerator = new BeanGenerator(); 4919c56471SVladimir Kotal for (Field field : object.getClass().getDeclaredFields()) { 5019c56471SVladimir Kotal if (field.isAnnotationPresent(DTOElement.class)) { 5119c56471SVladimir Kotal beanGenerator.addProperty(field.getName(), field.getType()); 5219c56471SVladimir Kotal } 5319c56471SVladimir Kotal } 5419c56471SVladimir Kotal Object bean = beanGenerator.create(); 5519c56471SVladimir Kotal 5619c56471SVladimir Kotal return modelMapper.map(object, bean.getClass()); 5719c56471SVladimir Kotal } 5819c56471SVladimir Kotal } 59