001/* 002 * Copyright 2018 Anyware Services 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 */ 016package org.ametys.cms.data.type.impl; 017 018import java.util.Arrays; 019import java.util.Optional; 020 021import org.apache.commons.lang3.ArrayUtils; 022 023import org.ametys.core.model.type.AbstractBooleanElementType; 024import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData; 025import org.ametys.plugins.repository.data.repositorydata.RepositoryData; 026import org.ametys.plugins.repository.data.type.RepositoryElementType; 027import org.ametys.runtime.model.exception.BadItemTypeException; 028 029/** 030 * Class for boolean type of elements stored in the repository 031 */ 032public class BooleanRepositoryElementType extends AbstractBooleanElementType implements RepositoryElementType<Boolean> 033{ 034 035 public Object read(RepositoryData parentData, String name) throws BadItemTypeException 036 { 037 if (!parentData.hasValue(name)) 038 { 039 return null; 040 } 041 042 if (!isCompatible(parentData, name)) 043 { 044 throw new BadItemTypeException("Try to get boolean value from the non boolean data '" + name + "' on '" + parentData + "'"); 045 } 046 047 if (parentData.isMultiple(name)) 048 { 049 return parentData.getBooleans(name); 050 } 051 else 052 { 053 return parentData.getBoolean(name); 054 } 055 } 056 057 public boolean hasNonEmptyValue(RepositoryData parentData, String name) throws BadItemTypeException 058 { 059 if (!parentData.hasValue(name)) 060 { 061 return false; 062 } 063 064 if (!isCompatible(parentData, name)) 065 { 066 throw new BadItemTypeException("Try to check boolean value from the non boolean data '" + name + "' on '" + parentData + "'"); 067 } 068 069 if (parentData.isMultiple(name)) 070 { 071 return parentData.getBooleans(name).length > 0; 072 } 073 else 074 { 075 return true; 076 } 077 } 078 079 public void write(ModifiableRepositoryData parentData, String name, Object value) throws BadItemTypeException 080 { 081 if (value == null) 082 { 083 throw new IllegalArgumentException("Try to set a null value into the " + getId() + " data '" + name + "' on '" + parentData + "'"); 084 } 085 else if (value instanceof Boolean) 086 { 087 parentData.setValue(name, (Boolean) value); 088 } 089 else if (value instanceof boolean[]) 090 { 091 parentData.setValues(name, ArrayUtils.toObject((boolean[]) value)); 092 } 093 else if (value instanceof Boolean[]) 094 { 095 Arrays.stream((Boolean[]) value) 096 .forEach(v -> Optional.ofNullable(v) 097 .orElseThrow(() -> new IllegalArgumentException("Try to set a null value into the multiple " + getId() + " data '" + name + "' on '" + parentData + "'"))); 098 099 parentData.setValues(name, (Boolean[]) value); 100 } 101 else 102 { 103 throw new BadItemTypeException("Try to set the non boolean value '" + value + "' to the boolean data '" + name + "' on '" + parentData + "'"); 104 } 105 } 106 107 public String getRepositoryDataType() 108 { 109 return RepositoryData.BOOLEAN_REPOSITORY_DATA_TYPE; 110 } 111}