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.xwork.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
49 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
51 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="deleteManagedRepositoryAction"
53 public class DeleteManagedRepositoryAction
54 extends AbstractManagedRepositoriesAction
57 private ManagedRepositoryConfiguration repository;
59 private String repoid;
62 * @plexus.requirement role-hint="jdo"
64 private ArchivaDAO archivaDAO;
68 if ( StringUtils.isNotBlank( repoid ) )
70 this.repository = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoid );
74 public String confirmDelete()
76 if ( StringUtils.isBlank( repoid ) )
78 addActionError( "Unable to delete managed repository: repository id was blank." );
85 public String deleteEntry()
87 return deleteRepository( false );
90 public String deleteContents()
92 return deleteRepository( true );
95 private String deleteRepository( boolean deleteContents )
97 ManagedRepositoryConfiguration existingRepository = repository;
98 if ( existingRepository == null )
100 addActionError( "A repository with that id does not exist" );
104 String result = SUCCESS;
108 Configuration configuration = archivaConfiguration.getConfiguration();
109 cleanupRepositoryData( existingRepository );
110 removeRepository( repoid, configuration );
111 result = saveConfiguration( configuration );
113 if ( result.equals( SUCCESS ) )
115 if ( deleteContents )
117 removeContents( existingRepository );
121 catch ( IOException e )
123 addActionError( "Unable to delete repository: " + e.getMessage() );
126 catch ( RoleManagerException e )
128 addActionError( "Unable to delete repository: " + e.getMessage() );
131 catch ( ArchivaDatabaseException e )
133 addActionError( "Unable to delete repositoy: " + e.getMessage() );
140 private void cleanupRepositoryData( ManagedRepositoryConfiguration cleanupRepository )
141 throws RoleManagerException, ArchivaDatabaseException
143 removeRepositoryRoles( cleanupRepository );
144 cleanupDatabase( cleanupRepository.getId() );
145 cleanupScanStats( cleanupRepository.getId() );
147 List<ProxyConnectorConfiguration> proxyConnectors = getProxyConnectors();
148 for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
150 if ( StringUtils.equals( proxyConnector.getSourceRepoId(), cleanupRepository.getId() ) )
152 archivaConfiguration.getConfiguration().removeProxyConnector( proxyConnector );
156 Map<String, List<String>> repoToGroupMap = archivaConfiguration.getConfiguration().getRepositoryToGroupMap();
157 if( repoToGroupMap != null )
159 if( repoToGroupMap.containsKey( cleanupRepository.getId() ) )
161 List<String> repoGroups = repoToGroupMap.get( cleanupRepository.getId() );
162 for( String repoGroup : repoGroups )
164 archivaConfiguration.getConfiguration().findRepositoryGroupById( repoGroup ).removeRepository( cleanupRepository.getId() );
170 private void cleanupDatabase( String repoId )
171 throws ArchivaDatabaseException
173 Constraint constraint = new ArtifactsByRepositoryConstraint( repoId );
175 List<ArchivaArtifact> artifacts = archivaDAO.getArtifactDAO().queryArtifacts( constraint );
177 for ( ArchivaArtifact artifact : artifacts )
179 getLogger().info( "Removing artifact " + artifact + " from the database." );
182 archivaDAO.getArtifactDAO().deleteArtifact( artifact );
184 ArchivaProjectModel projectModel =
185 archivaDAO.getProjectModelDAO().getProjectModel( artifact.getGroupId(), artifact.getArtifactId(),
186 artifact.getVersion() );
188 archivaDAO.getProjectModelDAO().deleteProjectModel( projectModel );
190 catch ( ObjectNotFoundException oe )
192 getLogger().info( "Project model of artifact " + artifact + " does not exist in the database. " +
193 "Moving on to the next artifact." );
195 catch ( ArchivaDatabaseException ae )
197 getLogger().info( "Unable to delete artifact " + artifact + " from the database. " +
198 "Moving on to the next artifact." );
203 private void cleanupScanStats( String repoId )
204 throws ArchivaDatabaseException
206 List<RepositoryContentStatistics> results =
207 archivaDAO.getRepositoryContentStatisticsDAO().queryRepositoryContentStatistics(
208 new RepositoryContentStatisticsByRepositoryConstraint( repoId ) );
210 for ( RepositoryContentStatistics stats : results )
212 archivaDAO.getRepositoryContentStatisticsDAO().deleteRepositoryContentStatistics( stats );
216 public ManagedRepositoryConfiguration getRepository()
221 public void setRepository( ManagedRepositoryConfiguration repository )
223 this.repository = repository;
226 public String getRepoid()
231 public void setRepoid( String repoid )
233 this.repoid = repoid;
236 public void setArchivaDAO( ArchivaDAO archivaDAO )
238 this.archivaDAO = archivaDAO;
241 public ArchivaDAO getArchivaDAO()