Guest
Public paste!

cfalzone

By: a guest | Apr 20th, 2010 | Syntax: Java | Size: 2.63 KB | Hits: 171 | Expires: Never
Copy text to clipboard
  1. package com.cfalzone.jobs;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6.  
  7. import org.quartz.Job;
  8. import org.quartz.JobExecutionContext;
  9. import org.quartz.JobExecutionException;
  10.  
  11. import com.dotmarketing.business.APILocator;
  12. import com.dotmarketing.cache.FieldsCache;
  13. import com.dotmarketing.cache.StructureCache;
  14. import com.dotmarketing.cms.factories.PublicUserFactory;
  15. import com.dotmarketing.portlets.contentlet.business.ContentletAPI;
  16. import com.dotmarketing.portlets.contentlet.model.Contentlet;
  17. import com.dotmarketing.portlets.languagesmanager.business.LanguageAPI;
  18. import com.dotmarketing.portlets.structure.model.Field;
  19. import com.dotmarketing.portlets.structure.model.Structure;
  20. import com.dotmarketing.util.Logger;
  21.  
  22. public class SimpleContentCreator implements Job {
  23.  
  24.         @SuppressWarnings("unchecked")
  25.         public void execute(JobExecutionContext arg0) throws JobExecutionException {
  26.                 Logger.info(this, "SimpleContentCreator Starting up");
  27.  
  28.                 /* Get the importStructure */
  29.                 Structure importStructure = StructureCache.getStructureByType('Import Structure');
  30.                 if (importStructure == null || importStructure.getName() == null) {
  31.                         Logger.error(this, "Could not find the Import Structure: " + importStructureStr);
  32.                         exit;
  33.                 }
  34.  
  35.                 /* Get the Fields */
  36.                 Field myField = null;
  37.                 Field anotherField = null;
  38.                 List<Field> fields = FieldsCache.getFieldsByStructureInode(importStructure.getInode());
  39.                 for (Field f : fields) {
  40.                         if (f.getVelocityVarName().equals('myField')) {
  41.                                 myField = f;
  42.                         }
  43.                         if (f.getVelocityVarName().equals('anotherField')) {
  44.                                 anotherField = f;
  45.                         }
  46.                 }
  47.                 if (myField == null) {
  48.                         Logger.error(this, "myField not found in " + importStructure.getName());
  49.                         exit;
  50.                 }
  51.                 if (anotherField == null) {
  52.                         Logger.error(this, "anotherField not found in " + importStructure.getName());
  53.                         exit;
  54.                 }
  55.                        
  56.                                
  57.                 /* Create A New Contentlet */
  58.                 Contentlet con2 = new Contentlet();
  59.                 con2.setStructureInode(importStructure.getInode());
  60.                
  61.                 LanguageAPI lAPI = APILocator.getLanguageAPI();
  62.                 con2.setArchived(false);
  63.                 con2.setWorking(true);
  64.                 con2.setLive(autoPub);
  65.                 con2.setLanguageId(lAPI.getDefaultLanguage().getId());
  66.                 conAPI.setContentletProperty(con2, myField, 'A Value');
  67.                 conAPI.setContentletProperty(con2, anotherField, 'Another Value');
  68.                                                
  69.                 /* Now check the contentlet back in */
  70.                 try {
  71.                         conAPI.checkin(con2, PublicUserFactory.getSystemUser(), true);
  72.                 } catch (Exception e) {
  73.                         Logger.error(this, "Unable checkin the Contentlet. " + e.getMessage(), e);
  74.                         exit;
  75.                 }
  76.                
  77.                 Logger.info(this, "SimpleContentCreator Finished");
  78.                 return;
  79.         }
  80. }