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.content.type; 017 018import java.util.Arrays; 019import java.util.Collections; 020import java.util.List; 021import java.util.stream.Collectors; 022 023import org.ametys.cms.content.references.OutgoingReferences; 024import org.ametys.cms.content.references.Referencer; 025import org.ametys.cms.data.Binary; 026import org.ametys.cms.data.ExplorerFile; 027import org.ametys.cms.data.type.impl.FileRepositoryElementType; 028 029/** 030 * Class for file type of elements used by contents 031 */ 032public class FileContentElementType extends FileRepositoryElementType implements Referencer 033{ 034 /** Constants for the explorer type for outgoing references */ 035 private static final String __OUTGOING_REFERENCE_TYPE = "explorer"; 036 037 public OutgoingReferences getOutgoingReferences(Object value) 038 { 039 if (value != null) 040 { 041 if (value instanceof String) 042 { 043 OutgoingReferences references = new OutgoingReferences(); 044 List<String> fileIds = Collections.singletonList((String) value); 045 references.put(__OUTGOING_REFERENCE_TYPE, fileIds); 046 return references; 047 } 048 else if (value instanceof String[]) 049 { 050 OutgoingReferences references = new OutgoingReferences(); 051 List<String> fileIds = Arrays.asList((String[]) value); 052 references.put(__OUTGOING_REFERENCE_TYPE, fileIds); 053 return references; 054 } 055 else if (value instanceof ExplorerFile) 056 { 057 OutgoingReferences references = new OutgoingReferences(); 058 List<String> fileIds = Collections.singletonList(((ExplorerFile) value).getResourceId()); 059 references.put(__OUTGOING_REFERENCE_TYPE, fileIds); 060 return references; 061 } 062 else if (value instanceof ExplorerFile[]) 063 { 064 OutgoingReferences references = new OutgoingReferences(); 065 List<String> fileIds = Arrays.stream((ExplorerFile[]) value) 066 .map(ExplorerFile::getResourceId) 067 .collect(Collectors.toList()); 068 references.put(__OUTGOING_REFERENCE_TYPE, fileIds); 069 return references; 070 } 071 else if (value instanceof Binary || value instanceof Binary[]) 072 { 073 return OutgoingReferences.EMPTY; 074 } 075 else 076 { 077 throw new IllegalArgumentException("Try to get outgoing references of the non file value '" + value + "'"); 078 } 079 } 080 else 081 { 082 return OutgoingReferences.EMPTY; 083 } 084 } 085}