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.access.getfile.conversation;
26  
27  import org.bitrepository.access.getfile.selectors.FastestPillarSelectorForGetFile;
28  import org.bitrepository.bitrepositorymessages.IdentifyPillarsForGetFileRequest;
29  import org.bitrepository.client.conversation.ConversationContext;
30  import org.bitrepository.client.conversation.GeneralConversationState;
31  import org.bitrepository.client.conversation.IdentifyingState;
32  import org.bitrepository.client.conversation.selector.SelectedComponentInfo;
33  
34  
35  /**
36   * Models the functionality for identifying pillars prior to a get file request.
37   */
38  public class IdentifyingPillarsForGetFile extends IdentifyingState {
39      private final GetFileConversationContext context;
40  
41      /** 
42       * The constructor for the indicated conversation.
43       * @param context The related context related to the conversation containing information.
44       */
45      public IdentifyingPillarsForGetFile(GetFileConversationContext context) {
46          super(context.getContributors());
47          this.context = context;
48          context.getMonitor().markAsFailedOnContributorFailure(false);
49          if (context.getContributors().size() > 1) {
50              setSelector(new FastestPillarSelectorForGetFile());
51          }
52      }
53  
54      @Override
55      public GeneralConversationState getOperationState() {
56          return new GettingFile(context, getSelectedPillar());
57  
58      }
59  
60      @Override
61      protected void sendRequest() {
62          IdentifyPillarsForGetFileRequest msg = new IdentifyPillarsForGetFileRequest();
63          initializeMessage(msg);
64          msg.setDestination(context.getSettings().getCollectionDestination());
65          msg.setFileID(context.getFileID());
66          context.getMessageSender().sendMessage(msg);
67          context.getMonitor().identifyRequestSent("Identifying pillars for GetFile");
68          
69      }
70  
71      @Override
72      protected ConversationContext getContext() {
73          return context;
74      }
75  
76      @Override
77      protected String getPrimitiveName() {
78          return "IdentifyPillarsForGetFile";
79      }
80  
81      /**
82       * Helper method to extract the single selected pillar fron the generic selector's SelectedComponents list.
83       */
84      private SelectedComponentInfo getSelectedPillar() {
85          return getSelector().getSelectedComponents().iterator().next();
86      }
87  }