1 package org.apache.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 org.apache.commons.lang.StringUtils;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.Configuration;
25 import org.apache.archiva.configuration.IndeterminateConfigurationException;
26 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
27 import org.springframework.context.annotation.Scope;
28 import org.springframework.stereotype.Controller;
30 import javax.inject.Inject;
31 import java.util.List;
34 * SortRepositoriesAction
35 * FIXME remove access to archivaconfiguration
37 @Controller( "sortRepositoriesAction" )
39 public class SortRepositoriesAction
40 extends AbstractRepositoriesAdminAction
43 private String repoGroupId;
45 private String targetRepo;
48 protected ArchivaConfiguration archivaConfiguration;
50 public String sortDown()
52 Configuration config = archivaConfiguration.getConfiguration();
54 List<String> repositories = getRepositoriesFromGroup();
56 int idx = findTargetRepository( repositories, targetRepo );
58 if ( idx >= 0 && validIndex( repositories, idx + 1 ) )
60 repositories.remove( idx );
61 repositories.add( idx + 1, targetRepo );
64 return saveConfiguration( config );
67 public String sortUp()
69 Configuration config = archivaConfiguration.getConfiguration();
71 List<String> repositories = getRepositoriesFromGroup();
73 int idx = findTargetRepository( repositories, targetRepo );
75 if ( idx >= 0 && validIndex( repositories, idx - 1 ) )
77 repositories.remove( idx );
78 repositories.add( idx - 1, targetRepo );
81 return saveConfiguration( config );
85 * Save the configuration.
87 * @param configuration the configuration to save.
88 * @return the webwork result code to issue.
89 * @throws java.io.IOException thrown if unable to save file to disk.
90 * @throws org.apache.archiva.configuration.InvalidConfigurationException thrown if configuration is invalid.
91 * @throws org.apache.archiva.redback.components.registry.RegistryException thrown if configuration subsystem has a problem saving the configuration to disk.
93 protected String saveConfiguration( Configuration configuration )
97 archivaConfiguration.save( configuration );
98 addActionMessage( "Successfully saved configuration" );
100 catch ( IndeterminateConfigurationException e )
102 addActionError( e.getMessage() );
105 catch ( org.apache.archiva.redback.components.registry.RegistryException e )
107 addActionError( "Configuration Registry Exception: " + e.getMessage() );
114 public String getRepoGroupId()
119 public void setRepoGroupId( String repoGroupId )
121 this.repoGroupId = repoGroupId;
124 public String getTargetRepo()
129 public void setTargetRepo( String targetRepo )
131 this.targetRepo = targetRepo;
134 private int findTargetRepository( List<String> repositories, String targetRepository )
138 for ( int i = 0; i < repositories.size(); i++ )
140 if ( StringUtils.equals( targetRepository, repositories.get( i ) ) )
149 private List<String> getRepositoriesFromGroup()
151 Configuration config = archivaConfiguration.getConfiguration();
152 RepositoryGroupConfiguration repoGroup = config.findRepositoryGroupById( repoGroupId );
153 return repoGroup.getRepositories();
156 private boolean validIndex( List<String> repositories, int idx )
158 return ( idx >= 0 ) && ( idx < repositories.size() );
161 public ArchivaConfiguration getArchivaConfiguration()
163 return archivaConfiguration;
166 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
168 this.archivaConfiguration = archivaConfiguration;