-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of publisher points table
Many things outstanding, this is just a proof of concept for H2
- Loading branch information
1 parent
635129f
commit 05d3376
Showing
14 changed files
with
678 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...test/com/infiniteautomation/mango/spring/db/MockPublishedPointDatabaseModelV1Mapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Copyright (C) 2019 Infinite Automation Software. All rights reserved. | ||
*/ | ||
package com.infiniteautomation.mango.spring.db; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import com.serotonin.m2m2.vo.publish.mock.MockPublishedPointDatabaseV1Model; | ||
import com.serotonin.m2m2.vo.publish.mock.MockPublishedPointVO; | ||
|
||
/** | ||
* @author Terry Packer | ||
* | ||
*/ | ||
@Component | ||
public class MockPublishedPointDatabaseModelV1Mapping implements DatabaseModelJacksonMapping<MockPublishedPointVO, MockPublishedPointDatabaseV1Model> { | ||
|
||
@Override | ||
public Class<? extends MockPublishedPointVO> fromClass() { | ||
return MockPublishedPointVO.class; | ||
} | ||
|
||
@Override | ||
public Class<? extends MockPublishedPointDatabaseV1Model> toClass() { | ||
return MockPublishedPointDatabaseV1Model.class; | ||
} | ||
|
||
@Override | ||
public MockPublishedPointDatabaseV1Model map(Object from, DatabaseModelMapper mapper) { | ||
return new MockPublishedPointDatabaseV1Model((MockPublishedPointVO)from); | ||
} | ||
|
||
@Override | ||
public String getVersion() { | ||
return MockPublishedPointDatabaseV1Model.class.getName(); | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
Core/src-test/com/serotonin/m2m2/vo/publish/PublishedPointTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* Copyright (C) 2019 Infinite Automation Software. All rights reserved. | ||
*/ | ||
package com.serotonin.m2m2.vo.publish; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import com.serotonin.m2m2.DataTypes; | ||
import com.serotonin.m2m2.MangoTestBase; | ||
import com.serotonin.m2m2.db.dao.DataPointDao; | ||
import com.serotonin.m2m2.db.dao.DataSourceDao; | ||
import com.serotonin.m2m2.db.dao.PublishedPointDao; | ||
import com.serotonin.m2m2.db.dao.PublisherDao; | ||
import com.serotonin.m2m2.module.ModuleElementDefinition; | ||
import com.serotonin.m2m2.vo.DataPointVO; | ||
import com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO; | ||
import com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO; | ||
import com.serotonin.m2m2.vo.publish.mock.MockPublishedPointVO; | ||
import com.serotonin.m2m2.vo.publish.mock.MockPublisherDefinition; | ||
import com.serotonin.m2m2.vo.publish.mock.MockPublisherVO; | ||
|
||
/** | ||
* @author Terry Packer | ||
* | ||
*/ | ||
public class PublishedPointTest extends MangoTestBase { | ||
|
||
@BeforeClass | ||
public static void addDefinitions() { | ||
List<ModuleElementDefinition> definitions = new ArrayList<>(); | ||
definitions.add(new MockPublisherDefinition()); | ||
addModule("mockPublisher", definitions); | ||
} | ||
|
||
@Test | ||
public void testPublisherAuditTrail() { | ||
MockPublisherVO pub = new MockPublisherVO(); | ||
pub.setDefinition(new MockPublisherDefinition()); | ||
pub.setXid("PUB_TEST1"); | ||
pub.setEnabled(false); | ||
PublisherDao.getInstance().savePublisher(pub); | ||
|
||
MockDataSourceVO ds = new MockDataSourceVO(); | ||
ds.setXid("DS_TEST1"); | ||
ds.setName("TEST"); | ||
DataSourceDao.getInstance().save(ds); | ||
|
||
//Save a point | ||
DataPointVO dp = new DataPointVO(); | ||
dp.setXid("DP_1"); | ||
dp.setName("Data point name"); | ||
dp.setPointLocator(new MockPointLocatorVO(DataTypes.NUMERIC, true)); | ||
dp.setDataSourceId(ds.getId()); | ||
DataPointDao.getInstance().save(dp); | ||
|
||
//Save publisher point | ||
MockPublishedPointVO pp = new MockPublishedPointVO(); | ||
pp.setXid(PublishedPointDao.getInstance().generateUniqueXid()); | ||
pp.setName("Test published point"); | ||
pp.setPublisherId(pub.getId()); | ||
pp.setDataPointId(dp.getId()); | ||
pp.setMockSetting("mock setting"); | ||
pp.ensureValid(); | ||
PublishedPointDao.getInstance().save(pp); | ||
|
||
//Verify the point was created correctly | ||
MockPublishedPointVO mpp = (MockPublishedPointVO)PublishedPointDao.getInstance().get(pp.getId()); | ||
Assert.assertEquals(pp.getId(), mpp.getId()); | ||
Assert.assertEquals(pp.getXid(), mpp.getXid()); | ||
Assert.assertEquals(pp.getName(), mpp.getName()); | ||
Assert.assertEquals(pp.isEnabled(), mpp.isEnabled()); | ||
Assert.assertEquals(pp.getPublisherId(), mpp.getPublisherId()); | ||
Assert.assertEquals(pp.getDataPointId(), mpp.getDataPointId()); | ||
Assert.assertEquals(pp.getMockSetting(), mpp.getMockSetting()); | ||
|
||
//TODO Verify Audit Events | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
Core/src-test/com/serotonin/m2m2/vo/publish/mock/MockPublishedPointDatabaseV1Model.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright (C) 2019 Infinite Automation Software. All rights reserved. | ||
*/ | ||
package com.serotonin.m2m2.vo.publish.mock; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import com.serotonin.m2m2.db.dao.PublishedPointDao.PublishedPointSettingsDatabaseModel; | ||
import com.serotonin.m2m2.vo.publish.PublishedPointVO; | ||
|
||
/** | ||
* @author Terry Packer | ||
* | ||
*/ | ||
@Component | ||
public class MockPublishedPointDatabaseV1Model extends PublishedPointSettingsDatabaseModel { | ||
|
||
private String setting; | ||
|
||
public MockPublishedPointDatabaseV1Model() { | ||
|
||
} | ||
|
||
public MockPublishedPointDatabaseV1Model(MockPublishedPointVO vo) { | ||
this.setting = vo.getMockSetting(); | ||
} | ||
|
||
public String getSetting() { | ||
return setting; | ||
} | ||
|
||
public void setSetting(String setting) { | ||
this.setting = setting; | ||
} | ||
|
||
@Override | ||
public PublishedPointVO toVO() { | ||
MockPublishedPointVO vo = new MockPublishedPointVO(); | ||
vo.setMockSetting(setting); | ||
return vo; | ||
} | ||
|
||
@Override | ||
public String getVersion() { | ||
return this.getClass().getName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
Core/src/com/infiniteautomation/mango/spring/db/AbstractDatabaseModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Copyright (C) 2019 Infinite Automation Software. All rights reserved. | ||
*/ | ||
package com.infiniteautomation.mango.spring.db; | ||
|
||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
|
||
/** | ||
* @author Terry Packer | ||
* | ||
*/ | ||
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.EXISTING_PROPERTY, property=AbstractDatabaseModel.VERSION_FIELD) | ||
public abstract class AbstractDatabaseModel { | ||
|
||
public static final String VERSION_FIELD = "version"; | ||
|
||
/** | ||
* Ensure the model has a version | ||
* @return | ||
*/ | ||
public abstract String getVersion(); | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
Core/src/com/infiniteautomation/mango/spring/db/DatabaseModelJacksonMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright (C) 2019 Infinite Automation Software. All rights reserved. | ||
*/ | ||
package com.infiniteautomation.mango.spring.db; | ||
|
||
/** | ||
* Used to ensure Jackson is able to de-serialize the model (type T) in this mapping | ||
* | ||
* @author Terry Packer | ||
* | ||
*/ | ||
public interface DatabaseModelJacksonMapping <F, T> extends DatabaseModelMapping<F, T> { | ||
|
||
/** | ||
* Return the type name that maps the T class (Model) to a Type in Jackson | ||
* in the DAO layer we call this the version | ||
*/ | ||
public String getVersion(); | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
Core/src/com/infiniteautomation/mango/spring/db/DatabaseModelMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright (C) 2019 Infinite Automation Software. All rights reserved. | ||
*/ | ||
package com.infiniteautomation.mango.spring.db; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.jsontype.NamedType; | ||
import com.infiniteautomation.mango.spring.MangoRuntimeContextConfiguration; | ||
import com.infiniteautomation.mango.util.exception.TranslatableRuntimeException; | ||
import com.serotonin.m2m2.i18n.TranslatableMessage; | ||
|
||
/** | ||
* Gets a list of DatabaseModelMapping beans from the Spring context and uses them to convert an object to its model. | ||
* The DatabaseModelMapping beans can be annotated with @Order to specify their priority. | ||
* | ||
* @author Terry Packer | ||
* | ||
*/ | ||
@Component | ||
public class DatabaseModelMapper { | ||
private final List<DatabaseModelMapping<?,?>> mappings; | ||
|
||
@Autowired | ||
public DatabaseModelMapper(Optional<List<DatabaseModelMapping<?,?>>> mappings, | ||
@Qualifier(MangoRuntimeContextConfiguration.DAO_OBJECT_MAPPER_NAME)ObjectMapper objectMapper) { | ||
this.mappings = mappings.orElseGet(Collections::emptyList); | ||
|
||
//Load in the mappings for Jackson | ||
for(DatabaseModelMapping<?,?> mapping : this.mappings) { | ||
if(mapping instanceof DatabaseModelJacksonMapping) | ||
objectMapper.registerSubtypes(new NamedType(mapping.toClass(), ((DatabaseModelJacksonMapping<?,?>)mapping).getVersion())); | ||
} | ||
} | ||
|
||
public <T> T map(Object from, Class<T> model) { | ||
Objects.requireNonNull(from); | ||
Objects.requireNonNull(model); | ||
|
||
for (DatabaseModelMapping<?,?> mapping : mappings) { | ||
if (mapping.supports(from, model)) { | ||
@SuppressWarnings("unchecked") | ||
T result = (T) mapping.map(from, this); | ||
if (result != null) { | ||
return result; | ||
} | ||
} | ||
} | ||
|
||
throw new TranslatableRuntimeException(new TranslatableMessage("database.missingModelMapping", from.getClass(), model)); | ||
} | ||
|
||
} |
Oops, something went wrong.