Introduced in Feature Pack 2

Error handling when restoring a Recipe object from a different version

Introduced in Feature Pack 2 By default, you can choose to restore the Recipe object with different versions. However, in some cases when you restore a version for Recipe that contains the data which has an association with the Catalog Entry object; if the Catalog Entry does not exist in the database anymore, you might not want to restore this Recipe since the Recipe is not complete. Assuming that you do not expect to restore the previous data successfully, you might prefer to receive a corresponding error message to make you aware of this issue. In this lesson, you will learn how to create a new Java class for handling the error exception to override the default behaviour.

About this task

Procedure

  1. Expand Project-Server > ejbModule > com.mycompany.commerce.project.logging package.
  2. Open the ProjectApplicationMessageKeys.java for editing
  3. Append the following code to the end of the file:
    /**
    	 * Constant for the message that the catentry is deleted/MARKFORDELETED.
    	 */
    public static final String _APP_PROJECT_CATENTRY_MARKFORDELETE = "_APP_PROJECT_CATENTRY_MARKFORDELETE";
  4. Expand com.mycompany.commerce.project.logging.properties.
  5. Open the file WcProjectMessages.properties for editing, append the following code to the end of the file:
    _APP_PROJECT_CATENTRY_MARKFORDELETE = The associated CatEntry no longer exists
    By doing this, you just defined a new error message for the error handling.
  6. In the JAVA EE view, navigate to WebSphereCommerceServerExtensionsLogic > src.
  7. Right-click on src, click New > Package.
  8. In the name field, enter com.mycompany.commerce.project.version, then click Finish. A new package is created successfully.
  9. Right-click com.mycompany.commerce.project.version, click New > Class.
  10. In the name field, enter ProjectContentVersionServiceImpl.
  11. In the Superclass field, click on the Browse. Superclass Selection panel will pop up.
  12. In the Choose a type field, enter AbstractContentVersionServiceImpl; then click OK.
  13. Click Finish to create the class. A new file is opened for editing. Paste the following code into the file:
    package com.mycompany.commerce.project.version;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.logging.Level;
    
    import com.ibm.commerce.content.facade.datatypes.ContentVersionType;
    import com.ibm.commerce.content.version.AbstractContentVersionServiceImpl;
    import com.ibm.commerce.foundation.common.exception.AbstractApplicationException;
    import com.ibm.commerce.foundation.common.util.logging.LoggingHelper;
    import com.mycompany.commerce.project.facade.server.exception.ProjectApplicationException;
    import com.mycompany.commerce.project.logging.ProjectApplicationMessageKeys;
    
    public class ProjectContentVersionServiceImpl extends
    		AbstractContentVersionServiceImpl {
    	
    	
    	private final static String CLASSNAME = ProjectContentVersionServiceImpl.class
    	.getName();
        private static final java.util.logging.Logger LOGGER = LoggingHelper
    	.getLogger(ProjectContentVersionServiceImpl.class);
    /* Error handling when restoring the project */
    	protected void restore() throws AbstractApplicationException {
    		final String METHODNAME = "restore";
    		/* Enable the log trace for the method */
    		if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
    			LOGGER.entering(CLASSNAME, METHODNAME);
    		}
    		super.restore();
    		/* Get the  project noun */
    		ContentVersionType aProjectVersion = getNoun();
    		/* Get the  storeId */
    		String storeId = aProjectVersion.getContentVersionIdentifier()
    				.getExternalIdentifier().getStoreIdentifier().getUniqueID();
    		/* Get the  projectId */
    		long objectId = aProjectVersion.getContentVersionIdentifier()
    				.getExternalIdentifier().getObjectId();
    		/* Get the Catentry's column MARKFORDELETE */
    		
    		String  SQL =" Select CATENTRY.MARKFORDELETE FROM CATENTRY LEFT OUTER JOIN XPRJCATREL ON (CATENTRY.CATENTRY_ID = XPRJCATREL.CATENTRY_ID)WHERE XPRJCATREL.XPROJECT_ID IN (?)AND XPRJCATREL.STOREENT_ID IN (?)";
    
    
    		
    		Connection connection = getConnection();
    		PreparedStatement ps = null;
    		ResultSet rs = null;
    		try {
    			ps = connection.prepareStatement(SQL);
    			ps.setLong(1, objectId);
    			ps.setInt(2, Integer.parseInt(storeId));
    			
    			rs = ps.executeQuery();
    			while (rs.next()) {
    				int markfordelete = rs.getInt(1);
    				if (markfordelete == 1) {
    					if (LOGGER.isLoggable(Level.SEVERE)) {
    						LOGGER.log(Level.SEVERE, "catentry is markfordelete");
    					}
    					throw new ProjectApplicationException(
    							ProjectApplicationMessageKeys._APP_PROJECT_CATENTRY_MARKFORDELETE,
    							new Object[] { }, CLASSNAME, METHODNAME);
    				}
    				
    			}
    		} catch (SQLException ex) {
    			if (LOGGER.isLoggable(Level.SEVERE)) {
    				LOGGER.log(Level.SEVERE, "The sql: " + SQL + " (" + storeId
    						+ "," + objectId + ")" + " returns an error: "
    						+ ex.toString());
    			}
    			throw new ProjectApplicationException(
    					ProjectApplicationMessageKeys._APP_PROJECT_CATENTRY_MARKFORDELETE,
    					new Object[] { }, CLASSNAME, METHODNAME);
    		} finally {
    			if (rs != null) {
    				try {
    					rs.close();
    				} catch (SQLException e) {
    					if (LOGGER.isLoggable(Level.WARNING)) {
    						LOGGER.log(Level.WARNING, "Error:" + e.toString());
    					}
    				}
    			}
    			if (ps != null) {
    				try {
    					ps.close();
    				} catch (SQLException e) {
    					if (LOGGER.isLoggable(Level.WARNING)) {
    						LOGGER.log(Level.WARNING, "Error:" + e.toString());
    						
    					}
    				}
    		
    			}
    		
    		}
    
    		if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
    			LOGGER.exiting(CLASSNAME, METHODNAME);
    		}
    
    	}
    
    }
  14. Save all your changes.
  15. Resolve compilation errors.
    You will encounter a compilation error "ProjectApplicationException cannot be resolved to a type".
    1. In the JAVA EE view, navigate to WebSphereCommerceServerExtensionsData.
    2. Right-click WebSphereCommerceServerExtensionsData, then click Properties.
    3. In the newly opened property panel, select Java EE Module Dependencies, and in JAR/Module field, click to add checkmark beside the Project-Server.jar.
    4. Click Apply and OK.
  16. Clean the WebSphereCOmmerceServerExtensionsLogic project.
  17. Navigate to the file wc-content-version.xml which is under WC > xml > config > com.mycompany.commerce.project. Locate the following code:
    <wc:ContentVersionContainer name="com.mycompany.commerce.project" id="1">
    <wc:ContentVersionNoun name="Project" topTable="XPROJECT"> 

    Change it to be:

    <wc:ContentVersionContainer name="com.mycompany.commerce.project" id="1">
    <wc:ContentVersionNoun name="Project" topTable="XPROJECT"
    className="com.mycompany.commerce.project.version.ProjectContentVersionServiceImpl"> 
  18. Restart the server.

Results