Skip to content

Commit

Permalink
Improvements for Java21
Browse files Browse the repository at this point in the history
  • Loading branch information
car031 committed Dec 30, 2024
1 parent 7293f98 commit 4031bd2
Show file tree
Hide file tree
Showing 19 changed files with 244 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,16 +320,13 @@ private void printExtendedAttributes(DateFormat df, PrintWriter writer, Document
Object val = extValues.get(key);
if (val != null) {
writer.print("<ext_" + name + ">");
if (val instanceof Date date)
writer.print(df.format(date));
else if (val instanceof Integer integer)
writer.print(Integer.toString(integer));
else if (val instanceof Long longVal)
writer.print(Long.toString(longVal));
else if (val instanceof Double doubleVal)
writer.print(Double.toString(doubleVal));
else
writer.print("<![CDATA[" + val + "]]>");
switch (val) {
case Date date -> writer.print(df.format(date));
case Integer integer -> writer.print(Integer.toString(integer));
case Long longVal -> writer.print(Long.toString(longVal));
case Double doubleVal -> writer.print(Double.toString(doubleVal));
default -> throw new IllegalArgumentException("Unexpected value: " + val);
}
writer.print("</ext_" + name + ">");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ public DigestProcessor() {
super(NAME);
log = LoggerFactory.getLogger(DigestProcessor.class);
}

public DigestProcessor(String name, DocumentDAO documentDao) {
this();
this.documentDao = documentDao;
}

@Override
public boolean isIndeterminate() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.logicaldoc.core.document;

import javax.annotation.Resource;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.logicaldoc.core.PersistenceException;
Expand All @@ -21,16 +20,12 @@ public class TagsProcessor extends Task {

public static final String NAME = "TagsProcessor";

@Resource(name = "DocumentDAO")
private DocumentDAO documentDao;
private final DocumentDAO documentDao;

public TagsProcessor() {
@Autowired
public TagsProcessor(DocumentDAO documentDao) {
super(NAME);
log = LoggerFactory.getLogger(TagsProcessor.class);
}

public TagsProcessor(String name, DocumentDAO documentDao) {
this();
this.documentDao = documentDao;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.logicaldoc.core.PersistenceException;
Expand All @@ -23,21 +22,17 @@ public class PathCalculator extends Task {

public static final String NAME = "PathCalculator";

@Resource(name = "FolderDAO")
private FolderDAO folderDao;

public PathCalculator(String name, FolderDAO folderDao) {
this();
this.folderDao = folderDao;
}

private long processed = 0;

private long errors = 0;

public PathCalculator() {
@Autowired
public PathCalculator(FolderDAO folderDao) {
super(NAME);
log = LoggerFactory.getLogger(PathCalculator.class);
this.folderDao = folderDao;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,11 @@ public void schedule(AbstractJob job, Map<String, Object> dictionary, Map<Object
}

private Trigger prepareTrigger(AbstractJob job, Object triggerSpec, Map<Object, Map<String, Object>> triggersMap) {
Trigger trig = null;

if (!triggersMap.get(triggerSpec).containsKey(TENANT_ID))
triggersMap.get(triggerSpec).put(TENANT_ID, job.getTenantId());

if (triggerSpec instanceof Date dateSpec) {
return switch (triggerSpec) {
case Date dateSpec -> {
// The job must be fired on a specific data
SimpleScheduleBuilder schedule = SimpleScheduleBuilder.simpleSchedule();
if (MISSFIRE_RUNNOW.equals(getMissfireInstruction(job.getGroup())))
Expand All @@ -131,25 +130,26 @@ private Trigger prepareTrigger(AbstractJob job, Object triggerSpec, Map<Object,
schedule = schedule.withMisfireHandlingInstructionIgnoreMisfires();

SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");
trig = TriggerBuilder.newTrigger()
.withIdentity(job.getName() + "-" + df.format(triggerSpec), job.getGroup())
yield TriggerBuilder.newTrigger().withIdentity(job.getName() + "-" + df.format(triggerSpec), job.getGroup())
.usingJobData(new JobDataMap(triggersMap.get(triggerSpec))).startAt(dateSpec).withSchedule(schedule)
.build();
} else if (triggerSpec instanceof String cronSpec) {
}
case String cronSpec -> {
// The job must be fired on a specific data
CronScheduleBuilder schedule = CronScheduleBuilder.cronSchedule(cronSpec);
if (MISSFIRE_RUNNOW.equals(getMissfireInstruction(job.getGroup())))
schedule = schedule.withMisfireHandlingInstructionFireAndProceed();
else
schedule = schedule.withMisfireHandlingInstructionDoNothing();

trig = TriggerBuilder.newTrigger().withIdentity(job.getName() + "-" + triggerSpec, job.getGroup())
yield TriggerBuilder.newTrigger().withIdentity(job.getName() + "-" + triggerSpec, job.getGroup())
.usingJobData(new JobDataMap(triggersMap.get(triggerSpec))).withSchedule(schedule).build();
} else {
}
default -> {
log.warn("Skipping trigger {} because not a string nor a date", triggerSpec);
yield null;
}

return trig;
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,38 +235,56 @@ public void setValue(Object value) {
return;
}

if (value instanceof String string) {
switch (value) {
case String string -> {
this.type = TYPE_STRING;
setStringValue(string);
} else if (value instanceof Integer integer) {
}

case Integer integer -> {
this.type = TYPE_INT;
setIntValue(Long.valueOf(integer));
} else if (value instanceof Long longVal) {
}

case Long longVal -> {
this.type = TYPE_INT;
setIntValue(longVal);
} else if (value instanceof Double doubleVal) {
}

case Double doubleVal -> {
this.type = TYPE_DOUBLE;
setDoubleValue(doubleVal);
} else if (value instanceof Date date) {
}

case Date date -> {
this.type = TYPE_DATE;
setDateValue(date);
} else if (value instanceof User user) {
}

case User user -> {
this.type = TYPE_USER;
this.intValue = user.getId();
this.stringValue = user.getUsername();
} else if (value instanceof Folder folder) {
}

case Folder folder -> {
this.type = TYPE_FOLDER;
this.intValue = folder.getId();
this.stringValue = folder.getName();
} else if (value instanceof Document document) {
}

case Document document -> {
this.type = TYPE_DOCUMENT;
this.intValue = document.getId();
this.stringValue = document.getFileName();
} else if (value instanceof Boolean bool) {
}

case Boolean bool -> {
this.type = TYPE_BOOLEAN;
this.intValue = bool.booleanValue() ? 1L : 0L;
} else {
throw new IllegalArgumentException("Not a String, Long, Double, Date, Boolean, User, Folder value");
}

default -> throw new IllegalArgumentException("Not a String, Long, Double, Date, Boolean, User, Folder value");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.logicaldoc.core.PersistenceException;
Expand All @@ -22,19 +21,14 @@
public class IndexOptimizer extends Task {
public static final String NAME = "IndexOptimizer";

@Resource(name = "SearchEngine")
private SearchEngine indexer;

@Resource(name = "DocumentDAO")
private DocumentDAO documentDao;

public IndexOptimizer() {
@Autowired
public IndexOptimizer(SearchEngine indexer, DocumentDAO documentDao) {
super(NAME);
log = LoggerFactory.getLogger(IndexOptimizer.class);
}

public IndexOptimizer(String name, SearchEngine indexer, DocumentDAO documentDao) {
this();
this.indexer = indexer;
this.documentDao = documentDao;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.logicaldoc.core.PersistenceException;
Expand Down Expand Up @@ -58,16 +57,12 @@ public class IndexerTask extends Task {

public static final String NAME = "IndexerTask";

@Resource(name = "documentManager")
private DocumentManager documentManager;

@Resource(name = "DocumentDAO")
private DocumentDAO documentDao;

@Resource(name = "TenantDAO")
private TenantDAO tenantDao;

@Resource(name = "SearchEngine")
private SearchEngine indexer;

private long indexed = 0;
Expand All @@ -78,14 +73,11 @@ public class IndexerTask extends Task {

private long parsingTime = 0;

public IndexerTask() {
@Autowired
public IndexerTask(DocumentManager documentManager, DocumentDAO documentDao, TenantDAO tenantDao,
SearchEngine indexer) {
super(NAME);
log = LoggerFactory.getLogger(IndexerTask.class);
}

public IndexerTask(String name, DocumentManager documentManager, DocumentDAO documentDao, TenantDAO tenantDao,
SearchEngine indexer) {
this();
this.documentManager = documentManager;
this.documentDao = documentDao;
this.tenantDao = tenantDao;
Expand Down Expand Up @@ -209,7 +201,9 @@ protected void runTask() throws TaskException {
Map<String, Object> params = new HashMap<>();
params.put("transactionId", transactionId);

documentDao.jdbcUpdate("update ld_document set ld_transactionid = null where ld_transactionId = :transactionId", params);
documentDao.jdbcUpdate(
"update ld_document set ld_transactionid = null where ld_transactionId = :transactionId",
params);
} catch (PersistenceException e) {
log.error(e.getMessage(), e);
}
Expand Down Expand Up @@ -256,7 +250,8 @@ private void assignTransition(List<Long> docIds) throws PersistenceException {
params.put("transactionId", transactionId);

documentDao.jdbcUpdate(
" update ld_document set ld_transactionid = :transactionId where ld_transactionid is null and ld_id in " + idsStr,
" update ld_document set ld_transactionid = :transactionId where ld_transactionid is null and ld_id in "
+ idsStr,
params);
}
log.info("Documents marked for indexing in transaction {}", transactionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ public void setValue(Serializable value) {
setLongValue((Long) value);
break;
case Attribute.TYPE_DOUBLE:
if (value instanceof Double doubleVal)
setDoubleValue(doubleVal);
else if (value instanceof Long longVal)
setDoubleValue(longVal.doubleValue());
else
setDoubleValue(((Float) value).doubleValue());
switch (value) {
case Double doubleVal -> setDoubleValue(doubleVal);
case Long longVal -> setDoubleValue(longVal.doubleValue());
case Float floatVal -> setDoubleValue(floatVal.doubleValue());
default -> setDoubleValue(null);
}
break;
case Attribute.TYPE_DATE:
setDateValue((Date) value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -816,14 +816,12 @@ protected void saveStatistic(String parameter, Object val, long tenantId) throws
} else
genericDAO.initialize(gen);

if (val instanceof Date date)
gen.setDate1(date);
else if (val instanceof String string)
gen.setString1(string);
else if (val instanceof Long longVal)
gen.setInteger1(longVal);
else
gen.setInteger1(((Integer) val).longValue());
switch (val) {
case Date dateVal -> gen.setDate1(dateVal);
case String stringVal -> gen.setString1(stringVal);
case Integer intVal -> gen.setInteger1(intVal.longValue());
default -> gen.setInteger1(null);
}

try {
genericDAO.store(gen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@ public void setApplicationContext(ApplicationContext applicationContext) {
for (Task task : tasks) {
String name = task.getName();
String triggerName = name + "Trigger";
Object trigger = applicationContext.getBean(triggerName);
if (trigger == null)
trigger = applicationContext
.getBean(Character.toLowerCase(triggerName.charAt(0)) + triggerName.substring(1));
if (trigger instanceof Trigger trgr) {
if (!applicationContext.containsBean(triggerName))
triggerName = Character.toLowerCase(triggerName.charAt(0)) + triggerName.substring(1);
if (applicationContext.getBean(triggerName) instanceof Trigger trgr) {
triggers.add(trgr);
} else
log.warn("Cannot schedule task {}", name);
Expand Down
Loading

0 comments on commit 4031bd2

Please sign in to comment.