1 package org.apache.maven.archiva.web.action.admin.repositories;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.opensymphony.xwork2.Preparable;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.database.ArchivaDAO;
29 import org.apache.maven.archiva.database.ArchivaDatabaseException;
30 import org.apache.maven.archiva.database.Constraint;
31 import org.apache.maven.archiva.database.ObjectNotFoundException;
32 import org.apache.maven.archiva.database.constraints.ArtifactsByRepositoryConstraint;
33 import org.apache.maven.archiva.database.constraints.RepositoryContentStatisticsByRepositoryConstraint;
34 import org.apache.maven.archiva.model.ArchivaArtifact;
35 import org.apache.maven.archiva.model.ArchivaProjectModel;
36 import org.apache.maven.archiva.model.RepositoryContentStatistics;
38 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
40 import org.codehaus.plexus.redback.role.RoleManagerException;
42 import java.io.IOException;
43 import java.util.List;
47 * DeleteManagedRepositoryAction
50 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="deleteManagedRepositoryAction" instantiation-strategy="per-lookup"
52 public class DeleteManagedRepositoryAction
53 extends AbstractManagedRepositoriesAction
56 private ManagedRepositoryConfiguration repository;
58 private String repoid;
61 * @plexus.requirement role-hint="jdo"
63 private ArchivaDAO archivaDAO;
67 if ( StringUtils.isNotBlank( repoid ) )
69 this.repository = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoid );
73 public String confirmDelete()
75 if ( StringUtils.isBlank( repoid ) )
77 addActionError( "Unable to delete managed repository: repository id was blank." );
84 public String deleteEntry()
86 return deleteRepository( false );
89 public String deleteContents()
91 return deleteRepository( true );
94 private String deleteRepository( boolean deleteContents )
96 ManagedRepositoryConfiguration existingRepository = repository;
97 if ( existingRepository == null )
99 addActionError( "A repository with that id does not exist" );
103 String result = SUCCESS;
107 Configuration configuration = archivaConfiguration.getConfiguration();
108 cleanupRepositoryData( existingRepository );
109 removeRepository( repoid, configuration );
110 result = saveConfiguration( configuration );
112 if ( result.equals( SUCCESS ) )
114 if ( deleteContents )
116 removeContents( existingRepository );
120 catch ( IOException e )
122 addActionError( "Unable to delete repository: " + e.getMessage() );
125 catch ( RoleManagerException e )
127 addActionError( "Unable to delete repository: " + e.getMessage() );
130 catch ( ArchivaDatabaseException e )
132 addActionError( "Unable to delete repositoy: " + e.getMessage() );
139 private void cleanupRepositoryData( ManagedRepositoryConfiguration cleanupRepository )
140 throws RoleManagerException, ArchivaDatabaseException
142 removeRepositoryRoles( cleanupRepository );
143 cleanupDatabase( cleanupRepository.getId() );
144 cleanupScanStats( cleanupRepository.getId() );
146 List<ProxyConnectorConfiguration> proxyConnectors = getProxyConnectors();
147 for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
149 if ( StringUtils.equals( proxyConnector.getSourceRepoId(), cleanupRepository.getId() ) )
151 archivaConfiguration.getConfiguration().removeProxyConnector( proxyConnector );
155 Map<String, List<String>> repoToGroupMap = archivaConfiguration.getConfiguration().getRepositoryToGroupMap();
156 if( repoToGroupMap != null )
158 if( repoToGroupMap.containsKey( cleanupRepository.getId() ) )
160 List<String> repoGroups = repoToGroupMap.get( cleanupRepository.getId() );
161 for( String repoGroup : repoGroups )
163 archivaConfiguration.getConfiguration().findRepositoryGroupById( repoGroup ).removeRepository( cleanupRepository.getId() );
169 private void cleanupDatabase( String repoId )
170 throws ArchivaDatabaseException
172 Constraint constraint = new ArtifactsByRepositoryConstraint( repoId );
174 List<ArchivaArtifact> artifacts = archivaDAO.getArtifactDAO().queryArtifacts( constraint );
176 for ( ArchivaArtifact artifact : artifacts )
178 getLogger().info( "Removing artifact " + artifact + " from the database." );
181 archivaDAO.getArtifactDAO().deleteArtifact( artifact );
183 ArchivaProjectModel projectModel =
184 archivaDAO.getProjectModelDAO().getProjectModel( artifact.getGroupId(), artifact.getArtifactId(),
185 artifact.getVersion() );
187 archivaDAO.getProjectModelDAO().deleteProjectModel( projectModel );
189 catch ( ObjectNotFoundException oe )
191 getLogger().info( "Project model of artifact " + artifact + " does not exist in the database. " +
192 "Moving on to the next artifact." );
194 catch ( ArchivaDatabaseException ae )
196 getLogger().info( "Unable to delete artifact " + artifact + " from the database. " +
197 "Moving on to the next artifact." );
202 private void cleanupScanStats( String repoId )
203 throws ArchivaDatabaseException
205 List<RepositoryContentStatistics> results =
206 archivaDAO.getRepositoryContentStatisticsDAO().queryRepositoryContentStatistics(
207 new RepositoryContentStatisticsByRepositoryConstraint( repoId ) );
209 for ( RepositoryContentStatistics stats : results )
211 archivaDAO.getRepositoryContentStatisticsDAO().deleteRepositoryContentStatistics( stats );
215 public ManagedRepositoryConfiguration getRepository()
220 public void setRepository( ManagedRepositoryConfiguration repository )
222 this.repository = repository;
225 public String getRepoid()
230 public void setRepoid( String repoid )
232 this.repoid = repoid;
235 public void setArchivaDAO( ArchivaDAO archivaDAO )
237 this.archivaDAO = archivaDAO;
240 public ArchivaDAO getArchivaDAO()