1 31 32 package org.opencms.scheduler.jobs; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsProperty; 37 import org.opencms.file.CmsPropertyDefinition; 38 import org.opencms.file.CmsResource; 39 import org.opencms.file.CmsResourceFilter; 40 import org.opencms.file.types.CmsResourceTypeImage; 41 import org.opencms.loader.CmsImageLoader; 42 import org.opencms.loader.CmsImageScaler; 43 import org.opencms.lock.CmsLock; 44 import org.opencms.main.CmsException; 45 import org.opencms.report.CmsLogReport; 46 import org.opencms.report.I_CmsReport; 47 import org.opencms.scheduler.I_CmsScheduledJob; 48 49 import java.util.Collections ; 50 import java.util.List ; 51 import java.util.Map ; 52 53 67 public class CmsCreateImageSizeJob implements I_CmsScheduledJob { 68 69 72 public String launch(CmsObject cms, Map parameters) throws Exception { 73 74 if (!CmsImageLoader.isEnabled()) { 75 return Messages.get().getBundle().key(Messages.LOG_IMAGE_SCALING_DISABLED_0); 77 } 78 79 I_CmsReport report = new CmsLogReport(cms.getRequestContext().getLocale(), CmsCreateImageSizeJob.class); 80 report.println(Messages.get().container(Messages.RPT_IMAGE_SIZE_START_0), I_CmsReport.FORMAT_HEADLINE); 81 82 List resources = Collections.EMPTY_LIST; 83 try { 84 resources = cms.readResources( 86 "/", 87 CmsResourceFilter.IGNORE_EXPIRATION.addRequireType(CmsResourceTypeImage.getStaticTypeId())); 88 } catch (CmsException e) { 89 report.println(e); 90 } 91 92 int count = 0; 93 for (int i = 0; i < resources.size(); i++) { 95 96 try { 97 98 CmsResource res = (CmsResource)resources.get(i); 99 report.print(Messages.get().container( 100 Messages.RPT_IMAGE_SIZE_PROCESS_3, 101 String.valueOf(i + 1), 102 String.valueOf(resources.size()), 103 res.getRootPath()), I_CmsReport.FORMAT_HEADLINE); 104 105 report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 106 107 CmsLock lock = cms.getLock(res); 110 if (lock.isNullLock() || lock.getUserId().equals(cms.getRequestContext().currentUser().getId())) { 111 112 CmsProperty prop = cms.readPropertyObject(res, CmsPropertyDefinition.PROPERTY_IMAGE_SIZE, false); 114 if (prop == null) { 115 prop = CmsProperty.getNullProperty(); 116 } 117 CmsFile file = CmsFile.upgrade(res, cms); 119 CmsImageScaler scaler = new CmsImageScaler(file.getContents(), file.getRootPath()); 121 122 if (scaler.isValid()) { 123 if (prop.isNullProperty() || !prop.getValue().equals(scaler.toString())) { 125 126 boolean unlockFlag = false; 127 if (lock.isNullLock()) { 129 cms.lockResource(res.getRootPath()); 130 unlockFlag = true; 131 } 132 if (prop.isNullProperty()) { 134 prop = new CmsProperty( 135 CmsPropertyDefinition.PROPERTY_IMAGE_SIZE, 136 null, 137 scaler.toString()); 138 } else { 139 prop.setStructureValue(CmsProperty.DELETE_VALUE); 141 prop.setResourceValue(scaler.toString()); 143 } 144 cms.writePropertyObject(res.getRootPath(), prop); 146 if (unlockFlag) { 148 cms.unlockResource(res.getRootPath()); 149 } 150 count++; 152 report.println( 154 Messages.get().container(Messages.RPT_IMAGE_SIZE_UPDATE_1, scaler.toString()), 155 I_CmsReport.FORMAT_DEFAULT); 156 157 } else { 158 report.println( 159 Messages.get().container(Messages.RPT_IMAGE_SIZE_SKIP_1, scaler.toString()), 160 I_CmsReport.FORMAT_DEFAULT); 161 } 162 } else { 163 report.println( 164 Messages.get().container(Messages.RPT_IMAGE_SIZE_UNABLE_TO_CALCULATE_0), 165 I_CmsReport.FORMAT_DEFAULT); 166 } 167 168 } else { 169 report.println( 171 Messages.get().container(Messages.RPT_IMAGE_SIZE_LOCKED_0), 172 I_CmsReport.FORMAT_DEFAULT); 173 } 174 175 } catch (CmsException e) { 176 report.println(e); 177 } 178 } 179 180 report.println(Messages.get().container(Messages.RPT_IMAGE_SIZE_END_0), I_CmsReport.FORMAT_HEADLINE); 181 182 return Messages.get().getBundle().key(Messages.LOG_IMAGE_SIZE_UPDATE_COUNT_1, new Integer (count)); 183 } 184 } | Popular Tags |