View Javadoc

1   /*
2    * #%L
3    * Bitrepository Access
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 org.bitrepository.bitrepositoryelements.ResponseCode;
28  import org.bitrepository.bitrepositorymessages.IdentifyPillarsForDeleteFileRequest;
29  import org.bitrepository.bitrepositorymessages.IdentifyPillarsForDeleteFileResponse;
30  import org.bitrepository.bitrepositorymessages.MessageResponse;
31  import org.bitrepository.client.conversation.ConversationContext;
32  import org.bitrepository.client.conversation.GeneralConversationState;
33  import org.bitrepository.client.conversation.IdentifyingState;
34  import org.bitrepository.common.exceptions.UnableToFinishException;
35  
36  /**
37   * Handles the identification of the deleteFile Contributors.
38   */
39  public class IdentifyPillarsForDeleteFile extends IdentifyingState {
40      private final DeleteFileConversationContext context;
41  
42      public IdentifyPillarsForDeleteFile(DeleteFileConversationContext context) {
43          super(context.getContributors());
44          this.context = context;
45      }
46  
47      @Override
48      public GeneralConversationState getOperationState() {
49          return new DeletingFile(context, getSelector().getSelectedComponents());
50      }
51  
52      /**
53       * Extends the default behaviour with a idempotent aspects. This assumes that the delete on a pillar is successful if
54       * if the file is already absent.
55       *
56       * Any other none-positive response is handled as a fatal problem.
57       */
58      @Override
59      protected void handleFailureResponse(MessageResponse msg) throws UnableToFinishException {
60          IdentifyPillarsForDeleteFileResponse response = (IdentifyPillarsForDeleteFileResponse) msg;
61          ResponseCode responseCode = response.getResponseInfo().getResponseCode();
62          if(responseCode.equals(ResponseCode.FILE_NOT_FOUND_FAILURE)) {
63              //Idempotent
64              getContext().getMonitor().contributorIdentified(response);
65              getContext().getMonitor().contributorComplete(new DeleteFileCompletePillarEvent(
66                      response.getFrom(), response.getCollectionID(), null ));
67          } else {
68              getContext().getMonitor().contributorFailed(
69                      msg.getResponseInfo().getResponseText(), msg.getFrom(), 
70                      msg.getResponseInfo().getResponseCode());
71              throw new UnableToFinishException("Can not continue with delete operation, as " + msg.getFrom() +
72                      " is unable to perform the deletion.");
73          }
74      }
75  
76      @Override
77      protected void sendRequest() {
78          IdentifyPillarsForDeleteFileRequest msg = new IdentifyPillarsForDeleteFileRequest();
79          initializeMessage(msg);
80          msg.setFileID(context.getFileID());
81          msg.setDestination(context.getSettings().getCollectionDestination());
82          context.getMessageSender().sendMessage(msg);
83          context.getMonitor().identifyRequestSent("Identifying pillars for delete file");
84      }
85  
86      @Override
87      protected ConversationContext getContext() {
88          return context;
89      }
90  
91      @Override
92      protected String getPrimitiveName() {
93          return "IdentifyPillarsForDeleteFile";
94      }
95  
96      @Override
97      protected boolean canFinish() {
98          return (getOutstandingComponents().isEmpty());
99      }
100 
101     @Override
102     protected void checkForChecksumPillar(MessageResponse msg) {
103         IdentifyPillarsForDeleteFileResponse response = (IdentifyPillarsForDeleteFileResponse) msg;
104         if (response.getPillarChecksumSpec() != null) {
105             context.addChecksumPillar(response.getPillarID());
106         }
107     }
108 }