DimensionDTO.java

/*
 * Copyright 2025 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.genesys.server.api.v2.model.kpi;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.genesys.server.api.v2.model.AuditedVersionedDTO;
import org.genesys.server.api.v2.model.json.CustomTypeIdResolver;
import org.genesys.server.model.kpi.BooleanDimension;
import org.genesys.server.model.kpi.JpaDimension;
import org.genesys.server.model.kpi.NumericListDimension;
import org.genesys.server.model.kpi.StringListDimension;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.HashMap;
import java.util.Map;

@EqualsAndHashCode(callSuper = true)
@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "_class")
@JsonTypeIdResolver(DimensionDTO.DimensionDTOTypeIdResolver.class)
public class DimensionDTO extends AuditedVersionedDTO {

	private String _class;

	@NotNull
	@Size(max = 100)
	private String name;

	@NotNull
	@Size(max = 100)
	private String title;

	private Class<?> targetType;

	public static class DimensionDTOTypeIdResolver extends CustomTypeIdResolver {

		@Override
		protected Map<String, Class<?>> initClassesMap() {
			Map<String, Class<?>> classes = new HashMap<>();
			classes.put(BooleanDimension.class.getSimpleName(), BooleanDimensionDTO.class);
			classes.put(JpaDimension.class.getSimpleName(), JpaDimensionDTO.class);
			classes.put(StringListDimension.class.getSimpleName(), StringListDimensionDTO.class);
			classes.put(NumericListDimension.class.getSimpleName(), NumericListDimensionDTO.class);
			return classes;
		}
	}
}