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.holder.group.impl; 017 018import java.util.HashMap; 019import java.util.Map; 020import java.util.Optional; 021 022import org.ametys.cms.data.holder.IndexableDataHolder; 023import org.ametys.cms.data.holder.group.IndexableRepeater; 024import org.ametys.cms.data.holder.group.IndexableRepeaterEntry; 025import org.ametys.cms.data.holder.impl.DefaultModelAwareDataHolder; 026import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder; 027import org.ametys.plugins.repository.data.repositorydata.RepositoryData; 028import org.ametys.runtime.model.ModelItemGroup; 029import org.ametys.runtime.model.ViewItemAccessor; 030import org.ametys.runtime.model.exception.BadItemTypeException; 031import org.ametys.runtime.model.type.DataContext; 032 033/** 034 * Class for model aware repeater entries 035 */ 036public class DefaultModelAwareRepeaterEntry implements IndexableRepeaterEntry 037{ 038 /** The repository data of the repeater entry */ 039 protected RepositoryData _repositoryData; 040 041 /** the default implementation of a {@link ModelAwareDataHolder} to use */ 042 protected IndexableDataHolder _defaultDataHolder; 043 044 /** the repeater containing this entry */ 045 protected IndexableRepeater _repeater; 046 047 /** the definition of this repeater entry */ 048 protected ModelItemGroup _definition; 049 050 /** 051 * Creates a model aware repeater entry 052 * @param repositoryData the repository data of the repeater entry 053 * @param repeater the repeater containing this entry 054 * @param definition the definition of the repeater 055 */ 056 public DefaultModelAwareRepeaterEntry(RepositoryData repositoryData, IndexableRepeater repeater, ModelItemGroup definition) 057 { 058 _defaultDataHolder = new DefaultModelAwareDataHolder(repositoryData, Optional.of(repeater.getParentDataHolder()), Optional.of(repeater.getRootDataHolder()), definition); 059 _repositoryData = repositoryData; 060 _repeater = repeater; 061 _definition = definition; 062 } 063 064 public int getPosition() 065 { 066 return Integer.parseInt(_repositoryData.getName()); 067 } 068 069 public Object dataToJSON(String dataPath, DataContext context) 070 { 071 return _dataToJSON(Optional.of(dataPath), Optional.empty(), context, false); 072 } 073 074 public Map<String, Object> dataToJSON(ViewItemAccessor viewItemAccessor, DataContext context) throws BadItemTypeException 075 { 076 return _dataToJSON(Optional.empty(), Optional.of(viewItemAccessor), context, false); 077 } 078 079 public Map<String, Object> dataToJSONForEdition(ViewItemAccessor viewItemAccessor, DataContext context) throws BadItemTypeException 080 { 081 return _dataToJSON(Optional.empty(), Optional.of(viewItemAccessor), context, true); 082 } 083 084 private Map<String, Object> _dataToJSON(Optional<String> dataPath, Optional<ViewItemAccessor> viewItemAccessor, DataContext context, boolean isEdition) throws BadItemTypeException 085 { 086 Map<String, Object> entryValues = new HashMap<>(); 087 entryValues.put("position", getPosition()); 088 089 if (dataPath.isPresent()) 090 { 091 entryValues.put("value", IndexableRepeaterEntry.super.dataToJSON(dataPath.get(), context)); 092 } 093 else if (viewItemAccessor.isPresent()) 094 { 095 if (isEdition) 096 { 097 entryValues.put("values", IndexableRepeaterEntry.super.dataToJSONForEdition(viewItemAccessor.get(), context)); 098 } 099 else 100 { 101 entryValues.put("values", IndexableRepeaterEntry.super.dataToJSON(viewItemAccessor.get(), context)); 102 } 103 } 104 105 return entryValues; 106 } 107 108 @Override 109 public IndexableDataHolder getDefaultDataHolder() 110 { 111 return _defaultDataHolder; 112 } 113 114 @Override 115 public RepositoryData getRepositoryData() 116 { 117 return _repositoryData; 118 } 119 120 public IndexableRepeater getHoldingRepeater() 121 { 122 return _repeater; 123 } 124}