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.core.migration.version; 017 018import java.time.Instant; 019import java.util.HashMap; 020import java.util.Map; 021 022import org.apache.commons.lang3.StringUtils; 023 024import org.ametys.core.migration.action.data.ActionData; 025import org.ametys.core.migration.handler.VersionHandler; 026 027/** 028 * Representation of a version 029 * Each upgrade create a new version 030 */ 031public abstract class AbstractVersion implements Version 032{ 033 private String _versionHandlerId; 034 private String _componentId; 035 private String _versionNumber; 036 private Instant _executionInstant; 037 private String _comment; 038 private Map<String, Object> _additionalValues; 039 040 /** 041 * Create a version to be stored 042 * @param versionHandlerId id of the {@link VersionHandler} 043 * @param componentId id of the component 044 */ 045 protected AbstractVersion(String versionHandlerId, String componentId) 046 { 047 this(versionHandlerId, componentId, null, null, null); 048 } 049 050 /** 051 * Create a version to be stored 052 * @param versionHandlerId id of the {@link VersionHandler} 053 * @param componentId id of the component 054 * @param versionNumber version number of the upgrade that created this version 055 * @param executionInstant time of the application of this version 056 * @param comment comment stored about this version 057 */ 058 protected AbstractVersion(String versionHandlerId, String componentId, String versionNumber, Instant executionInstant, String comment) 059 { 060 _versionHandlerId = versionHandlerId; 061 _componentId = componentId; 062 _versionNumber = versionNumber; 063 _executionInstant = executionInstant; 064 _comment = comment; 065 _additionalValues = new HashMap<>(); 066 } 067 068 /** 069 * Create a version from the data of an {@link ActionData} and its associated version. 070 * @param actionData The action data 071 */ 072 protected AbstractVersion(ActionData actionData) 073 { 074 this( 075 actionData.getVersion().getVersionHandlerId(), 076 actionData.getVersion().getComponentId(), 077 actionData.getVersionNumber(), 078 null, 079 actionData.getComment() 080 ); 081 } 082 083 /** 084 * Build a readable log for this Version 085 * @return a readable log 086 */ 087 @Override 088 public String toString() 089 { 090 String log = "Version '" + _versionNumber + "' for component '" + _componentId + "'"; 091 if (StringUtils.isNotBlank(_comment)) 092 { 093 log += " (" + _comment + ")"; 094 } 095 return log; 096 } 097 098 @Override 099 public String getVersionHandlerId() 100 { 101 return _versionHandlerId; 102 } 103 104 @Override 105 public String getComponentId() 106 { 107 return _componentId; 108 } 109 110 @Override 111 public String getVersionNumber() 112 { 113 return _versionNumber; 114 } 115 116 @Override 117 public Instant getExecutionInstant() 118 { 119 return _executionInstant; 120 } 121 122 @Override 123 public String getComment() 124 { 125 return _comment; 126 } 127 128 @Override 129 public void setVersionNumber(String versionNumber) 130 { 131 _versionNumber = versionNumber; 132 } 133 134 @Override 135 public void setExecutionInstant(Instant executionInstant) 136 { 137 _executionInstant = executionInstant; 138 } 139 140 @Override 141 public void setComment(String comment) 142 { 143 _comment = comment; 144 } 145 146 @Override 147 public Object addAdditionalValue(String key, Object value) 148 { 149 return _additionalValues.put(key, value); 150 } 151 152 @Override 153 public Map<String, Object> getAdditionalValues() 154 { 155 return _additionalValues; 156 } 157}