001package org.apache.archiva.metadata.repository.cassandra.model; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.metadata.repository.cassandra.CassandraUtils; 023 024import java.io.Serializable; 025import java.util.Date; 026import java.util.HashMap; 027import java.util.Map; 028 029import static org.apache.archiva.metadata.repository.cassandra.model.ColumnNames.*; 030 031/** 032 * Cassandra storage model for {@link org.apache.archiva.metadata.model.ArtifactMetadata} 033 * 034 * @author Olivier Lamy 035 * @since 2.0.0 036 */ 037public class ArtifactMetadataModel 038 implements Serializable 039{ 040 041 public final static String[] COLUMNS = new String[] { ID.toString(), REPOSITORY_NAME.toString(), 042 NAMESPACE_ID.toString(), PROJECT.toString(), PROJECT_VERSION.toString(), VERSION.toString(), 043 FILE_LAST_MODIFIED.toString(), SIZE.toString(), MD5.toString(), SHA1.toString(), WHEN_GATHERED.toString() }; 044 045 private String id; 046 047 private String repositoryId; 048 049 private String namespace; 050 051 private String project; 052 053 private String projectVersion; 054 055 private String version; 056 057 private long fileLastModified; 058 059 private long size; 060 061 private String md5; 062 063 private String sha1; 064 065 private long whenGathered; 066 067 private Map<String, String> checksums = new HashMap<>(); 068 069 public ArtifactMetadataModel() 070 { 071 // no op 072 } 073 074 public ArtifactMetadataModel( String id, String repositoryId, String namespace, String project, 075 String projectVersion, String version, Date fileLastModified, long size, String md5, 076 String sha1, Date whenGathered ) 077 { 078 this.id = id; 079 this.repositoryId = repositoryId; 080 this.namespace = namespace; 081 this.project = project; 082 this.projectVersion = projectVersion; 083 this.version = version; 084 this.fileLastModified = ( fileLastModified != null ? fileLastModified.getTime() : 0 ); 085 this.size = size; 086 this.md5 = md5; 087 this.sha1 = sha1; 088 this.whenGathered = whenGathered != null ? whenGathered.getTime() : new Date().getTime(); 089 } 090 091 092 public String getId() 093 { 094 return id; 095 } 096 097 public void setId( String id ) 098 { 099 this.id = id; 100 } 101 102 public String getRepositoryId() 103 { 104 return repositoryId; 105 } 106 107 public void setRepositoryId( String repositoryId ) 108 { 109 this.repositoryId = repositoryId; 110 } 111 112 public String getNamespace() 113 { 114 return namespace; 115 } 116 117 public void setNamespace( String namespace ) 118 { 119 this.namespace = namespace; 120 } 121 122 public String getProject() 123 { 124 return project; 125 } 126 127 public void setProject( String project ) 128 { 129 this.project = project; 130 } 131 132 public String getProjectVersion() 133 { 134 return projectVersion; 135 } 136 137 public void setProjectVersion( String projectVersion ) 138 { 139 this.projectVersion = projectVersion; 140 } 141 142 public String getVersion() 143 { 144 return version; 145 } 146 147 public void setVersion( String version ) 148 { 149 this.version = version; 150 } 151 152 public long getFileLastModified() 153 { 154 return fileLastModified; 155 } 156 157 public void setFileLastModified( long fileLastModified ) 158 { 159 this.fileLastModified = fileLastModified; 160 } 161 162 public long getSize() 163 { 164 return size; 165 } 166 167 public void setSize( long size ) 168 { 169 this.size = size; 170 } 171 172 public String getMd5() 173 { 174 return md5; 175 } 176 177 public void setMd5( String md5 ) 178 { 179 this.md5 = md5; 180 } 181 182 public String getSha1() 183 { 184 return sha1; 185 } 186 187 public void setSha1( String sha1 ) 188 { 189 this.sha1 = sha1; 190 } 191 192 public Date getWhenGathered() 193 { 194 return new Date( whenGathered ); 195 } 196 197 public void setWhenGathered( long whenGathered ) 198 { 199 this.whenGathered = whenGathered; 200 } 201 202 public void setChecksum(String type, String value) { 203 this.checksums.put(type, value); 204 } 205 206 public String getChecksum(String type) { 207 return this.checksums.get(type); 208 } 209 210 public void setChecksums(Map<String,String> checksums) { 211 this.checksums = checksums; 212 } 213 214 public Map<String,String> getChecksums() { 215 return this.checksums; 216 } 217 218 219 @Override 220 public String toString() 221 { 222 final StringBuilder sb = new StringBuilder( "ArtifactMetadataModel{" ); 223 sb.append( ", id='" ).append( id ).append( '\'' ); 224 sb.append( ", repositoryId='" ).append( repositoryId ).append( '\'' ); 225 sb.append( ", namespace='" ).append( namespace ).append( '\'' ); 226 sb.append( ", project='" ).append( project ).append( '\'' ); 227 sb.append( ", projectVersion='" ).append( projectVersion ).append( '\'' ); 228 sb.append( ", version='" ).append( version ).append( '\'' ); 229 sb.append( ", fileLastModified=" ).append( fileLastModified ); 230 sb.append( ", size=" ).append( size ); 231 sb.append( ", md5='" ).append( md5 ).append( '\'' ); 232 sb.append( ", sha1='" ).append( sha1 ).append( '\'' ); 233 sb.append( ", whenGathered=" ).append( whenGathered ); 234 sb.append( '}' ); 235 return sb.toString(); 236 } 237 238 public static class KeyBuilder 239 { 240 241 private String project; 242 243 private String id; 244 245 private String namespaceId; 246 247 private String repositoryId; 248 249 private String projectVersion; 250 251 public KeyBuilder() 252 { 253 254 } 255 256 public KeyBuilder withId( String id ) 257 { 258 this.id = id; 259 return this; 260 } 261 262 263 public KeyBuilder withNamespace( Namespace namespace ) 264 { 265 this.namespaceId = namespace.getName(); 266 this.repositoryId = namespace.getRepository().getName(); 267 return this; 268 } 269 270 public KeyBuilder withNamespace( String namespaceId ) 271 { 272 this.namespaceId = namespaceId; 273 return this; 274 } 275 276 public KeyBuilder withProject( String project ) 277 { 278 this.project = project; 279 return this; 280 } 281 282 public KeyBuilder withProjectVersion( String projectVersion ) 283 { 284 this.projectVersion = projectVersion; 285 return this; 286 } 287 288 public KeyBuilder withRepositoryId( String repositoryId ) 289 { 290 this.repositoryId = repositoryId; 291 return this; 292 } 293 294 public String build() 295 { 296 //repositoryId + namespaceId + project + projectVersion + id 297 // FIXME add some controls 298 299 String str = 300 CassandraUtils.generateKey( this.repositoryId, this.namespaceId, this.project, this.projectVersion, 301 this.id ); 302 303 //return Long.toString( str.hashCode() ); 304 return str; 305 } 306 } 307 308}