Skip to content

Commit

Permalink
Merge pull request #435 from hashmapinc/Tempus-310
Browse files Browse the repository at this point in the history
Tempus 310
  • Loading branch information
jetinder20 authored Jun 11, 2018
2 parents ea72d76 + 13493cd commit 1a7765b
Show file tree
Hide file tree
Showing 56 changed files with 1,639 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class TempusSecurityConfiguration extends WebSecurityConfigurerAdapter {
public static final String FORM_BASED_LOGIN_ENTRY_POINT = "/api/auth/login";
public static final String PUBLIC_LOGIN_ENTRY_POINT = "/api/auth/login/public";
public static final String TOKEN_REFRESH_ENTRY_POINT = "/api/auth/token";
protected static final String[] NON_TOKEN_BASED_AUTH_ENTRY_POINTS = new String[] {"/index.html", "/static/**", "/api/noauth/**", "/api/theming/**", "/webjars/**"};
protected static final String[] NON_TOKEN_BASED_AUTH_ENTRY_POINTS = new String[] {"/index.html", "/static/**", "/api/noauth/**", "/api/theming/**","/api/logo/**","/webjars/**"};
public static final String TOKEN_BASED_AUTH_ENTRY_POINT = "/api/**";
public static final String WS_TOKEN_BASED_AUTH_ENTRY_POINT = "/api/ws/**";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.hashmapinc.server.common.data.Theme;
import com.hashmapinc.server.common.data.Logo;
import com.hashmapinc.server.common.data.User;
import com.hashmapinc.server.common.data.UserSettings;
import com.hashmapinc.server.common.data.id.CustomerId;
Expand All @@ -31,12 +32,18 @@
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.hashmapinc.server.dao.settings.UserSettingsService;
import com.hashmapinc.server.dao.logo.LogoService;
import org.springframework.web.multipart.MultipartFile;
import com.hashmapinc.server.dao.theme.ThemeService;
import com.hashmapinc.server.exception.TempusException;
import com.hashmapinc.server.service.mail.MailService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;


@Slf4j
@RestController
Expand All @@ -52,6 +59,10 @@ public class UserSettingsController extends BaseController {
@Autowired
private ThemeService themeService;

@Autowired
private LogoService logoService;


@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER', 'SYS_ADMIN')")
@RequestMapping(value = "/settings/{key}", method = RequestMethod.GET)
@ResponseBody
Expand Down Expand Up @@ -128,4 +139,40 @@ public Theme updateTheme(@RequestBody String value) throws TempusException {
}
}

@PreAuthorize("hasAuthority('SYS_ADMIN')")
@RequestMapping(value = "/settings/uploadLogo", method = RequestMethod.POST)
public Logo uploadLogo(@RequestParam("file") MultipartFile file) throws TempusException {
try {

Logo l = new Logo();
l.setDisplay(true);
l.setName(file.getOriginalFilename());
l.setFile(file.getBytes());

return logoService.saveLogo(l);
// return null;
} catch (Exception e) {
throw handleException(e);
}
}

@RequestMapping(value = "/logo", method = RequestMethod.GET)
@ResponseStatus(value = HttpStatus.OK)
public Logo getLogo() throws TempusException {
try {

List <Logo> logo = logoService.find();

if(logo.size() > 0) {

return logo.get(0);
}

return null;

} catch (Exception e) {
throw handleException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,13 @@ public void performInstall() {

componentDiscoveryService.discoverComponents();

systemDataLoaderService.loadSystemThemes();
systemDataLoaderService.createSysAdmin();
systemDataLoaderService.createAdminSettings();
systemDataLoaderService.loadSystemWidgets();
systemDataLoaderService.loadSystemPlugins();
systemDataLoaderService.loadSystemRules();
systemDataLoaderService.loadSystemThemes();


if (loadDemo) {
log.info("Loading demo data...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,24 @@ public void createAdminSettings() throws Exception {

public void loadSystemThemes() throws Exception {

log.info("Loading theme data...");
Theme theme1 = new Theme();
theme1.setThemeName("Tempus Blue");
theme1.setThemeValue("themeBlue");
theme1.setThemeStatus(false);
themeService.saveTheme(theme1);

Theme theme2 = new Theme();
theme2.setThemeName("Tempus Dark");
theme2.setThemeValue("themeDark");
theme2.setThemeStatus(true);
themeService.saveTheme(theme2);
List<Theme> theme = themeService.findAll();

if(theme.size() < 2) {


Theme theme1 = new Theme();
theme1.setThemeName("Tempus Blue");
theme1.setThemeValue("themeBlue");
theme1.setThemeStatus(false);
themeService.saveTheme(theme1);

Theme theme2 = new Theme();
theme2.setThemeName("Tempus Dark");
theme2.setThemeValue("themeDark");
theme2.setThemeStatus(true);
themeService.saveTheme(theme2);

}


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@
@SqlDao
public class SqlDatabaseSchemaService implements DatabaseSchemaService {

private static final String SQL_DIR = "sql";
private static final String SQL_DIR_HSQL = "sql/hsql";
private static String SQL_DIR = "";
private static final String SQL_DIR_POSTGRES = "sql/postgres";
private static final String UPGRADE_DIR = "upgrade";
private static final String SCHEMA_SQL = "schema.sql";
protected static final String SCHEMA_SQL = "schema.sql";
//private static final String SCHEMA_HSQLDB_SQL = "schemaH.sql";
//private static final String SCHEMA_POSTGRES_SQL = "schemaP.sql";

@Value("${install.data_dir}")
private String dataDir;
Expand All @@ -63,17 +67,37 @@ public class SqlDatabaseSchemaService implements DatabaseSchemaService {
public void createDatabaseSchema() throws Exception {

log.info("Installing SQL DataBase schema...");
int hsqldbConn = dbUrl.indexOf("hsqldb");
int postgresConn = dbUrl.indexOf("postgres");



if(postgresConn != -1) {

SQL_DIR = SQL_DIR_POSTGRES;

//schemaFiles = schemaFile;
}

if(hsqldbConn != -1) {


SQL_DIR = SQL_DIR_HSQL;
//schemaFiles = schemaFile;
}


Path schemaFile = Paths.get(this.dataDir, SQL_DIR, SCHEMA_SQL);
Path upgradeScriptsDirectory = Paths.get(this.dataDir, SQL_DIR, UPGRADE_DIR);


try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
Statement stmt = conn.createStatement()) {

String sql = new String(Files.readAllBytes(schemaFile), Charset.forName("UTF-8"));

stmt.execute(sql);

log.info("Installing pending upgrades ...");

List<String> executedUpgrades = new ArrayList<>();
ResultSet rs = stmt.executeQuery("select " + ModelConstants.INSTALLED_SCRIPTS_COLUMN + " from " + ModelConstants.INSTALLED_SCHEMA_VERSIONS);
Expand All @@ -88,6 +112,7 @@ public void createDatabaseSchema() throws Exception {
String scriptFileName = i.toString() + ".sql";
if (!executedUpgrades.contains(scriptFileName)) {
String upgradeQueries = new String(Files.readAllBytes(upgradeScriptsDirectory.resolve(scriptFileName)), Charset.forName("UTF-8"));
System.out.println(upgradeQueries);
stmt.execute(upgradeQueries);
stmt.execute("insert into " + ModelConstants.INSTALLED_SCHEMA_VERSIONS + " values('" + scriptFileName + "'" + ")");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hashmapinc.server.common.data.User;
import com.hashmapinc.server.common.data.Theme;
import com.hashmapinc.server.common.data.Logo;
import com.hashmapinc.server.common.data.id.ThemeId;
import com.hashmapinc.server.common.data.UserSettings;
import org.junit.Assert;
import com.hashmapinc.server.dao.theme.ThemeService;
import com.hashmapinc.server.dao.logo.LogoService;
import org.springframework.beans.factory.annotation.Autowired;


Expand All @@ -41,6 +43,10 @@ public abstract class BaseUserSettingsControllerTest extends AbstractControllerT
@Autowired
ThemeService themeService;

@Autowired
LogoService logoService;


private static final ObjectMapper mapper = new ObjectMapper();

@Test
Expand Down Expand Up @@ -208,5 +214,25 @@ public void getThemes() throws Exception{

}


@Test
public void getLogo() throws Exception{

byte[] aByteArray = {0xa,0x2,0xf,(byte)0xff,(byte)0xff,(byte)0xff};

Logo logo = new Logo();
logo.setName("test.jpg");
logo.setDisplay(true);
logo.setFile(aByteArray);
logoService.saveLogo(logo);

Logo logoNew = doGet("/api/logo", Logo.class);

Assert.assertEquals(logo.isDisplay(),logoNew.isDisplay());

logoService.deleteLogoByName(logoNew.getName());

}



}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ public class ControllerSqlTestSuite {

@ClassRule
public static CustomSqlUnit sqlUnit = new CustomSqlUnit(
Arrays.asList("sql/schema.sql", "sql/system-data.sql"),
Arrays.asList("sql/hsql/schema.sql", "sql/system-data.sql"),
"sql/drop-all-tables.sql",
"sql-test.properties",
Arrays.asList("sql/upgrade/1.sql", "sql/upgrade/2.sql", "sql/upgrade/4.sql")
Arrays.asList("sql/hsql/upgrade/1.sql", "sql/hsql/upgrade/2.sql","sql/hsql/upgrade/4.sql")


);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ public class MqttSqlTestSuite {

@ClassRule
public static CustomSqlUnit sqlUnit = new CustomSqlUnit(
Arrays.asList("sql/schema.sql", "sql/system-data.sql"),
Arrays.asList("sql/hsql/schema.sql", "sql/system-data.sql"),
"sql/drop-all-tables.sql",
"sql-test.properties",
Arrays.asList("sql/upgrade/1.sql", "sql/upgrade/2.sql", "sql/upgrade/4.sql"));
Arrays.asList("sql/hsql/upgrade/1.sql", "sql/hsql/upgrade/2.sql", "sql/hsql/upgrade/4.sql"));


}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ public class SystemSqlTestSuite {

@ClassRule
public static CustomSqlUnit sqlUnit = new CustomSqlUnit(
Arrays.asList("sql/schema.sql", "sql/system-data.sql"),
Arrays.asList("sql/hsql/schema.sql", "sql/system-data.sql"),
"sql/drop-all-tables.sql",
"sql-test.properties",
Arrays.asList("sql/upgrade/1.sql", "sql/upgrade/2.sql", "sql/upgrade/4.sql")
Arrays.asList("sql/hsql/upgrade/1.sql", "sql/hsql/upgrade/2.sql", "sql/hsql/upgrade/4.sql")


);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@

public enum EntityType {

TENANT, CUSTOMER, USER, RULE, PLUGIN, DASHBOARD, ASSET, DEVICE, ALARM, APPLICATION, COMPUTATION, COMPUTATION_JOB, NODE_METRIC,THEME, DATA_MODEL

TENANT, CUSTOMER, USER, RULE, PLUGIN, DASHBOARD, ASSET, DEVICE, ALARM, APPLICATION, COMPUTATION, COMPUTATION_JOB, NODE_METRIC,THEME, LOGO, DATA_MODEL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright © 2017-2018 Hashmap, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hashmapinc.server.common.data;
import com.hashmapinc.server.common.data.BaseData;
import com.hashmapinc.server.common.data.id.LogoId;
import lombok.EqualsAndHashCode;


public class Logo extends BaseData<LogoId> {

private boolean display;
private byte[] file;
private String name;

public Logo() {
super();
}


public Logo(LogoId id) {
super(id);
}

public Logo(Logo logo) {
super(logo);
this.file = logo.file;
this.display = logo.display;
this.name = logo.name;
}


public boolean isDisplay() {
return display;
}

public void setDisplay(boolean display) {
this.display = display;
}

public byte[] getFile() {
return file;
}

public void setFile(byte[] file) {
this.file = file;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


@Override
public String toString() {
return "logo{" +
"display=" + display +
", file=" + file +
",logoName=" + name +
'}';
}

}
Loading

0 comments on commit 1a7765b

Please sign in to comment.