001/* 002 * Copyright 2016 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.plugins.contentio.synchronize.impl; 017 018import java.util.Arrays; 019import java.util.HashMap; 020import java.util.HashSet; 021import java.util.List; 022import java.util.Map; 023import java.util.Set; 024 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.commons.lang.StringUtils; 028 029import org.ametys.core.util.JSONUtils; 030import org.ametys.plugins.contentio.synchronize.AbstractSynchronizableContentsCollection; 031import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection; 032 033/** 034 * Abstract implementation of {@link SynchronizableContentsCollection} to be synchronized with a data source 035 */ 036public abstract class AbstractDataSourceSynchronizableContentsCollection extends AbstractSynchronizableContentsCollection 037{ 038 /** Name of parameter holding the data source id */ 039 protected static final String __PARAM_DATASOURCE_ID = "datasourceId"; 040 /** Name of parameter holding the data source id */ 041 protected static final String __PARAM_FIELD_ID = "idField"; 042 /** Name of parameter holding the fields mapping */ 043 protected static final String __PARAM_MAPPING = "mapping"; 044 /** Name of parameter into mapping holding the synchronized property */ 045 protected static final String __PARAM_MAPPING_SYNCHRO = "synchro"; 046 /** Name of parameter into mapping holding the path of metadata */ 047 protected static final String __PARAM_MAPPING_METADATA_REF = "metadata-ref"; 048 /** Name of parameter into mapping holding the remote attribute */ 049 protected static final String __PARAM_MAPPING_ATTRIBUTE = "attribute"; 050 051 /** The JSON Utils */ 052 protected JSONUtils _jsonUtils; 053 054 @Override 055 public void service(ServiceManager smanager) throws ServiceException 056 { 057 super.service(smanager); 058 _jsonUtils = (JSONUtils) smanager.lookup(JSONUtils.ROLE); 059 } 060 061 @Override 062 public String getIdField() 063 { 064 return (String) _modelParamValues.get(__PARAM_FIELD_ID); 065 } 066 067 /** 068 * Get the id of data source 069 * @return The id of data source 070 */ 071 public String getDataSourceId() 072 { 073 return (String) _modelParamValues.get(__PARAM_DATASOURCE_ID); 074 } 075 076 public Set<String> getExternalOnlyFields() 077 { 078 Set<String> extFields = new HashSet<>(); 079 080 String mappingAsString = (String) getParameterValues().get(__PARAM_MAPPING); 081 if (StringUtils.isNotEmpty(mappingAsString)) 082 { 083 List<Object> mappingAsList = _jsonUtils.convertJsonToList(mappingAsString); 084 for (Object object : mappingAsList) 085 { 086 @SuppressWarnings("unchecked") 087 Map<String, Object> field = (Map<String, Object>) object; 088 boolean isSynchronized = field.containsKey(__PARAM_MAPPING_SYNCHRO) ? (Boolean) field.get(__PARAM_MAPPING_SYNCHRO) : false; 089 if (!isSynchronized) 090 { 091 extFields.add((String) field.get(__PARAM_MAPPING_METADATA_REF)); 092 } 093 } 094 } 095 096 return extFields; 097 } 098 099 @Override 100 public Set<String> getLocalAndExternalFields() 101 { 102 Set<String> syncFields = new HashSet<>(); 103 104 String mappingAsString = (String) getParameterValues().get(__PARAM_MAPPING); 105 if (StringUtils.isNotEmpty(mappingAsString)) 106 { 107 List<Object> mappingAsList = _jsonUtils.convertJsonToList(mappingAsString); 108 for (Object object : mappingAsList) 109 { 110 @SuppressWarnings("unchecked") 111 Map<String, Object> field = (Map<String, Object>) object; 112 boolean isSynchronized = field.containsKey(__PARAM_MAPPING_SYNCHRO) ? (Boolean) field.get(__PARAM_MAPPING_SYNCHRO) : false; 113 if (isSynchronized) 114 { 115 syncFields.add((String) field.get(__PARAM_MAPPING_METADATA_REF)); 116 } 117 } 118 } 119 120 return syncFields; 121 } 122 123 /** 124 * Get the field mapping 125 * @return The mapping 126 */ 127 public Map<String, List<String>> getMapping () 128 { 129 Map<String, List<String>> mapping = new HashMap<>(); 130 131 String mappingAsString = (String) getParameterValues().get(__PARAM_MAPPING); 132 if (StringUtils.isNotEmpty(mappingAsString)) 133 { 134 List<Object> mappingAsList = _jsonUtils.convertJsonToList(mappingAsString); 135 for (Object object : mappingAsList) 136 { 137 @SuppressWarnings("unchecked") 138 Map<String, Object> field = (Map<String, Object>) object; 139 String metadataRef = (String) field.get(__PARAM_MAPPING_METADATA_REF); 140 String[] attributes = ((String) field.get(__PARAM_MAPPING_ATTRIBUTE)).split(","); 141 142 mapping.put(metadataRef, Arrays.asList(attributes)); 143 } 144 } 145 146 return mapping; 147 } 148 149}