/**
* Copyright (C) 2010-2017 Structr GmbH
*
* This file is part of Structr <http://structr.org>.
*
* Structr is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Structr is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Structr. If not, see <http://www.gnu.org/licenses/>.
*/
package org.structr.web.entity;
import java.io.InputStream;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.structr.common.PropertyView;
import org.structr.common.SecurityContext;
import org.structr.common.View;
import org.structr.common.error.FrameworkException;
import org.structr.common.fulltext.FulltextIndexer;
import org.structr.common.fulltext.Indexable;
import org.structr.core.Export;
import org.structr.core.GraphObject;
import org.structr.core.app.StructrApp;
import org.structr.core.entity.AbstractNode;
import org.structr.core.property.IntProperty;
import org.structr.core.property.LongProperty;
import org.structr.core.property.Property;
import org.structr.core.property.StringProperty;
import org.structr.rest.common.HttpHelper;
/**
*
*
*/
public class RemoteDocument extends AbstractNode implements Indexable {
private static final Logger logger = LoggerFactory.getLogger(RemoteDocument.class.getName());
public static final Property<String> url = new StringProperty("url");
public static final Property<Long> checksum = new LongProperty("checksum").indexed().unvalidated().readOnly();
public static final Property<Integer> cacheForSeconds = new IntProperty("cacheForSeconds").cmis();
public static final Property<Integer> version = new IntProperty("version").indexed().readOnly();
public static final View publicView = new View(RemoteDocument.class, PropertyView.Public, type, name, contentType, url, owner);
public static final View uiView = new View(RemoteDocument.class, PropertyView.Ui, type, contentType, url, checksum, version, cacheForSeconds, owner, extractedContent, indexedWords);
@Override
public void afterCreation(SecurityContext securityContext) {
update();
}
@Export
public void update() {
try {
final FulltextIndexer indexer = StructrApp.getInstance(securityContext).getFulltextIndexer();
indexer.addToFulltextIndex(this);
} catch (FrameworkException fex) {
logger.warn("Unable to index " + this, fex);
}
}
@Export
@Override
public GraphObject getSearchContext(final String searchTerm, final int contextLength) {
final String text = getProperty(extractedContent);
if (text != null) {
final FulltextIndexer indexer = StructrApp.getInstance(securityContext).getFulltextIndexer();
return indexer.getContextObject(searchTerm, text, contextLength);
}
return null;
}
public void increaseVersion() throws FrameworkException {
final Integer _version = getProperty(RemoteDocument.version);
unlockSystemPropertiesOnce();
if (_version == null) {
setProperty(RemoteDocument.version, 1);
} else {
setProperty(RemoteDocument.version, _version + 1);
}
}
@Override
public InputStream getInputStream() {
final String remoteUrl = getProperty(url);
if (StringUtils.isNotBlank(remoteUrl)) {
return HttpHelper.getAsStream(remoteUrl);
}
return null;
}
// ----- private methods -----
}