001/* 002 * Copyright 2019 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; 017 018import java.util.Arrays; 019import java.util.List; 020import java.util.Optional; 021import java.util.stream.Collectors; 022import java.util.stream.Stream; 023 024import org.apache.commons.lang.StringUtils; 025 026import org.ametys.cms.data.ContentValue; 027import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder; 028import org.ametys.runtime.model.exception.BadItemTypeException; 029 030/** 031 * Helper for data of type 'content' 032 */ 033public final class ContentDataHelper 034{ 035 private ContentDataHelper() 036 { 037 // Empty constructor 038 } 039 040 /** 041 * Retrieves the content identifier of a content data 042 * @param dataHolder data holder that contains the content data 043 * @param dataPath path to the content data 044 * @param defaultValue The default value to return 045 * @return the content identifier 046 * @throws BadItemTypeException if the data at the given path is not a content data 047 */ 048 public static String getContentIdFromContentData(ModelAwareDataHolder dataHolder, String dataPath, String defaultValue) throws BadItemTypeException 049 { 050 ContentValue value = dataHolder.getValue(dataPath); 051 return Optional.ofNullable(value) 052 .map(ContentValue::getContentId) 053 .orElse(defaultValue); 054 } 055 056 /** 057 * Retrieves the content identifier of a content data 058 * @param dataHolder data holder that contains the content data 059 * @param dataPath path to the content data 060 * @return the content identifier, empty string if it is invalid 061 * @throws BadItemTypeException if the data at the given path is not a content data 062 */ 063 public static String getContentIdFromContentData(ModelAwareDataHolder dataHolder, String dataPath) throws BadItemTypeException 064 { 065 return getContentIdFromContentData(dataHolder, dataPath, StringUtils.EMPTY); 066 } 067 068 /** 069 * Retrieves the content identifiers in an array from a multiple content data 070 * @param dataHolder data holder that contains the multiple content data 071 * @param dataPath path to the multiple content data 072 * @return an array containing the content identifiers 073 * @throws BadItemTypeException if the data at the given path is not a content data 074 */ 075 public static boolean isMultipleContentDataEmpty(ModelAwareDataHolder dataHolder, String dataPath) throws BadItemTypeException 076 { 077 return getContentIdsStreamFromMultipleContentData(dataHolder, dataPath).count() <= 0; 078 } 079 080 /** 081 * Retrieves the content identifiers in a {@link List} from a multiple content data 082 * @param dataHolder data holder that contains the multiple content data 083 * @param dataPath path to the multiple content data 084 * @return a {@link List} containing the content identifiers 085 * @throws BadItemTypeException if the data at the given path is not a content data 086 */ 087 public static List<String> getContentIdsListFromMultipleContentData(ModelAwareDataHolder dataHolder, String dataPath) throws BadItemTypeException 088 { 089 return getContentIdsStreamFromMultipleContentData(dataHolder, dataPath).collect(Collectors.toList()); 090 } 091 092 /** 093 * Retrieves the content identifiers in an array from a multiple content data 094 * @param dataHolder data holder that contains the multiple content data 095 * @param dataPath path to the multiple content data 096 * @return an array containing the content identifiers 097 * @throws BadItemTypeException if the data at the given path is not a content data 098 */ 099 public static String[] getContentIdsArrayFromMultipleContentData(ModelAwareDataHolder dataHolder, String dataPath) throws BadItemTypeException 100 { 101 return getContentIdsStreamFromMultipleContentData(dataHolder, dataPath).toArray(String[]::new); 102 } 103 104 /** 105 * Retrieves a {@link Stream} of the content identifiers from a multiple content data 106 * @param dataHolder data holder that contains the multiple content data 107 * @param dataPath path to the multiple content data 108 * @return a {@link Stream} of the content identifiers 109 * @throws BadItemTypeException if the data at the given path is not a content data 110 */ 111 public static Stream<String> getContentIdsStreamFromMultipleContentData(ModelAwareDataHolder dataHolder, String dataPath) throws BadItemTypeException 112 { 113 ContentValue[] value = dataHolder.getValue(dataPath, false, new ContentValue[0]); 114 return Optional.ofNullable(value) 115 .map(v -> Arrays.stream(v)) 116 .orElse(Stream.empty()) 117 .map(ContentValue::getContentId); 118 } 119}