View Javadoc

1   /*
2    * #%L
3    * Bitrepository Modifying Client
4    * 
5    * $Id$
6    * $HeadURL$
7    * %%
8    * Copyright (C) 2010 - 2011 The State and University Library, The Royal Library and The State Archives, Denmark
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as 
12   * published by the Free Software Foundation, either version 2.1 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Lesser Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
23   * #L%
24   */
25  package org.bitrepository.modify.deletefile.conversation;
26  
27  import java.util.Collection;
28  import org.bitrepository.bitrepositorymessages.DeleteFileFinalResponse;
29  import org.bitrepository.bitrepositorymessages.DeleteFileRequest;
30  import org.bitrepository.bitrepositorymessages.MessageResponse;
31  import org.bitrepository.client.conversation.ConversationContext;
32  import org.bitrepository.client.conversation.PerformingOperationState;
33  import org.bitrepository.client.conversation.selector.SelectedComponentInfo;
34  import org.bitrepository.client.exceptions.UnexpectedResponseException;
35  import org.bitrepository.common.utils.ChecksumUtils;
36  
37  /**
38   * Models the behavior of a DeleteFile conversation during the operation phase. That is, it begins with the sending of
39   * <code>DeleteFileRequest</code> messages and finishes with the reception of the <code>DeleteFileFinalResponse</code>
40   * messages from all responding pillars. 
41   *
42   * Note that this is only used by the DeleteFileConversation in the same package, therefore the visibility is package 
43   * protected.
44   */
45  public class DeletingFile extends PerformingOperationState {
46      private final DeleteFileConversationContext context;
47  
48      /*
49       * @param context The conversation context.
50       * @param contributors The list of components the fileIDs should be collected from.
51       */
52      public DeletingFile(DeleteFileConversationContext context, Collection<SelectedComponentInfo> contributors) {
53          super(contributors);
54          this.context = context;
55      }
56  
57      @Override
58      protected void generateContributorCompleteEvent(MessageResponse msg) throws UnexpectedResponseException {
59          DeleteFileFinalResponse response = (DeleteFileFinalResponse) msg;
60          getContext().getMonitor().contributorComplete(new DeleteFileCompletePillarEvent(
61                  response.getPillarID(), response.getCollectionID(), response.getChecksumDataForExistingFile()));
62      }
63  
64      @Override
65      protected void sendRequest() {
66          context.getMonitor().requestSent("Sending request for deleting file", activeContributors.keySet().toString());
67          for(String pillar : activeContributors.keySet()) {
68              DeleteFileRequest msg = createRequest(pillar);
69              if (context.getChecksumRequestForValidation() != null) {
70                  if (!context.isChecksumPillar(pillar) ||
71                          context.getChecksumRequestForValidation().equals(ChecksumUtils.getDefault(context.getSettings()))) {
72                      msg.setChecksumRequestForExistingFile(context.getChecksumRequestForValidation());
73                  }
74              }
75              msg.setPillarID(pillar);
76              msg.setDestination(activeContributors.get(pillar));
77              context.getMessageSender().sendMessage(msg);
78          }
79      }
80  
81      /**
82       * Will create a PutFileRequest based on the context. The ChecksumRequestForNewFile parameter is not added as this
83       * should only be added in case of full pillars.
84       */
85      private DeleteFileRequest createRequest(String pillar) {
86          DeleteFileRequest request = new DeleteFileRequest();
87          initializeMessage(request);
88          request.setFileID(context.getFileID());
89          request.setChecksumDataForExistingFile(context.getChecksumForValidationAtPillar());
90          request.setPillarID(pillar);
91          request.setDestination(activeContributors.get(pillar));
92          return request;
93      }
94  
95  
96      @Override
97      protected ConversationContext getContext() {
98          return context;
99      }
100 
101     @Override
102     protected String getPrimitiveName() {
103         return "DeleteFile";
104     }
105 }