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/DefaultViewService.java


1   /*
2    * DefaultViewService.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.util.Enumeration;
14  import java.util.Vector;
15  
16  import com.flexstor.common.constants.SequenceConstantsI;
17  import com.flexstor.common.data.ActionData;
18  import com.flexstor.common.data.ActionResult;
19  import com.flexstor.common.data.ejb.disguiserecord.DisguiseAssetRecordData;
20  import com.flexstor.common.data.ejb.disguiserecord.DisguiseRecordData;
21  import com.flexstor.common.data.ejb.sequence.SequenceData;
22  import com.flexstor.common.gateway.SequenceGateway;
23  import com.flexstor.common.gateway.exceptions.TransactionFailedException;
24  import com.flexstor.common.importprocessor.ImportData;
25  import com.flexstor.common.importprocessor.ImportResult;
26  import com.flexstor.common.resources.Resources;
27  import com.flexstor.common.services.ServiceArgumentsI;
28  import com.flexstor.common.util.Diagnostic;
29  import com.flexstor.flexdbserver.services.Service;
30  import com.flexstor.flexdbserver.services.ServiceContext;
31  
32  /**
33   * <P>
34   * DefaultViewService <BR>
35   * <BLOCKQUOTE>
36   *    Assigns a default view to each asset according to the following rules:  <BR>
37   *    - A parent only has one default view.  <BR>
38   *    - A thumbnail is always its own default view.  <BR>
39   *    - Low resolution always uses the default view of its parent asset.  <BR>
40   *    - The first thumbnail created (in a multi-thumbnail environment) is the default view of its parent.  <BR>
41   *    - If an asset has a default view already assigned, it will not be reassigned a new one.  <BR>
42   * </BLOCKQUOTE>
43   * </P>
44   *
45   * Configurable Properties in roletype_services.config <BR>
46   *
47   * <TABLE BORDER="1" CELLPADDING="3" CELLSPACING="3">
48   * <CAPTION ALIGN=TOP>
49   *    <B> In/Out Properties for Assets </B>
50   * </CAPTION>
51   *     <TR>
52   *        <FONT SIZE=+1><B>
53   *        <TH WIDTH="120">Attribute</TH>
54   *                                   <TH WIDTH="30">IN</TH>           <TH WIDTH="30">OUT</TH>          <TH WIDTH="30">Default IN</TH>  <TH WIDTH="30">Default OUT</TH>
55   *        </B></FONT>
56   *     </TR>
57   *     <TR>
58   *        <TH ALIGN=LEFT><FONT SIZE=+1><B>ROLE</B></FONT></TH>
59   *                                   <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> ALL </TD>      <TD ALIGN=CENTER> &nbsp </TD>
60   *     </TR>
61   *     <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>
62   *     <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>
63   *     <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>
64   *     <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>
65   *     <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>
66   *     <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>
67   *     <TR>
68   *        <TH ALIGN=LEFT><FONT SIZE=+1><B>TYPE</B></FONT></TH>
69   *                                   <TD ALIGN=CENTER> X </TD>        <TD ALIGN=CENTER> &nbsp </TD>    <TD ALIGN=CENTER> ALL </TD>      <TD ALIGN=CENTER> &nbsp </TD></TR>
70   *     </TR>
71   *     <TR>
72   *        <TH ALIGN=LEFT><FONT SIZE=+1><B>FLAG</B></FONT></TH>
73   *     </TR>
74   *     <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>
75   *     <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>
76   *     <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>
77   *     <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>
78   *     <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>
79   *     <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>
80   * </TABLE>
81   *
82   * <P>
83   * Input Data Object <BR>
84   * <BLOCKQUOTE>
85   *    com.flexstor.common.importprocessor.ImportData
86   * </BLOCKQUOTE>
87   * </P>
88   *
89   * <P>
90   * Output Data Object <BR>
91   * <BLOCKQUOTE>
92   *    com.flexstor.common.importprocessor.ImportResult
93   * </BLOCKQUOTE>
94   * </P>
95   */
96  public class DefaultViewService
97     implements Service
98  {
99     // To get the version number from MKS
100    public final static String IDENTIFIER="$Id: DefaultViewService.java,v 1.4 2003/08/11 02:22:29 aleric Exp $";
101 
102    private String     sThisService    = "";
103    protected ServiceContext context;
104    protected int id = -1;
105    
106    protected ImportData        refImportData  = null;
107    protected Vector              vAssetRecords  =  null;
108    protected boolean             successful     = true;
109    protected SequenceData      sequenceData   = null;
110    
111    /**
112     * Calls before the service is initialized (before initData is called) to 
113     * pass information about the environment in which the service is running.
114     * This environment consists of information about the properties set for the
115     * service in one of these files (services.config, roletype_services.config,
116     * or *.ctl), plus methods to access other information such as an instance
117     * of the service broker to invoke other services, the transaction id for
118     * the service, file separator character and local path for the installation
119     * directory and configuration directory.
120     * 
121     * @param context Holds information about the environment in which the service
122     *                is running.
123     */
124    public void setServiceContext( ServiceContext context )
125    {
126       this.context = context;
127       this.id = context.getTransactionId();
128    }
129 
130    /**
131    * A data initialization method called at the beginning of the service.
132    * The input argument, ActionData must be cast into its subclass, ImportData
133    * in order to extract the service specific data from it.
134    */
135    public void initData(ActionData actionData)
136    {
137       // cast the data object to a convert data wrapper
138       refImportData = (ImportData) actionData;
139 
140       //6317=DefaultView Service
141       sThisService = Resources.get(6317) + " (" + id + ")";
142    }
143 
144    /**
145    * The start of the DefaultView Service.
146    */
147    public ActionResult go()
148    {
149       if ( successful )
150       {
151          // Also get Role, Type and Flag values from property list (AssetService)
152          DisguiseRecordData refDisguiseRecordData = refImportData.getDisguiseRecordRef();
153          String sRole = context.getProperty(ServiceArgumentsI.ROLE_DATA_SOURCE);
154          String sType = context.getProperty(ServiceArgumentsI.TYPE_DATA_SOURCE);
155          String sFlag = context.getProperty(ServiceArgumentsI.FLAG_DATA_SOURCE);
156 
157          Diagnostic.trace(Diagnostic.APPSERVER_IMPORT, "Role = " + sRole);
158          Diagnostic.trace(Diagnostic.APPSERVER_IMPORT, "Type = " + sType);
159          Diagnostic.trace(Diagnostic.APPSERVER_IMPORT, "Flag = " + sFlag);
160 
161          if(refDisguiseRecordData != null)
162          {
163                vAssetRecords = refDisguiseRecordData.getAssets(sRole, sType, sFlag);
164                if( vAssetRecords != null && vAssetRecords.size() > 0 )
165                {
166                   // Retrieve a bulk of sequence numbers for use during looping
167                   requestSequenceBulk( vAssetRecords.size() );
168 
169                   Enumeration assets = vAssetRecords.elements();
170 
171                   // Processs each asset in each element in ImportData as required
172                   while(assets.hasMoreElements())
173                   {
174                      DisguiseAssetRecordData anAsset = (DisguiseAssetRecordData)assets.nextElement();
175 
176                      Diagnostic.trace(Diagnostic.APPSERVER_IMPORT, "####### Calling SetDefaultView for " + anAsset.getFileName());
177                      // Get Info only
178                      if (setDefaultView(anAsset) == false)
179                      {
180                         // Hard error, quit now
181                         Diagnostic.trace(Diagnostic.APPSERVER_IMPORT, "####### SetDefaultView error");
182                         successful = false;
183                         break;
184                      }
185                   }
186                }
187                else
188                   successful = false;
189          }
190          else
191          successful = false;
192       }
193       // pass back the ImportData
194       ImportResult response = new ImportResult(successful); // turn off default result
195       response.setImportData(refImportData);
196 
197       String sResult = "OK";
198       if (successful == false)
199         sResult = "FAILED";
200       Diagnostic.trace(Diagnostic.APPSERVER_IMPORT, "####### Leaving DefaultViewService: " + sResult);
201 
202       return response;
203    } // go()
204 
205    private boolean setDefaultView( DisguiseAssetRecordData defaultViewAsset )
206    {
207       // First get an asset Id from the database
208       long nAssetId = defaultViewAsset.getRecordId();
209 
210       if ( nAssetId == 0 )
211       {
212          nAssetId = getNewAssetId();
213          if ( nAssetId == 0 )
214             return false;
215             
216          defaultViewAsset.setRecordId( nAssetId );
217       }
218       
219       else
220       {
221       Diagnostic.trace(Diagnostic.APPSERVER_IMPORT, "####### Existing asset ID is " + nAssetId);
222       }
223       
224       defaultViewAsset.setDefaultViewAssetId( nAssetId );
225       
226       DisguiseAssetRecordData parent = defaultViewAsset.getParent();
227       
228       if ( parent != null )
229       {
230          // If the default view for the parent is not set yet, then set the 
231          // default view for children.
232          if ( parent.setDefaultViewAssetId( nAssetId ) )
233          {
234             DisguiseAssetRecordData[] childAssets = parent.getChildAssets();
235             if ( childAssets != null )
236             {
237                for ( int i = 0; i < childAssets.length; i++ )
238                {
239                   // If this asset is a parent, we don't assign the default view yet.
240                   // It will be taken care when its thumbnail child gets passed to this
241                   // method.
242                   if ( childAssets[i].hasChildren() == false )
243                   {
244                      // Also, if this asset is not part of the vector, then assign its
245                      // default view.
246                      if ( vAssetRecords.contains( childAssets[i] ) == false )
247                         childAssets[i].setDefaultViewAssetId( nAssetId );
248                   }
249                }
250             }
251          }
252       }
253       return true;
254    }
255    
256    private void requestSequenceBulk( int nCount )
257    {
258       SequenceGateway seqGateway = null;
259       if ( nCount > 0 )
260       {
261          try
262          {
263             seqGateway = new SequenceGateway();
264             seqGateway.connect();
265             sequenceData = seqGateway.getSequenceData( SequenceConstantsI.ASSET_ID, nCount );
266          }
267          catch(TransactionFailedException rtfe)
268          {
269             sequenceData = null;
270          }
271          finally
272          {
273             seqGateway.dispose();
274          }
275       }
276       else
277          sequenceData = null;
278    }
279   
280    private long getNewAssetId()
281    {
282       if ( sequenceData == null )
283          return 0;
284           
285       if ( sequenceData.hasMoreItems() )
286       {
287          long nAssetId = sequenceData.nextSequence();
288          Diagnostic.trace(Diagnostic.APPSERVER_IMPORT, "####### Sequence number is " + nAssetId);
289          return nAssetId;
290       }
291       else
292          return 0;
293    }
294 }