Search in sources :

Example 1 with BimBotContext

use of org.bimserver.bimbots.BimBotContext in project BIMserver by opensourceBIM.

the class BimBotAbstractService method newRevision.

@Override
public void newRevision(RunningService runningService, BimServerClientInterface bimServerClientInterface, long poid, long roid, String userToken, long soid, SObjectType settings) throws Exception {
    try {
        long start = System.nanoTime();
        byte[] data = null;
        if (needsRawInput()) {
            // We need to generate some, by serializing the current model
            SSerializerPluginConfiguration serializerByContentType = bimServerClientInterface.getServiceInterface().getSerializerByContentType("application/ifc");
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            bimServerClientInterface.download(roid, serializerByContentType.getOid(), outputStream);
            data = outputStream.toByteArray();
        }
        BimBotsInput input = new BimBotsInput(SchemaName.IFC_STEP_2X3TC1, data);
        SProject project = bimServerClientInterface.getServiceInterface().getProjectByPoid(poid);
        String contextId = project.getUuid().toString();
        IfcModelInterface model = bimServerClientInterface.getModel(project, roid, preloadCompleteModel(), false, requiresGeometry());
        input.setIfcModel(model);
        BimBotContext bimBotContext = new BimBotContext() {

            @Override
            public void updateProgress(String label, int percentage) {
            }

            @Override
            public String getCurrentUser() {
                return runningService.getCurrentUser();
            }

            @Override
            public String getContextId() {
                return contextId;
            }
        };
        BimBotsOutput output = runBimBot(input, bimBotContext, new PluginConfiguration(settings));
        long end = System.nanoTime();
        if (output.getSchemaName().contentEquals(SchemaName.IFC_STEP_2X3TC1.name()) || output.getSchemaName().contentEquals(SchemaName.IFC_STEP_4.name())) {
            // There is no point in storing the retuned model as extended data, lets make a new revision
            output.getModel().checkin(poid, output.getTitle());
        } else {
            SFile file = new SFile();
            SExtendedData extendedData = new SExtendedData();
            extendedData.setTimeToGenerate((end - start) / 1000000);
            extendedData.setTitle(output.getTitle());
            extendedData.setSize(output.getData().length);
            file.setFilename(output.getContentDisposition());
            SExtendedDataSchema extendedDataSchemaByName = null;
            try {
                extendedDataSchemaByName = bimServerClientInterface.getServiceInterface().getExtendedDataSchemaByName(output.getSchemaName());
            } catch (Exception e) {
                extendedDataSchemaByName = new SExtendedDataSchema();
                extendedDataSchemaByName.setContentType(output.getContentType());
                extendedDataSchemaByName.setName(output.getSchemaName());
                bimServerClientInterface.getServiceInterface().addExtendedDataSchema(extendedDataSchemaByName);
            }
            extendedData.setSchemaId(extendedDataSchemaByName.getOid());
            file.setData(output.getData());
            file.setSize(output.getData().length);
            file.setMime(output.getContentType());
            long fileId = bimServerClientInterface.getServiceInterface().uploadFile(file);
            extendedData.setFileId(fileId);
            bimServerClientInterface.getServiceInterface().addExtendedDataToRevision(roid, extendedData);
        }
    } catch (BimBotsException e) {
        LOGGER.error("", e);
    } catch (Throwable e) {
        LOGGER.error("", e);
    }
}
Also used : IfcModelInterface(org.bimserver.emf.IfcModelInterface) BimBotsException(org.bimserver.bimbots.BimBotsException) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) SProject(org.bimserver.interfaces.objects.SProject) SExtendedDataSchema(org.bimserver.interfaces.objects.SExtendedDataSchema) BimBotsException(org.bimserver.bimbots.BimBotsException) SExtendedData(org.bimserver.interfaces.objects.SExtendedData) BimBotsInput(org.bimserver.bimbots.BimBotsInput) BimBotContext(org.bimserver.bimbots.BimBotContext) BimBotsOutput(org.bimserver.bimbots.BimBotsOutput) PluginConfiguration(org.bimserver.plugins.PluginConfiguration) SSerializerPluginConfiguration(org.bimserver.interfaces.objects.SSerializerPluginConfiguration) SSerializerPluginConfiguration(org.bimserver.interfaces.objects.SSerializerPluginConfiguration) SFile(org.bimserver.interfaces.objects.SFile)

