Search in sources :

Example 1 with MUCRoomEntity

use of fi.otavanopisto.muikku.openfire.rest.client.entity.MUCRoomEntity in project muikku by otavanopisto.

the class ChatRoomSyncScheduler method updateChatRooms.

@Schedule(second = "*", minute = "*/15", hour = "*", persistent = false)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void updateChatRooms() {
    String enabledWorkspacesCsv = pluginSettingsController.getPluginSetting("chat", "enabledWorkspaces");
    if (enabledWorkspacesCsv == null) {
        return;
    }
    List<String> enabledWorkspaces = Arrays.asList(enabledWorkspacesCsv.split(","));
    String openfireToken = pluginSettingsController.getPluginSetting("chat", "openfireToken");
    if (openfireToken == null) {
        logger.log(Level.INFO, "No openfire token set, skipping room sync");
        return;
    }
    String openfireUrl = pluginSettingsController.getPluginSetting("chat", "openfireUrl");
    if (openfireUrl == null) {
        logger.log(Level.INFO, "No openfire url set, skipping room sync");
        return;
    }
    String openfirePort = pluginSettingsController.getPluginSetting("chat", "openfirePort");
    if (openfirePort == null) {
        logger.log(Level.INFO, "No openfire port set, skipping room sync");
        return;
    }
    if (!StringUtils.isNumeric(openfirePort)) {
        logger.log(Level.WARNING, "Invalid openfire port, skipping room sync");
        return;
    }
    AuthenticationToken token = new AuthenticationToken(openfireToken);
    RestApiClient client = new RestApiClient(openfireUrl, Integer.parseInt(openfirePort, 10), token);
    for (String enabledWorkspace : enabledWorkspaces) {
        try {
            // Checking before creating is subject to a race condition, but in the worst case
            // the creation just fails, resulting in a log entry
            MUCRoomEntity chatRoomEntity = client.getChatRoom(enabledWorkspace);
            if (chatRoomEntity == null) {
                logger.log(Level.INFO, "Syncing chat workspace " + enabledWorkspace);
                SchoolDataIdentifier identifier = SchoolDataIdentifier.fromId(enabledWorkspace);
                if (identifier == null) {
                    logger.log(Level.WARNING, "Invalid workspace identifier " + enabledWorkspace + ", skipping...");
                    continue;
                }
                WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceByUrlName(enabledWorkspace);
                if (workspaceEntity == null) {
                    logger.log(Level.WARNING, "No workspace entity found for identifier " + enabledWorkspace + ", skipping...");
                    continue;
                }
                Workspace workspace = workspaceController.findWorkspace(workspaceEntity);
                chatRoomEntity = new MUCRoomEntity(enabledWorkspace, workspace.getName(), workspace.getDescription());
                client.createChatRoom(chatRoomEntity);
            }
        } catch (Exception e) {
            logger.log(Level.INFO, "Exception when syncing chat workspace " + enabledWorkspace, e);
        }
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) AuthenticationToken(fi.otavanopisto.muikku.openfire.rest.client.entity.AuthenticationToken) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) MUCRoomEntity(fi.otavanopisto.muikku.openfire.rest.client.entity.MUCRoomEntity) RestApiClient(fi.otavanopisto.muikku.openfire.rest.client.RestApiClient) Workspace(fi.otavanopisto.muikku.schooldata.entity.Workspace) TransactionAttribute(javax.ejb.TransactionAttribute) Schedule(javax.ejb.Schedule)

Aggregations

WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)1 RestApiClient (fi.otavanopisto.muikku.openfire.rest.client.RestApiClient)1 AuthenticationToken (fi.otavanopisto.muikku.openfire.rest.client.entity.AuthenticationToken)1 MUCRoomEntity (fi.otavanopisto.muikku.openfire.rest.client.entity.MUCRoomEntity)1 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)1 Workspace (fi.otavanopisto.muikku.schooldata.entity.Workspace)1 Schedule (javax.ejb.Schedule)1 TransactionAttribute (javax.ejb.TransactionAttribute)1