Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/flexdbserver/services/asset/ImportMoveService.java


1   /*
2    * ImportMoveService.java
3    *
4    * Copyright $Date: 2003/08/11 02:22:29 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.flexdbserver.services.asset;
12  
13  import java.net.UnknownHostException;
14  import java.util.Iterator;
15  import java.util.Vector;
16  
17  import com.flexstor.common.constants.ServicesI;
18  import com.flexstor.common.data.ActionData;
19  import com.flexstor.common.data.ActionResult;
20  import com.flexstor.common.data.AssetRecordData;
21  import com.flexstor.common.data.ejb.disguiserecord.DisguiseAssetRecordData;
22  import com.flexstor.common.data.ejb.disguiserecord.DisguiseRecordData;
23  import com.flexstor.common.importprocessor.ImportData;
24  import com.flexstor.common.importprocessor.ImportResult;
25  import com.flexstor.common.io.xfile.FlexXFile;
26  import com.flexstor.common.resources.Resources;
27  import com.flexstor.common.services.ServiceArgumentsI;
28  import com.flexstor.common.services.ServiceBrokerI;
29  import com.flexstor.common.services.SrvcNotAvailException;
30  import com.flexstor.common.util.InetUtil;
31  import com.flexstor.common.util.ServerList;
32  import com.flexstor.flexdbserver.services.Service;
33  import com.flexstor.flexdbserver.services.ServiceContext;
34  
35  
36  /**
37   * <P>
38   * ImportMoveService <BR>
39   * <BLOCKQUOTE>
40   *    Moves files during the import process.
41   *    This service accepts assets for which the destination location has already been set
42   *    or can be configured to create the destination path for each asset. See
43   *    SetDestinationService for configurable properties if you want to create paths.
44   * </BLOCKQUOTE>
45   * </P>
46   *
47   * Configurable Properties in roletype_services.config (or *.ctl file if run as a preservice) <BR>
48   *
49   * <TABLE BORDER="1" CELLPADDING="3" CELLSPACING="3">
50   * <CAPTION ALIGN=TOP>
51   *    <B> In/Out Properties for Assets </B>
52   * </CAPTION>
53   *     <TR>
54   *        <FONT SIZE=+1><B>
55   *        <TH WIDTH="120">Attribute</TH>
56   *                                   <TH WIDTH="30">IN</TH>           <TH WIDTH="30">OUT</TH>          <TH WIDTH="30">Default IN</TH>  <TH WIDTH="30">Default OUT</TH>
57   *        </B></FONT>
58   *     </TR>
59   *     <TR>
60   *        <TH ALIGN=LEFT><FONT SIZE=+1><B>ROLE</B></FONT></TH>
61   *                                   <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> ALL </TD>      <TD ALIGN=CENTER> &nbsp </TD>
62   *     </TR>
63   *     <TR><TH> Highres </TH>        <TD ALIGN=CENTER> X </TD>        <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD></TR>
64   *     <TR><TH> Lowres </TH>         <TD ALIGN=CENTER> X </TD>        <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD></TR>
65   *     <TR><TH> Thumbnail </TH>      <TD ALIGN=CENTER> X </TD>        <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD></TR>
66   *     <TR><TH> Layout </TH>         <TD ALIGN=CENTER> X </TD>        <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD></TR>
67   *     <TR><TH> Video </TH>          <TD ALIGN=CENTER> X </TD>        <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD></TR>
68   *     <TR><TH> Audio </TH>          <TD ALIGN=CENTER> X </TD>        <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD></TR>
69   *     <TR>
70   *        <TH ALIGN=LEFT><FONT SIZE=+1><B>TYPE</B></FONT></TH>
71   *                                   <TD ALIGN=CENTER> X </TD>        <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> ALL </TD>    <TD ALIGN=CENTER> &nbsp </TD></TR>
72   *     </TR>
73   *     <TR>
74   *        <TH ALIGN=LEFT><FONT SIZE=+1><B>FLAG</B></FONT></TH>
75   *     </TR>
76   *     <TR><TH> PARENT </TH>         <TD ALIGN=CENTER> X </TD>        <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> X </TD>        <TD ALIGN=CENTER> &nbsp </TD></TR>
77   *     <TR><TH> CHLDREN </TH>        <TD ALIGN=CENTER> X </TD>        <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD></TR>
78   *     <TR><TH> ALL </TH>            <TD ALIGN=CENTER> X </TD>        <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD></TR>
79   *     <TR><TH> TEMP_PARENT </TH>    <TD ALIGN=CENTER> X </TD>        <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD></TR>
80   *     <TR><TH> TEMP_CHILDREN </TH>  <TD ALIGN=CENTER> X </TD>        <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD></TR>
81   *     <TR><TH> TEMP_ALL </TH>       <TD ALIGN=CENTER> X </TD>        <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD></TR>
82   * </TABLE>
83   *
84   * <P>
85   *   mode: If the destination paths for the assets have already been defined, set this
86   *   property to DestinationSet to avoid running the SetDestinationService; otherwise
87   *   see the SetDestinationService for more possible values. <BR>
88   * </P>
89   *
90   *   destinationbase: Path where files will be copied. If ImportMoveService is specified as a
91   *   preservice in the input control (*.ctl) file and this property is not declared, it will default to
92   *   either the lowcopybase or highcopybase property (depending on role) in the input control file.
93   *   If ImportMoveService is not defined as a preservice, this property must be declared. <BR>
94   *   Legal values: Full path <BR>
95   * <P>
96   * <P>
97   *   originalbase: Base path where original files reside. If ImportMoveService is specified as a
98   *   preservice in the input control file and this property is not declared, it will default to either
99   *   the lowfilelistbase or highfilelistbase property (depending on role) in the input control file. If
100  *   ImportMoveService is not defined as a preservice, this property must be declared. <BR>
101  *   Legal values: Full path <BR>
102  * </P>
103  * Input Data Object <BR>
104  * <BLOCKQUOTE>
105  *    com.flexstor.common.importprocessor.ImportData
106  * </BLOCKQUOTE>
107  * </P>
108  *
109  * <P>
110  * Output Data Object <BR>
111  * <BLOCKQUOTE>
112  *    com.flexstor.common.importprocessor.ImportResult
113  * </BLOCKQUOTE>
114  * </P>
115  *
116  * Programmable Properties (passed inside data object) <BR>
117  * <BLOCKQUOTE>
118  *   Specific Properties (apply to each individual asset) <BR>
119  * <BLOCKQUOTE>
120  * <P>
121  *       DestinationLocation: Location where files will be moved. <BR>
122  *       Set using DisguiseAssetRecordData.addProperty( String key, Object value ); <BR>
123  *       Data type: String <BR>
124  *       Legal values: Full path to destination directory <BR>
125  * </P>
126  * </BLOCKQUOTE>
127  * </BLOCKQUOTE>
128  */
129 public class ImportMoveService
130    implements Service
131 {
132    // To get the version number from MKS
133    public final static String IDENTIFIER="$Id: ImportMoveService.java,v 1.5 2003/08/11 02:22:29 aleric Exp $";
134 
135    protected ServiceContext    context;
136    protected ServiceBrokerI    serviceBroker;
137    protected int                 id = -1;
138    protected String              fileSeparator;
139    private  ImportData         importData;
140    private  DisguiseRecordData disguiseRecord;
141    private  String               sThisService;
142 
143    /**
144     * Calls before the service is initialized (before initData is called) to 
145     * pass information about the environment in which the service is running.
146     * This environment consists of information about the properties set for the
147     * service in one of these files (services.config, roletype_services.config,
148     * or *.ctl), plus methods to access other information such as an instance
149     * of the service broker to invoke other services, the transaction id for
150     * the service, file separator character and local path for the installation
151     * directory and configuration directory.
152     * 
153     * @param context Holds information about the environment in which the service
154     *                is running.
155     */
156    public void setServiceContext( ServiceContext context )
157    {
158       this.context = context;
159       id = context.getTransactionId();
160       fileSeparator = context.getFileSeparator();
161       serviceBroker = context.getServiceBroker();
162    }
163    
164    public void initData(ActionData actionData)
165    {
166       importData = (ImportData) actionData;
167 
168       //6918=Import Move Service
169       sThisService = Resources.get(6918) + " (" + id + ")";
170    }
171 
172    public ActionResult go()
173    {
174       boolean bSuccess = false;
175 
176       // Get Role, Type and Flag values from property list (AssetService)
177       String sRoleIn   = context.getProperty(ServiceArgumentsI.ROLE_DATA_SOURCE);
178       String sTypeIn   = context.getProperty(ServiceArgumentsI.TYPE_DATA_SOURCE);
179       String sFlagIn   = context.getProperty(ServiceArgumentsI.FLAG_DATA_SOURCE);
180 
181       Vector vAssetsIn = null;
182       if ( importData != null && (disguiseRecord = importData.getDisguiseRecordRef()) != null )
183          vAssetsIn = disguiseRecord.getAssets( sRoleIn, sTypeIn, sFlagIn );
184 
185       try
186       {
187          ActionResult result = new ActionResult(true);
188 
189          // Lets find out whether we need to run the SetDestinationService or the destination paths
190          // were already set.
191          String sMode = (String) context.getProperty("mode");
192          if ( sMode == null || sMode.equals("") || !sMode.equalsIgnoreCase("DESTINATIONSET") )
193          {
194             importData.setServiceName(ServicesI.SETDESTINATION_SERVICE);
195             result = (ActionResult) serviceBroker.invokeImmediately(importData, context.getProperties());
196          }
197          
198          if ( result != null && result.isSuccess() )
199          {
200             importData = (ImportData)result.getData();
201             importData.setServiceName(ServicesI.ACTIONDATA_CREATE_SERVICE);
202             result = (ActionResult) serviceBroker.invokeImmediately(importData, context.getProperties());
203             if ( result != null && result.isSuccess() )
204             {
205                ActionData actionData = (ActionData) result.getData();
206 
207                // Call the MoveService
208                actionData.setServiceName(ServicesI.MOVE_SERVICE);
209                result = (ActionResult) serviceBroker.invokeImmediately(actionData);
210                // Process the result by updating the import data object; it will be
211                // false only if all assets failed
212                if ( processResult( result, vAssetsIn ) )
213                   bSuccess = true;
214             }
215          }
216       }
217       catch ( SrvcNotAvailException rsnae ) {}
218 
219       ImportResult result = new ImportResult(bSuccess);
220       result.setImportData(importData);
221       return result;
222    }
223 
224    private boolean processResult( ActionResult result, Vector vAssetsIn )
225    {
226       if ( result == null )
227          return false;
228 
229       Vector vGoodRecords = result.getGoodRecords();
230       if ( vGoodRecords == null || vGoodRecords.size() == 0 )
231          return false;
232 
233       AssetRecordData record;
234       DisguiseAssetRecordData asset;
235 
236       // Remove bad records from import data object
237       Vector vBadRecords = result.getBadRecords();
238       if ( vBadRecords != null && vBadRecords.size() > 0 )
239       {
240          for ( int i = 0; i < vBadRecords.size(); i++ )
241          {
242             record = (AssetRecordData) vBadRecords.elementAt(i);
243             asset = getAsset( record, vAssetsIn );
244             if ( asset != null )
245                disguiseRecord.deleteAsset( asset );
246          }
247       }
248 
249       // Set the new path for the assets
250       for ( int i = 0; i < vGoodRecords.size(); i++ )
251       {
252          record = (AssetRecordData) vGoodRecords.elementAt(i);
253          asset = getAsset( record, vAssetsIn );
254          FlexXFile xRecord = new FlexXFile(record.getDestinationLocation());
255          // Set the new location
256          String sDestLoc = xRecord.getLocalPath();
257          if ( sDestLoc.startsWith(fileSeparator) )
258             sDestLoc = sDestLoc.substring(1);
259          if ( !sDestLoc.endsWith(fileSeparator) )
260             sDestLoc += fileSeparator;
261          asset.setLocation( sDestLoc );
262 
263          // Set the new server, if different
264          String sAssetServer = ServerList.getDNSName( asset.getServer() );
265          String sRecordServer = xRecord.getServer();
266          try
267          {
268             InetUtil recordHost = new InetUtil( sRecordServer );
269             if ( !recordHost.isThisHost( sAssetServer ) )
270                asset.setServer( ServerList.getServerName(sRecordServer) );
271          }
272          catch ( UnknownHostException uhe )
273          {
274             // If the InetAddress test fails, just compare the Strings
275             sRecordServer = ServerList.getServerName(sRecordServer);
276             if ( !sAssetServer.equals( sRecordServer ) )
277                asset.setServer( sRecordServer );
278          }
279       }
280 
281       return true;
282    }
283 
284    private DisguiseAssetRecordData getAsset( AssetRecordData record, Vector vAssetsIn )
285    {
286       DisguiseAssetRecordData asset;
287       if ( vAssetsIn == null )
288          return null;
289          
290       for ( Iterator i = vAssetsIn.iterator(); i.hasNext(); )
291       {
292          asset = (DisguiseAssetRecordData) i.next();
293          if ( record.getServer().equals( asset.getServer() )
294             && record.getLocation().equals( asset.getLocation() )
295                && record.getFileName().equals( asset.getFileName() ) )
296          {
297             i.remove();
298             return asset;
299          }
300       }
301       return null;
302    }
303 }