001/* 002 * Copyright 2020 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.archive; 017 018import java.util.LinkedHashMap; 019import java.util.Map; 020 021import org.ametys.runtime.i18n.I18nizableText; 022import org.ametys.runtime.model.Enumerator; 023import org.ametys.runtime.model.StaticEnumerator; 024 025/** 026 * The policy to apply when trying to import an object with same identifier as an existing object. 027 * <br>This is the view of the different policies of merge available to the application, for the business logic, see {@link Merger} (and its implementations in {@link Mergers}). 028 */ 029public enum MergePolicy 030{ 031 /** Delete all existing objects before the import process */ 032 DELETE_BEFORE 033 { 034 @Override 035 public Merger getMerger() 036 { 037 return Mergers.DELETE_BEFORE; 038 } 039 }, 040 041 /** Ignore the objects that already exist */ 042 IGNORE 043 { 044 @Override 045 public Merger getMerger() 046 { 047 return Mergers.IGNORE; 048 } 049 }, 050 /** Replace the objects that already exist */ 051 REPLACE 052 { 053 @Override 054 public Merger getMerger() 055 { 056 return Mergers.REPLACE; 057 } 058 }, 059 060 /** Fail when an object try to be imported but already exist */ 061 FAIL 062 { 063 @Override 064 public Merger getMerger() 065 { 066 return Mergers.FAIL; 067 } 068 }; 069 070 static Map<String, MergePolicy> _ids = new LinkedHashMap<>(); 071 static 072 { 073 for (MergePolicy mergePolicy : MergePolicy.values()) 074 { 075 _ids.put(mergePolicy._id, mergePolicy); 076 } 077 } 078 079 private String _id; 080 private I18nizableText _label; 081 private I18nizableText _description; 082 083 private MergePolicy() 084 { 085 final String catalogue = "plugin.contentio"; 086 String label = String.format("PLUGINS_CONTENTIO_ARCHIVE_IMPORT_SCHEDULABLE_PARAM_MERGE_POLICY_OPTION_%s", name()); 087 _label = new I18nizableText(catalogue, label); 088 String desc = String.format("PLUGINS_CONTENTIO_ARCHIVE_IMPORT_SCHEDULABLE_PARAM_MERGE_POLICY_OPTION_%s_DESC", name()); 089 _description = new I18nizableText(catalogue, desc); 090 _setId(); 091 } 092 093 private void _setId() 094 { 095 _id = name() 096 .toLowerCase() 097 .replace('_', '-'); 098 } 099 100 /** 101 * Gets the {@link Merger} 102 * @return the {@link Merger} 103 */ 104 public abstract Merger getMerger(); 105 106 static MergePolicy forId(String id) 107 { 108 return _ids.computeIfAbsent(id, MergePolicy::_throwIllegalId); 109 } 110 111 private static MergePolicy _throwIllegalId(String id) 112 { 113 throw new IllegalArgumentException(String.format("Wrong MergePolicy id: '%s'", id)); 114 } 115 116 /** 117 * Gets the id 118 * @return the id 119 */ 120 public String getId() 121 { 122 return _id; 123 } 124 125 Map<String, Object> toJson() 126 { 127 return Map.of( 128 "value", _id, 129 "label", _label, 130 "desc", _description); 131 } 132 133 /** 134 * {@link Enumerator} for listing merge policies for import. 135 */ 136 public static class MergePolicyEnumerator extends StaticEnumerator<String> 137 { 138 /** 139 * Constructor 140 */ 141 public MergePolicyEnumerator() 142 { 143 super(); 144 MergePolicy._ids.forEach((id, mergePolicy) -> add(mergePolicy._label, id)); 145 } 146 } 147}