Example 2 with BimBotContext

use of org.bimserver.bimbots.BimBotContext in project BIMserver by opensourceBIM.

the class BimBotRunner method runBimBot.

public BimBotsOutput runBimBot() throws UserException, IOException {
    BimBotContext bimBotContext = new BimBotContext() {

        @Override
        public void updateProgress(String label, int percentage) {
            if (streamingSocketInterface != null) {
                ObjectNode message = objectMapper.createObjectNode();
                message.put("type", "progress");
                message.put("topicId", topicId);
                ObjectNode payload = objectMapper.createObjectNode();
                payload.put("progress", percentage);
                payload.put("label", label);
                message.set("payload", payload);
                streamingSocketInterface.send(message);
            }
        }

        public String getCurrentUser() {
            return authorization.getUsername();
        }

        public String getContextId() {
            return contextId;
        }
    };
    try (DatabaseSession session = bimServer.getDatabase().createSession(OperationType.READ_ONLY)) {
        ServiceMap serviceMap = bimServer.getServiceFactory().get(authorization, AccessMethod.INTERNAL);
        ServiceInterface serviceInterface = serviceMap.get(ServiceInterface.class);
        if (bimServer.getServerSettingsCache().getServerSettings().isStoreServiceRuns()) {
            long start = System.nanoTime();
            // When we store service runs, we can just use the streaming deserializer to stream directly to the database, after that we'll trigger the actual service
            // Create or find project and link user and service to project
            // Checkin stream into project
            // Trigger service
            byte[] data = null;
            if (bimBotsServiceInterface.needsRawInput()) {
                // We need the raw input later on, lets just get it now
                data = ByteStreams.toByteArray(inputStream);
                // Make a clean inputstream that uses the data as the original won't be available after this
                inputStream = new ByteArrayInputStream(data);
            }
            SchemaName schema = SchemaName.valueOf(inputType);
            String projectSchema = getProjectSchema(serviceInterface, schema);
            SProject project = null;
            String uuid = contextId;
            if (uuid != null) {
                project = serviceInterface.getProjectByUuid(uuid);
            } else {
                project = serviceInterface.addProject("tmp-" + new Random().nextInt(), projectSchema);
            }
            SDeserializerPluginConfiguration deserializer = serviceInterface.getSuggestedDeserializerForExtension("ifc", project.getOid());
            if (deserializer == null) {
                throw new BimBotsException("No deserializer found", BimBotDefaultErrorCode.NO_DESERIALIZER);
            }
            Long topicId = serviceInterface.initiateCheckin(project.getOid(), deserializer.getOid());
            ProgressTopic progressTopic = null;
            VirtualEndPoint virtualEndpoint = null;
            try {
                progressTopic = bimServer.getNotificationsManager().getProgressTopic(topicId);
                virtualEndpoint = new VirtualEndPoint() {

                    @Override
                    public NotificationInterface getNotificationInterface() {
                        return new NotificationInterfaceAdaptor() {

                            @Override
                            public void progress(Long topicId, SLongActionState state) throws UserException, ServerException {
                                bimBotContext.updateProgress(state.getTitle(), state.getProgress());
                            }
                        };
                    }
                };
                progressTopic.register(virtualEndpoint);
            } catch (TopicRegisterException e1) {
                e1.printStackTrace();
            }
            serviceInterface.checkinInitiatedSync(topicId, project.getOid(), "Auto checkin", deserializer.getOid(), -1L, "s", new DataHandler(new InputStreamDataSource(inputStream)), false);
            project = serviceInterface.getProjectByPoid(project.getOid());
            PackageMetaData packageMetaData = bimServer.getMetaDataManager().getPackageMetaData(project.getSchema());
            BasicIfcModel model = new BasicIfcModel(packageMetaData, null);
            model.setPluginClassLoaderProvider(bimServer.getPluginManager());
            try {
                Revision revision = session.get(project.getLastRevisionId(), OldQuery.getDefault());
                session.getMap(model, new OldQuery(packageMetaData, project.getId(), revision.getId(), revision.getOid(), Deep.NO));
                model.getModelMetaData().setIfcHeader(revision.getLastConcreteRevision().getIfcHeader());
            } catch (BimserverDatabaseException e) {
                e.printStackTrace();
            }
            BimServerBimBotsInput input = new BimServerBimBotsInput(bimServer, authorization.getUoid(), inputType, data, model, false);
            BimBotsOutput output = bimBotsServiceInterface.runBimBot(input, bimBotContext, settings);
            long end = System.nanoTime();
            if (output.getModel() != null) {
                SerializerPlugin plugin = bimServer.getPluginManager().getSerializerPlugin("org.bimserver.ifc.step.serializer.Ifc2x3tc1StepSerializerPlugin");
                Serializer serializer = plugin.createSerializer(new PluginConfiguration());
                serializer.init(output.getModel(), new ProjectInfo(), true);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                serializer.writeToOutputStream(baos, null);
                output.setData(baos.toByteArray());
                output.setContentType("application/ifc");
            }
            SExtendedData extendedData = new SExtendedData();
            SFile file = new SFile();
            file.setData(output.getData());
            file.setFilename(output.getContentDisposition());
            file.setMime(output.getContentType());
            file.setSize(output.getData().length);
            Long fileId = serviceInterface.uploadFile(file);
            long ms = (end - start) / 1000000;
            extendedData.setTimeToGenerate(ms);
            extendedData.setFileId(fileId);
            extendedData.setTitle(output.getTitle());
            SExtendedDataSchema extendedDataSchema = null;
            try {
                extendedDataSchema = serviceInterface.getExtendedDataSchemaByName(output.getSchemaName());
            } catch (UserException e) {
                extendedDataSchema = new SExtendedDataSchema();
                extendedDataSchema.setContentType(output.getContentType());
                extendedDataSchema.setName(output.getSchemaName());
                serviceInterface.addExtendedDataSchema(extendedDataSchema);
            }
            extendedData.setSchemaId(extendedDataSchema.getOid());
            serviceInterface.addExtendedDataToRevision(project.getLastRevisionId(), extendedData);
            output.setContextId(project.getUuid().toString());
            if (progressTopic != null) {
                try {
                    progressTopic.unregister(virtualEndpoint);
                } catch (TopicRegisterException e) {
                    e.printStackTrace();
                }
            }
            return output;
        } else {
            // When we don't store the service runs, there is no other way than to just use the old deserializer and run the service from the EMF model
            LOGGER.info("NOT Storing intermediate results");
            String projectSchema = getProjectSchema(serviceInterface, SchemaName.valueOf(inputType));
            Schema schema = Schema.valueOf(projectSchema.toUpperCase());
            DeserializerPlugin deserializerPlugin = bimServer.getPluginManager().getFirstDeserializer("ifc", schema, true);
            if (deserializerPlugin == null) {
                throw new BimBotsException("No deserializer plugin found", BimBotDefaultErrorCode.NO_DESERIALIZER);
            }
            byte[] data = IOUtils.toByteArray(inputStream);
            Deserializer deserializer = deserializerPlugin.createDeserializer(new PluginConfiguration());
            PackageMetaData packageMetaData = bimServer.getMetaDataManager().getPackageMetaData(schema.name());
            deserializer.init(packageMetaData);
            IfcModelInterface model = deserializer.read(new ByteArrayInputStream(data), inputType, data.length, null);
            BimServerBimBotsInput input = new BimServerBimBotsInput(bimServer, authorization.getUoid(), inputType, data, model, true);
            BimBotsOutput output = bimBotsServiceInterface.runBimBot(input, bimBotContext, new PluginConfiguration(foundService.getSettings()));
            return output;
        }
    } catch (BimBotsException e) {
        ObjectNode errorNode = OBJECT_MAPPER.createObjectNode();
        errorNode.put("code", e.getErrorCode());
        errorNode.put("message", e.getMessage());
        BimBotsOutput bimBotsOutput = new BimBotsOutput("ERROR", errorNode.toString().getBytes(Charsets.UTF_8));
        return bimBotsOutput;
    } catch (Throwable e) {
        LOGGER.error("", e);
        ObjectNode errorNode = OBJECT_MAPPER.createObjectNode();
        errorNode.put("code", 500);
        // TODO should not leak this much info to called, but for now this is useful debugging info
        errorNode.put("message", "Unknown error: " + e.getMessage());
        BimBotsOutput bimBotsOutput = new BimBotsOutput("ERROR", errorNode.toString().getBytes(Charsets.UTF_8));
        return bimBotsOutput;
    }
}
Also used : SDeserializerPluginConfiguration(org.bimserver.interfaces.objects.SDeserializerPluginConfiguration) ServiceMap(org.bimserver.webservices.ServiceMap) DatabaseSession(org.bimserver.database.DatabaseSession) IfcModelInterface(org.bimserver.emf.IfcModelInterface) SExtendedDataSchema(org.bimserver.interfaces.objects.SExtendedDataSchema) Schema(org.bimserver.emf.Schema) SLongActionState(org.bimserver.interfaces.objects.SLongActionState) DataHandler(javax.activation.DataHandler) SProject(org.bimserver.interfaces.objects.SProject) SExtendedDataSchema(org.bimserver.interfaces.objects.SExtendedDataSchema) InputStreamDataSource(org.bimserver.utils.InputStreamDataSource) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) TopicRegisterException(org.bimserver.notifications.TopicRegisterException) Random(java.util.Random) BimBotsServiceInterface(org.bimserver.bimbots.BimBotsServiceInterface) ServiceInterface(org.bimserver.shared.interfaces.ServiceInterface) ProjectInfo(org.bimserver.plugins.serializers.ProjectInfo) InternalServicePluginConfiguration(org.bimserver.models.store.InternalServicePluginConfiguration) SDeserializerPluginConfiguration(org.bimserver.interfaces.objects.SDeserializerPluginConfiguration) PluginConfiguration(org.bimserver.plugins.PluginConfiguration) UserException(org.bimserver.shared.exceptions.UserException) VirtualEndPoint(org.bimserver.endpoints.VirtualEndPoint) SFile(org.bimserver.interfaces.objects.SFile) Serializer(org.bimserver.plugins.serializers.Serializer) ServerException(org.bimserver.shared.exceptions.ServerException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PackageMetaData(org.bimserver.emf.PackageMetaData) DeserializerPlugin(org.bimserver.plugins.deserializers.DeserializerPlugin) BimBotsException(org.bimserver.bimbots.BimBotsException) SerializerPlugin(org.bimserver.plugins.serializers.SerializerPlugin) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) BasicIfcModel(org.bimserver.ifc.BasicIfcModel) BimServerBimBotsInput(org.bimserver.bimbots.BimServerBimBotsInput) OldQuery(org.bimserver.database.OldQuery) ProgressTopic(org.bimserver.notifications.ProgressTopic) Revision(org.bimserver.models.store.Revision) SExtendedData(org.bimserver.interfaces.objects.SExtendedData) ByteArrayInputStream(java.io.ByteArrayInputStream) BimBotContext(org.bimserver.bimbots.BimBotContext) Deserializer(org.bimserver.plugins.deserializers.Deserializer) BimBotsOutput(org.bimserver.bimbots.BimBotsOutput) NotificationInterface(org.bimserver.shared.interfaces.NotificationInterface) NotificationInterfaceAdaptor(org.bimserver.shared.interfaces.NotificationInterfaceAdaptor) SchemaName(org.bimserver.plugins.SchemaName)

Aggregations

ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)2 BimBotContext (org.bimserver.bimbots.BimBotContext)2 BimBotsException (org.bimserver.bimbots.BimBotsException)2 BimBotsOutput (org.bimserver.bimbots.BimBotsOutput)2 IfcModelInterface (org.bimserver.emf.IfcModelInterface)2 SExtendedData (org.bimserver.interfaces.objects.SExtendedData)2 SExtendedDataSchema (org.bimserver.interfaces.objects.SExtendedDataSchema)2 SFile (org.bimserver.interfaces.objects.SFile)2 SProject (org.bimserver.interfaces.objects.SProject)2 PluginConfiguration (org.bimserver.plugins.PluginConfiguration)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Random (java.util.Random)1 DataHandler (javax.activation.DataHandler)1 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)1 BimBotsInput (org.bimserver.bimbots.BimBotsInput)1 BimBotsServiceInterface (org.bimserver.bimbots.BimBotsServiceInterface)1 BimServerBimBotsInput (org.bimserver.bimbots.BimServerBimBotsInput)1 DatabaseSession (org.bimserver.database.DatabaseSession)1 OldQuery (org.bimserver.database.OldQuery)1