Skip to content

Commit

Permalink
1289 History of searches
Browse files Browse the repository at this point in the history
  • Loading branch information
car031 committed Oct 21, 2024
1 parent bfa670a commit 2060362
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import com.logicaldoc.core.security.TenantDAO;
import com.logicaldoc.core.security.user.User;
import com.logicaldoc.core.security.user.UserDAO;
import com.logicaldoc.core.security.user.UserEvent;
import com.logicaldoc.core.security.user.UserHistory;
import com.logicaldoc.core.security.user.UserHistoryDAO;
import com.logicaldoc.util.Context;
import com.logicaldoc.util.config.ContextProperties;
import com.logicaldoc.util.plugin.PluginRegistry;
Expand Down Expand Up @@ -211,6 +214,18 @@ public Long mapRow(ResultSet rs, int row) throws SQLException {
log.info("Search completed in {} ms and found {} hits (estimated {})", execTime, hits.size(),
estimatedHitsNumber);

UserHistoryDAO historyDao = (UserHistoryDAO) Context.get().getBean(UserHistoryDAO.class);
UserHistory transaction = options.getTransaction();
if (transaction == null)
transaction = new UserHistory();
transaction.setUser(searchUser);
transaction.setComment(StringUtils.left(options.toString(), 500));
transaction.setEvent(UserEvent.SEARCH.toString());
try {
historyDao.store(transaction);
} catch (PersistenceException e) {
log.info("Error trying to save search history", e);
}
return hits;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,28 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.logicaldoc.core.security.user.UserHistory;

/**
* Search options
*
* @author Michael Scholz
*/
public class SearchOptions implements Serializable, Comparable<SearchOptions> {
public abstract class SearchOptions implements Serializable, Comparable<SearchOptions> {

private static final long serialVersionUID = 2L;

Expand Down Expand Up @@ -67,6 +79,9 @@ public class SearchOptions implements Serializable, Comparable<SearchOptions> {

private Long tenantId = null;

@Transient
private UserHistory transaction;

public Long getTemplate() {
return template;
}
Expand Down Expand Up @@ -236,6 +251,14 @@ public void setParameters(Map<String, Object> parameters) {
this.parameters = parameters;
}

public UserHistory getTransaction() {
return transaction;
}

public void setTransaction(UserHistory transaction) {
this.transaction = transaction;
}

@Override
public int compareTo(SearchOptions o) {
return this.getName().compareTo(o.getName());
Expand All @@ -248,9 +271,32 @@ public boolean equals(Object obj) {
SearchOptions other = (SearchOptions) obj;
return getName().equals(other.getName());
}

@Override
public int hashCode() {
return getName().hashCode();
}

@Override
public String toString() {
return this.getClass().getSimpleName().replace("Options", "")
+ new ReflectionToStringBuilder(this, ToStringStyle.NO_CLASS_NAME_STYLE) {
protected boolean accept(Field field) {
try {
Object value = field.get(getObject());
// avoid to print null values and empty collections
return super.accept(field) && !field.getName().equals("name")
&& !field.getName().equals("description") && !field.getName().equals("transaction")
&& value != null && StringUtils.isNotEmpty(value.toString())
&& (!field.getType().isArray() || Array.getLength(value) > 0)
&& (!Collection.class.isAssignableFrom(field.getType())
|| Boolean.FALSE.equals(value.getClass().getDeclaredMethod("isEmpty")
.invoke(value, new Object[0])));
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
return false;
}
}
}.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,10 @@ protected TagSearch() {
@SuppressWarnings("unchecked")
@Override
public void internalSearch() throws SearchException {
try {
prepareExpression();
} catch (PersistenceException e) {
throw new SearchException(e);
}

DocumentDAO dao = (DocumentDAO) Context.get().getBean(DocumentDAO.class);
try {
hits.addAll(dao.query(options.getExpression(), new HitMapper(), options.getMaxHits()));
String query = prepareQuery();
hits.addAll(dao.query(query, new HitMapper(), options.getMaxHits()));
} catch (PersistenceException e) {
throw new SearchException(e);
}
Expand All @@ -56,7 +51,7 @@ public void internalSearch() throws SearchException {
*
* @throws PersistenceException error at data layer
*/
private void prepareExpression() throws PersistenceException {
private String prepareQuery() throws PersistenceException {
// Find all real documents
StringBuilder query = new StringBuilder(
"select A.ld_id, A.ld_customid, A.ld_docref, A.ld_type, A.ld_version, A.ld_lastmodified, ");
Expand Down Expand Up @@ -93,7 +88,7 @@ private void prepareExpression() throws PersistenceException {

log.info("executing tag search query = {}", query);

options.setExpression(query.toString());
return query.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.logicaldoc.core.searchengine.folder;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.logicaldoc.core.metadata.Attribute;

Expand Down Expand Up @@ -271,4 +274,19 @@ public void setDoubleValue(Double doubleValue) {
public void setExtendedAttribute(boolean extendedAttribute) {
this.extendedAttribute = extendedAttribute;
}

@Override
public String toString() {
return new ReflectionToStringBuilder(this, ToStringStyle.NO_CLASS_NAME_STYLE) {
protected boolean accept(Field field) {
try {
Object value = field.get(getObject());
return super.accept(field) && value != null && StringUtils.isNotEmpty(value.toString())
&& !field.getName().equals("field");
} catch (IllegalAccessException | IllegalArgumentException | SecurityException e) {
return false;
}
}
}.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class FolderSearch extends Search {

private static final String AND = " and ";

private String query;

@SuppressWarnings("unchecked")
@Override
public void internalSearch() throws SearchException {
Expand All @@ -63,7 +65,7 @@ public void internalSearch() throws SearchException {

Map<String, Object> params = null;
try {
params = prepareExpression();
params = prepareQuery();
} catch (PersistenceException e1) {
throw new SearchException(e1);
}
Expand All @@ -74,7 +76,7 @@ public void internalSearch() throws SearchException {
// Execute the search
List<Hit> folders;
try {
folders = dao.query(options.getExpression(), params, new HitMapper(), null);
folders = dao.query(query, params, new HitMapper(), null);
} catch (PersistenceException e) {
throw new SearchException(e);
}
Expand Down Expand Up @@ -104,39 +106,36 @@ public void internalSearch() throws SearchException {
*
* PersistenceException error at data layer
*/
private Map<String, Object> prepareExpression() throws PersistenceException {
if (StringUtils.isNotEmpty(options.getExpression()))
private Map<String, Object> prepareQuery() throws PersistenceException {
if (StringUtils.isNotEmpty(query))
return options.getParameters();

Map<String, Object> params = new HashMap<>();
StringBuilder query = new StringBuilder();
StringBuilder sb = new StringBuilder();

if (options.isRetrieveAliases())
query.append("(");
sb.append("(");

// Find all real folders
query.append(
sb.append(
"select A.ld_id, A.ld_parentid, A.ld_name, A.ld_description, A.ld_creation, A.ld_lastmodified, A.ld_type, A.ld_foldref, C.ld_name, A.ld_templateid, A.ld_tgs, A.ld_color ");
query.append(" from ld_folder A ");
query.append(" left outer join ld_template C on A.ld_templateid=C.ld_id ");
appendWhereClause(false, params, query);
sb.append(" from ld_folder A ");
sb.append(" left outer join ld_template C on A.ld_templateid=C.ld_id ");
appendWhereClause(false, params, sb);

if (options.isRetrieveAliases()) {
// Append all aliases
query.append(
sb.append(
") UNION (select A.ld_id, A.ld_parentid, A.ld_name, A.ld_description, A.ld_creation, A.ld_lastmodified, A.ld_type, A.ld_foldref, C.ld_name, REF.ld_templateid, REF.ld_tgs, A.ld_color ");
query.append(" from ld_folder A ");
query.append(" join ld_folder REF on A.ld_foldref=REF.ld_id ");
query.append(" left outer join ld_template C on REF.ld_templateid=C.ld_id ");
appendWhereClause(true, params, query);
query.append(")");
sb.append(" from ld_folder A ");
sb.append(" join ld_folder REF on A.ld_foldref=REF.ld_id ");
sb.append(" left outer join ld_template C on REF.ld_templateid=C.ld_id ");
appendWhereClause(true, params, sb);
sb.append(")");
}

options.setExpression(query.toString());

log.info("executing query {}", query);
log.info("with parameters {}", params);

log.info("executing query {} with parameters {}", sb, params);
query = sb.toString();
return params;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public enum UserEvent {
UPDATED("event.user.updated"),
DISABLED("event.user.disabled"),
ENABLED("event.user.enabled"),
NEWAPIKEY("event.user.newapikey");
NEWAPIKEY("event.user.newapikey"),
SEARCH("event.user.search");

private String event;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.logicaldoc.core.security.user;

import com.logicaldoc.core.History;
import com.logicaldoc.core.security.Session;

/**
* History entry due to an event on a user.
Expand All @@ -18,6 +19,11 @@ public UserHistory() {
super();
}

public UserHistory(Session session) {
super();
setSession(session);
}

public UserHistory(UserHistory source) {
copyAttributesFrom(source);
setAuthor(source.getAuthor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
Expand Down Expand Up @@ -55,6 +56,9 @@ public void testWrite() throws IOException, ClassNotFoundException {
opt.write(file);

FulltextSearchOptions opt2 = (FulltextSearchOptions) SearchOptions.read(file);

System.out.println(opt);


Assert.assertEquals("prova test", opt2.getExpression());
Assert.assertEquals("it", opt2.getExpressionLanguage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class TagSearchTest extends AbstractCoreTestCase {

@Test
public void testSearch() {
SearchOptions opt = new SearchOptions();
SearchOptions opt = new TagSearchOptions();
opt.setType(SearchOptions.TYPE_TAG);
opt.setUserId(1);
opt.setExpression("abc");
Expand All @@ -35,8 +35,7 @@ public void testSearch() {
assertEquals(2, results.size());
assertEquals(1, results.get(0).getId());

opt = new SearchOptions();
opt.setType(SearchOptions.TYPE_TAG);
opt = new TagSearchOptions();
opt.setUserId(1);
opt.setExpression("abc");
opt.setMaxHits(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ protected void onDraw() {
list.setFields(user, event, date, comment, fileName, path);
}

list.addDoubleClickHandler(evnt -> {
LD.askForValue(I18N.message(COMMENT), I18N.message(COMMENT),
list.getSelectedRecord().getAttributeAsString(COMMENT), value -> {
// Nothing to do
});
evnt.cancel();
list.addCellClickHandler(click -> {
ListGridField field = list.getField(click.getColNum());
String title = field.getTitle();
String value = list.getDefaultFormattedFieldValue(click.getRecord(), field);
LD.askForValue(title, title, value, v -> {
// Nothing to do
});
click.cancel();
});

ToolStrip buttons = new ToolStrip();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,15 @@ public void onDraw() {
showContextMenu();
evn.cancel();
});
histories.addCellClickHandler(click -> {
ListGridField field = histories.getField(click.getColNum());
String title = field.getTitle();
String value = histories.getDefaultFormattedFieldValue(click.getRecord(), field);
LD.askForValue(title, title, value, v -> {
// Nothing to do
});
click.cancel();
});

results.addMember(histories);

Expand Down
Loading

0 comments on commit 2060362

Please sign in to comment.