Skip to content

Commit

Permalink
refs #583410 - Coma now can issue code resources to the above layers …
Browse files Browse the repository at this point in the history
…but no notifications on changes.
  • Loading branch information
mgoellnitz committed Aug 22, 2016
1 parent 69abb0b commit ae8ed5f
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 1 deletion.
41 changes: 41 additions & 0 deletions coma/src/org/tangram/coma/ComaBeanFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,37 @@
*/
package org.tangram.coma;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tangram.content.BeanListener;
import org.tangram.content.CodeResource;
import org.tangram.content.Content;
import org.tangram.util.SystemUtils;


public class ComaBeanFactory extends AbstractComaBeanFactory {

private static final Logger LOG = LoggerFactory.getLogger(ComaBeanFactory.class);

@Inject
private Set<ComaBeanPopulator> populators;

/**
* set of document type names holding codes.
*/
private Set<String> codeTypeNames = new HashSet<>();


public void setCodeTypeNames(Set<String> codeTypeNames) {
this.codeTypeNames = codeTypeNames;
}


@Override
public Object createBlob(String id, String propertyName, String mimeType, long len, byte[] data) {
Expand All @@ -44,6 +62,29 @@ public Content createContent(String id, String type, Map<String, Object> propert
} // createContent()


public <T extends Content> List<T> listCode() {
List<T> result = new ArrayList<>();
for (String typeName : codeTypeNames) {
for (String id : listIds(typeName, null, null, false)) {
ComaCode code = new ComaCode(id, typeName, getProperties(this, typeName, id));
for (ComaBeanPopulator populator : populators) {
populator.populate(code);
} // if
if (code.containsKey("annotation")&&code.containsKey("mimeType")) {
result.add(SystemUtils.convert(code));
} // if
} // for
} // for
return result;
} // listCode()


@Override
public <T extends Content> List<T> listBeans(Class<T> cls) {
return cls==CodeResource.class ? listCode() : super.listBeans(cls);
} // listBeans()


public Set<Content> getChildrenWithType(String parentId, String type) {
Set<Content> result = new HashSet<>();
for (String id : getChildrenWithTypeIds(parentId, type)) {
Expand Down
72 changes: 72 additions & 0 deletions coma/src/org/tangram/coma/ComaCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
*
* Copyright 2016 Martin Goellnitz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.tangram.coma;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Map;
import org.tangram.content.CodeResource;


/**
* Code implementation in the Coma context.
*/
public class ComaCode extends ComaContent implements CodeResource {

public ComaCode(String id, String type, Map<String, Object> properties) {
super(id, type, properties);
} // ()


@Override
public String getAnnotation() {
return ""+get("annotation");
}


@Override
public String getMimeType() {
return ""+get("mimeType");
}


@Override
public String getCodeText() {
return ""+get("code");
}


@Override
public long getModificationTime() {
return (Long)get("modificationTime");
}


@Override
public long getSize() {
return getCodeText().length();
} // getSize()


@Override
public InputStream getStream() throws Exception {
return new ByteArrayInputStream(getCodeText().getBytes("UTF-8"));
} // getStream()

} // ComaCode
37 changes: 36 additions & 1 deletion coma/test/org/tangram/coma/test/ComaBeanFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@
*/
package org.tangram.coma.test;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.tangram.coma.ComaBeanFactory;
import org.tangram.coma.ComaBeanPopulator;
import org.tangram.coma.ComaBlob;
import org.tangram.coma.ComaContent;
import org.tangram.components.coma.test.ComaTestCodeBeanPopulator;
import org.tangram.content.CodeResource;
import org.tangram.content.Content;
import org.tangram.mock.content.Topic;
import org.tangram.util.SystemUtils;
Expand Down Expand Up @@ -54,13 +59,27 @@ public class ComaBeanFactoryTest {
*/
private ComaBeanFactory createFactory(String dbUrl) {
ComaBeanFactory factory = new ComaBeanFactory();
Map<String,String> parents = new HashMap<>();
Map<String, String> parents = new HashMap<>();
parents.put("Topic", "Linkable");
factory.setParents(parents);
factory.setDbUrl(dbUrl);
factory.setDbDriver(DB_DRIVER);
factory.setDbUser(DB_USER);
factory.setDbPassword(DB_PASSWORD);
Set<String> codeTypeNames = new HashSet<>(1);
codeTypeNames.add("Topic");
codeTypeNames.add("RootTopic");
factory.setCodeTypeNames(codeTypeNames);
Set<ComaBeanPopulator> populators = new HashSet<>(1);
populators.add(new ComaTestCodeBeanPopulator());
try {
Field populatorField = factory.getClass().getDeclaredField("populators");
populatorField.setAccessible(true);
populatorField.set(factory, populators);
populatorField.setAccessible(false);
} catch (NoSuchFieldException|IllegalAccessException e) {
Assert.fail("could not create coma bean factory for some unexpected reason", e);
} // try/catch
factory.afterPropertiesSet();
return factory;
} // createFactory()
Expand Down Expand Up @@ -113,6 +132,22 @@ public void testRepository() {
} // testRepository()


/**
* Test code related methods.
*/
@Test
public void testCodes() {
ComaBeanFactory factory = createFactory(DB_URL);
List<CodeResource> contents = factory.listBeans(CodeResource.class);
Assert.assertNotNull(contents, "There should be some result when listing code resources.");
Assert.assertEquals(contents.size(), 2, "We have a fixed number of code resources available.");
CodeResource code = contents.get(1);
Assert.assertEquals(code.getAnnotation(), "org.tangram.example.Topic", "Unexpected annotation for code.");
Assert.assertEquals(code.getMimeType(), "text/html", "Unexpected mime type for code.");
Assert.assertEquals(code.getSize(), 58, "Unexpected size for code.");
} // testCodes()


@Test
public void testInitFailure() {
ComaBeanFactory factory = createFactory("jdbc:xsqldb:test/unittest;readonly=true");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
*
* Copyright 2016 Martin Goellnitz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.tangram.components.coma.test;

import java.util.Date;
import javax.inject.Named;
import javax.inject.Singleton;
import org.tangram.coma.ComaBeanPopulator;
import org.tangram.coma.ComaContent;


/**
* Dummy population of beans as code.
*/
@Named
@Singleton
public class ComaTestCodeBeanPopulator implements ComaBeanPopulator {

@Override
public void populate(ComaContent content) {
if ("Topic".equals(content.getDocumentType())) {
content.put("annotation", "org.tangram.example.Topic");
content.put("mimeType", "text/html");
content.put("code", "<html><head><title>Test</title></head><body></body></html>");
content.put("modificationTime", new Date().getTime());
} // if
} // populate()

} // ComaTestCodeBeanPopulator

0 comments on commit ae8ed5f

Please sign in to comment.