Source code: com/flexstor/flexdbserver/services/asset/SwapParentPDFService.java
1 /*
2 * SwapParentPDFService.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:28 $ 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.data.ActionData;
17 import com.flexstor.common.data.ActionResult;
18 import com.flexstor.common.data.ejb.disguiserecord.DisguiseAssetRecordData;
19 import com.flexstor.common.data.ejb.disguiserecord.DisguiseRecordData;
20 import com.flexstor.common.data.ejb.disguiserecord.LayoutRoleData;
21 import com.flexstor.common.importprocessor.ImportData;
22 import com.flexstor.common.importprocessor.ImportResult;
23 import com.flexstor.common.services.ServiceArgumentsI;
24 import com.flexstor.common.util.Diagnostic;
25 import com.flexstor.flexdbserver.services.Service;
26 import com.flexstor.flexdbserver.services.ServiceContext;
27
28
29
30 /**
31 * <P>
32 * SwapParentPDFService <BR>
33 * <BLOCKQUOTE>
34 * Swaps to the location (directory) of a parent and child.
35 * The intent is to have a parent and child change places. We assume that both the
36 * parent and the child have the same file name and are on the same server. The parent
37 * file name must also end in .pdf or .PDF. The parent must have only one child asset
38 * of the specified type and that child asset must be a Temp asset. If these conditions
39 * are met, then the location and role are swapped.
40 * </BLOCKQUOTE>
41 * </P>
42 *
43 * <P>
44 * Configurable Properties in services.config <BR>
45 * <BLOCKQUOTE>
46 * None
47 * </BLOCKQUOTE>
48 * </P>
49 *
50 * Configurable Properties in roletype_services.config <BR>
51 *
52 * <TABLE BORDER="1" CELLPADDING="3" CELLSPACING="3">
53 * <CAPTION ALIGN=TOP>
54 * <B> In/Out Properties for Assets </B>
55 * </CAPTION>
56 * <TR>
57 * <FONT SIZE=+1><B>
58 * <TH WIDTH="120">Attribute</TH>
59 * <TH WIDTH="30">IN</TH> <TH WIDTH="30">OUT</TH> <TH WIDTH="30">Default IN</TH> <TH WIDTH="30">Default OUT</TH>
60 * </B></FONT>
61 * </TR>
62 * <TR>
63 * <TH ALIGN=LEFT><FONT SIZE=+1><B>ROLE</B></FONT></TH><TD ALIGN=CENTER>   </TD> <TD ALIGN=CENTER>   </TD> <TD ALIGN=CENTER> ALL </TD> <TD ALIGN=CENTER> ALL </TD>
64 * </TR>
65 * <TR><TH> Highres </TH> <TD ALIGN=CENTER> X </TD> <TD ALIGN=CENTER> X </TD> <TD ALIGN=CENTER>   </TD> <TD ALIGN=CENTER>   </TD></TR>
66 * <TR><TH> Lowres </TH> <TD ALIGN=CENTER> X </TD> <TD ALIGN=CENTER> X </TD> <TD ALIGN=CENTER>   </TD> <TD ALIGN=CENTER>   </TD></TR>
67 * <TR><TH> Thumbnail </TH> <TD ALIGN=CENTER> X </TD> <TD ALIGN=CENTER> X </TD> <TD ALIGN=CENTER>   </TD> <TD ALIGN=CENTER>   </TD></TR>
68 * <TR><TH> Layout </TH> <TD ALIGN=CENTER> X </TD> <TD ALIGN=CENTER> X </TD> <TD ALIGN=CENTER>   </TD> <TD ALIGN=CENTER>   </TD></TR>
69 * <TR><TH> Video </TH> <TD ALIGN=CENTER> X </TD> <TD ALIGN=CENTER> X </TD> <TD ALIGN=CENTER>   </TD> <TD ALIGN=CENTER>   </TD></TR>
70 * <TR><TH> Audio </TH> <TD ALIGN=CENTER> X </TD> <TD ALIGN=CENTER> X </TD> <TD ALIGN=CENTER>   </TD> <TD ALIGN=CENTER>   </TD></TR>
71 * <TR>
72 * <TH ALIGN=LEFT><FONT SIZE=+1><B>TYPE</B></FONT></TH>
73 * <TD ALIGN=CENTER> X </TD> <TD ALIGN=CENTER> X </TD> <TD ALIGN=CENTER> ALL* </TD> <TD ALIGN=CENTER> ALL* </TD></TR>
74 * </TR>
75 * <TR>
76 * <TH ALIGN=LEFT><FONT SIZE=+1><B>FLAG</B></FONT></TH>
77 * </TR>
78 * <TR><TH> PARENT </TH> <TD ALIGN=CENTER> X* </TD> <TD ALIGN=CENTER> X* </TD> <TD ALIGN=CENTER> X </TD> <TD ALIGN=CENTER> X </TD></TR>
79 * <TR><TH> CHLDREN </TH> <TD ALIGN=CENTER> * </TD> <TD ALIGN=CENTER> * </TD> <TD ALIGN=CENTER>   </TD> <TD ALIGN=CENTER>   </TD></TR>
80 * <TR><TH> ALL </TH> <TD ALIGN=CENTER> * </TD> <TD ALIGN=CENTER> * </TD> <TD ALIGN=CENTER>   </TD> <TD ALIGN=CENTER>   </TD></TR>
81 * <TR><TH> TEMP_PARENT </TH> <TD ALIGN=CENTER> * </TD> <TD ALIGN=CENTER> * </TD> <TD ALIGN=CENTER>   </TD> <TD ALIGN=CENTER>   </TD></TR>
82 * <TR><TH> TEMP_CHILDREN </TH> <TD ALIGN=CENTER> * </TD> <TD ALIGN=CENTER> * </TD> <TD ALIGN=CENTER>   </TD> <TD ALIGN=CENTER>   </TD></TR>
83 * <TR><TH> TEMP_ALL </TH> <TD ALIGN=CENTER> X* </TD> <TD ALIGN=CENTER> X* </TD> <TD ALIGN=CENTER>   </TD> <TD ALIGN=CENTER>   </TD></TR>
84 * </TABLE>
85 * *The type for input should be Adobe Acrobat PDF. The flag should be set to parent
86 * and that parent should have a temp child. Other types and flags may work, but are not
87 * recommended or endorsed.
88 *
89 * <P>
90 * Types allowed for input: Adobe Acrobat PDF <BR>
91 * Types allowed for output: Adobe Acrobat PDF <BR>
92 * </P>
93 *
94 * <P>
95 * Input Data Object <BR>
96 * <BLOCKQUOTE>
97 * com.flexstor.common.importprocessor.ImportData
98 * </BLOCKQUOTE>
99 * </P>
100 *
101 * <P>
102 * Output Data Object <BR>
103 * <BLOCKQUOTE>
104 * com.flexstor.common.importprocessor.ImportResult
105 * </BLOCKQUOTE>
106 * </P>
107 *
108 */
109 public class SwapParentPDFService
110 implements Service
111 {
112 // Used for logging errors
113 public final static String IDENTIFIER = "$Id: SwapParentPDFService.java,v 1.4 2003/08/11 02:22:28 aleric Exp $";
114
115 // The data object
116 private ImportData refImportData = null;
117 private DisguiseRecordData refDisguiseRecord = null;
118 protected ServiceContext context;
119 protected boolean successful = true;
120
121 /**
122 * Calls before the service is initialized (before initData is called) to
123 * pass information about the environment in which the service is running.
124 * This environment consists of information about the properties set for the
125 * service in one of these files (services.config, roletype_services.config,
126 * or *.ctl), plus methods to access other information such as an instance
127 * of the service broker to invoke other services, the transaction id for
128 * the service, file separator character and local path for the installation
129 * directory and configuration directory.
130 *
131 * @param context Holds information about the environment in which the service
132 * is running.
133 */
134 public void setServiceContext( ServiceContext context )
135 {
136 this.context = context;
137 }
138
139 /**
140 * Data initialization method called at the beginning of the service.
141 *
142 * @param Action is the super class of the data wrapper object
143 * which contains all the information for executing the service.
144 */
145 public void initData( ActionData actionData )
146 {
147 refImportData = (ImportData) actionData;
148 }
149
150
151
152 /**
153 * Start of the Service
154 *
155 * @return a Result object with the an updated data object.
156 */
157 public ActionResult go()
158 {
159 DisguiseAssetRecordData aParentAsset = null;
160
161 BGPDebug("SwapParentPDFService Started");
162
163 successful = true;
164
165 // get the input assets, according to the role, type and flag specified
166 // in remote_server.config or TypeServiceDat.config
167 String sRoleIn = context.getProperty(ServiceArgumentsI.ROLE_DATA_SOURCE);
168 String sTypeIn = context.getProperty(ServiceArgumentsI.TYPE_DATA_SOURCE);
169 String sFlagIn = context.getProperty(ServiceArgumentsI.FLAG_DATA_SOURCE);
170
171 BGPDebug("SwapParent:Role(in) = " + sRoleIn);
172 BGPDebug("SwapParent:Type(in) = " + sTypeIn);
173 BGPDebug("SwapParent:Flag(in) = " + sFlagIn);
174
175 String sRoleOut = context.getProperty(ServiceArgumentsI.ROLE_DATA_DESTINATION);
176 String sTypeOut = context.getProperty(ServiceArgumentsI.TYPE_DATA_DESTINATION);
177 String sFlagOut = context.getProperty(ServiceArgumentsI.FLAG_DATA_DESTINATION);
178
179 BGPDebug("SwapParent:Role(out) = " + sRoleOut);
180 BGPDebug("SwapParent:Type(out) = " + sTypeOut);
181 BGPDebug("SwapParent:Flag(out) = " + sFlagOut);
182
183
184 ImportResult result = null;
185
186 if( refImportData == null )
187 {
188 result = new ImportResult(false);
189 return result;
190 }
191
192 // Get the reference to the DisguiseRecordData
193 refDisguiseRecord = refImportData.getDisguiseRecordRef();
194 if( refDisguiseRecord == null ) {
195 successful = false;
196 }
197
198 Vector vAssetsIn = null;
199
200 if( sRoleIn==null || sTypeIn==null || sFlagIn==null ) {
201 successful = false;
202 } else {
203 vAssetsIn = refDisguiseRecord.getAssets(sRoleIn, sTypeIn, sFlagIn);
204 if( vAssetsIn != null )
205 {
206 Enumeration assets = vAssetsIn.elements();
207
208 // Process each asset in each element in ImportData
209 while(assets.hasMoreElements())
210 {
211 aParentAsset = (DisguiseAssetRecordData)assets.nextElement();
212
213 String assetFileName = aParentAsset.getFileName();
214 String fullPathName = "/" + aParentAsset.getLocation() + assetFileName;
215 BGPDebug("An asset is found: "+fullPathName);
216
217 // We will only swap parent<->child if it's a PDF
218 String assetExtension = assetFileName.substring(assetFileName.lastIndexOf('.'));
219 if (assetExtension.equalsIgnoreCase(".PDF")) {
220 //BGPDebug(assetFileName+" is a pdf");
221 //BGPDebug("parent S is "+aParentAsset.getServer());
222 //BGPDebug("parent L is "+aParentAsset.getLocation());
223 //BGPDebug("parent F is "+aParentAsset.getFileName());
224
225 // If there are no children, we won't swap anything.
226 if (aParentAsset.hasChildren()) {
227
228 String sAll = "ALL";
229 String sTempAll = "TEMP_ALL";
230 Vector vChildAssets = new Vector();
231 // fill this vector with all children of the InType
232 BGPDebug("fetching children of type "+sTypeIn+" for swap");
233 aParentAsset.getChildren(vChildAssets,sAll,sTypeIn,sTempAll);
234 BGPDebug("size of child asset vector is "+vChildAssets.size());
235
236 // If there's more than one child, how do we know which child
237 // to swap with the parent? blaarg
238 if ((vChildAssets.size() > 1) || (vChildAssets.size() == 0)) {
239 BGPDebug(assetFileName+" has "+vChildAssets.size()+" children!");
240 BGPDebug("This was not expected, no swap done.");
241 } else {
242 // Because I'm going to refer to this puppy several times
243 // set up a temporary reference to our firstElement();
244 DisguiseAssetRecordData theChild = null;
245 theChild = (DisguiseAssetRecordData)vChildAssets.firstElement();
246
247 //BGPDebug("child[0] S is "+theChild.getServer());
248 //BGPDebug("child[0] L is "+theChild.getLocation());
249 //BGPDebug("child[0] F is "+theChild.getFileName());
250
251 BGPDebug("Swapping location");
252 // per the request, we just want to swap the location
253 String sParentLocation = aParentAsset.getLocation();
254 aParentAsset.setLocation(theChild.getLocation());
255 theChild.setLocation(sParentLocation);
256
257 BGPDebug("Swapping Role");
258 // The location isn't the only thing that needs to be swapped
259 // The Role also needs to get swapped from child to parent
260 //aParentAsset.setAssetRole(theChild.getAssetRole());
261 aParentAsset.setAssetRole(new LayoutRoleData());
262 aParentAsset.getAssetRole().setAssetFileType(sTypeOut);
263 aParentAsset.getAssetRole().setTempRole(false);
264
265 BGPDebug("Parent Info:");
266 BGPDebug("\t\tRoleId = " + aParentAsset.getAssetRoleId());
267 BGPDebug("\t\tIsTemp = " + aParentAsset.getAssetRole().isTempRole());
268 }
269 } else {
270 BGPDebug("No children. Nothing to do.");
271 }
272 }
273 }
274 }
275 else
276 successful = false;
277 }
278
279
280 BGPDebug("Done with SwapParentFile service");
281
282 if( successful == false ) {
283 result = new ImportResult(false);
284 } else {
285 result = new ImportResult(true);
286 result.setImportData(refImportData);
287 }
288
289 return result;
290 }
291
292
293
294 /*
295 * BGPDebug(String of debug text)
296 */
297 void BGPDebug(String sSomeString)
298 {
299 boolean printNormalMessages = true;
300 if (printNormalMessages == true)
301 Diagnostic.trace(Diagnostic.APPSERVER_IMPORT, sSomeString);
302 else
303 System.out.println("BGP DEBUG: "+sSomeString);
304 }
305
306 }
307