Search in sources :

Example 1 with BimServerClientFactory

use of org.bimserver.shared.BimServerClientFactory in project BIMserver by opensourceBIM.

the class BimServerImporter method start.

public void start() {
    try (BimServerClientFactory factory = new JsonBimServerClientFactory(bimServer.getMetaDataManager(), address)) {
        LOGGER.info("Importing...");
        remoteClient = factory.create(new UsernamePasswordAuthenticationInfo(username, password));
        final BimDatabase database = bimServer.getDatabase();
        DatabaseSession databaseSession = database.createSession(OperationType.POSSIBLY_WRITE);
        try {
            LOGGER.info("Users...");
            for (SUser user : remoteClient.getServiceInterface().getAllUsers()) {
                createUser(databaseSession, user.getOid());
            }
            LOGGER.info("Projects...");
            for (SProject project : remoteClient.getServiceInterface().getAllProjects(false, false)) {
                createProject(databaseSession, project.getOid());
            }
            LOGGER.info("Done");
            databaseSession.commit();
        } catch (BimserverDatabaseException e) {
            LOGGER.error("", e);
        } finally {
            databaseSession.close();
        }
        final BimServerClientInterface client = bimServer.getBimServerClientFactory().create(new UsernamePasswordAuthenticationInfo(username, password));
        // for (SRenderEnginePluginConfiguration renderEnginePluginConfiguration : client.getPluginInterface().getAllRenderEngines(true)) {
        // if (renderEnginePluginConfiguration.getName().equals("IFC Engine DLL")) {
        // client.getPluginInterface().setDefaultRenderEngine(renderEnginePluginConfiguration.getOid());
        // }
        // }
        Path incoming = Paths.get(path);
        final Map<GregorianCalendar, Key> comments = new TreeMap<>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
        for (SProject project : remoteClient.getServiceInterface().getAllProjects(false, false)) {
            for (SRevision revision : remoteClient.getServiceInterface().getAllRevisionsOfProject(project.getOid())) {
                GregorianCalendar gregorianCalendar = new GregorianCalendar();
                gregorianCalendar.setTime(revision.getDate());
                if (!revision.getComment().startsWith("generated for")) {
                    User user = users.get(revision.getUserId());
                    Path userFolder = incoming.resolve(user.getUsername());
                    boolean found = false;
                    for (Path file : PathUtils.list(userFolder)) {
                        if (file.getFileName().toString().endsWith(revision.getComment())) {
                            String dateStr = file.getFileName().toString().substring(0, 19);
                            Date parse = dateFormat.parse(dateStr);
                            GregorianCalendar fileDate = new GregorianCalendar();
                            fileDate.setTime(parse);
                            long millisDiff = Math.abs(fileDate.getTimeInMillis() - revision.getDate().getTime());
                            if (millisDiff > 1000 * 60 * 120) {
                                // 120 minutes
                                continue;
                            }
                            if (revision.getOid() == project.getLastRevisionId()) {
                                comments.put(gregorianCalendar, new Key(file, project.getOid(), revision.getComment(), revision.getDate(), revision.getUserId()));
                            }
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        LOGGER.info("Not found: " + revision.getComment());
                    }
                }
            }
        }
        ExecutorService executorService = new ThreadPoolExecutor(1, 1, 1, TimeUnit.DAYS, new ArrayBlockingQueue<Runnable>(1000));
        for (final GregorianCalendar gregorianCalendar : comments.keySet()) {
            executorService.submit(new Runnable() {

                @Override
                public void run() {
                    Key key = comments.get(gregorianCalendar);
                    LOGGER.info("Checking in: " + key.file.getFileName().toString() + " " + Formatters.bytesToString(key.file.toFile().length()));
                    Project sProject = projects.get(key.poid);
                    try {
                        SDeserializerPluginConfiguration desserializer = client.getServiceInterface().getSuggestedDeserializerForExtension("ifc", sProject.getOid());
                        client.checkinSync(sProject.getOid(), key.comment, desserializer.getOid(), false, key.file);
                        SProject updatedProject = client.getServiceInterface().getProjectByPoid(sProject.getOid());
                        DatabaseSession databaseSession = database.createSession(OperationType.POSSIBLY_WRITE);
                        try {
                            LOGGER.info("Done");
                            Project project = databaseSession.get(updatedProject.getOid(), OldQuery.getDefault());
                            Revision revision = project.getLastRevision();
                            User user = (User) databaseSession.get(users.get(key.userId).getOid(), OldQuery.getDefault());
                            for (Revision otherRevision : revision.getConcreteRevisions().get(0).getRevisions()) {
                                otherRevision.load();
                                otherRevision.setDate(key.date);
                                otherRevision.setComment(otherRevision.getComment().replace("Administrator", user.getName()));
                                databaseSession.store(otherRevision);
                            }
                            DateFormat m = new SimpleDateFormat("dd-MM-yyyy");
                            LOGGER.info("Setting date to " + m.format(key.date));
                            revision.setUser(user);
                            revision.setDate(key.date);
                            databaseSession.store(revision);
                            databaseSession.commit();
                        } catch (BimserverDatabaseException | ServiceException e) {
                            LOGGER.error("", e);
                        } finally {
                            databaseSession.close();
                        }
                    } catch (IOException | UserException | ServerException | PublicInterfaceNotFoundException e) {
                        LOGGER.error("", e);
                    }
                }
            });
        }
        executorService.shutdown();
    } catch (ServiceException e) {
        LOGGER.error("", e);
    } catch (ChannelConnectionException e) {
        LOGGER.error("", e);
    } catch (PublicInterfaceNotFoundException e) {
        LOGGER.error("", e);
    } catch (ParseException e) {
        LOGGER.error("", e);
    } catch (IOException e) {
        LOGGER.error("", e);
    } catch (BimServerClientException e1) {
        LOGGER.error("", e1);
    } catch (Exception e2) {
        LOGGER.error("", e2);
    }
}
Also used : User(org.bimserver.models.store.User) SUser(org.bimserver.interfaces.objects.SUser) SDeserializerPluginConfiguration(org.bimserver.interfaces.objects.SDeserializerPluginConfiguration) DatabaseSession(org.bimserver.database.DatabaseSession) UsernamePasswordAuthenticationInfo(org.bimserver.shared.UsernamePasswordAuthenticationInfo) JsonBimServerClientFactory(org.bimserver.client.json.JsonBimServerClientFactory) SUser(org.bimserver.interfaces.objects.SUser) SProject(org.bimserver.interfaces.objects.SProject) JsonBimServerClientFactory(org.bimserver.client.json.JsonBimServerClientFactory) BimServerClientFactory(org.bimserver.shared.BimServerClientFactory) BimServerClientInterface(org.bimserver.plugins.services.BimServerClientInterface) Path(java.nio.file.Path) ChannelConnectionException(org.bimserver.shared.ChannelConnectionException) GregorianCalendar(java.util.GregorianCalendar) IOException(java.io.IOException) TreeMap(java.util.TreeMap) BimServerClientException(org.bimserver.shared.exceptions.BimServerClientException) Date(java.util.Date) ChannelConnectionException(org.bimserver.shared.ChannelConnectionException) ServiceException(org.bimserver.shared.exceptions.ServiceException) ParseException(java.text.ParseException) PublicInterfaceNotFoundException(org.bimserver.shared.exceptions.PublicInterfaceNotFoundException) BimServerClientException(org.bimserver.shared.exceptions.BimServerClientException) IOException(java.io.IOException) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) SRevision(org.bimserver.interfaces.objects.SRevision) Project(org.bimserver.models.store.Project) SProject(org.bimserver.interfaces.objects.SProject) Revision(org.bimserver.models.store.Revision) SRevision(org.bimserver.interfaces.objects.SRevision) ServiceException(org.bimserver.shared.exceptions.ServiceException) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) PublicInterfaceNotFoundException(org.bimserver.shared.exceptions.PublicInterfaceNotFoundException) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ParseException(java.text.ParseException) BimDatabase(org.bimserver.database.BimDatabase) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with BimServerClientFactory

use of org.bimserver.shared.BimServerClientFactory in project BIMserver by opensourceBIM.

the class TestBigModelEmfRemote method test.

@Test
public void test() {
    boolean doreuse = true;
    boolean useLowLevelCalls = false;
    try (BimServerClientFactory factory = new JsonBimServerClientFactory("http://localhost:8080")) {
        BimServerClientInterface bimServerClient = factory.create(new UsernamePasswordAuthenticationInfo("admin@bimserver.org", "admin"));
        SProject newProject = bimServerClient.getServiceInterface().addProject("test" + Math.random(), "ifc2x3tc1");
        IfcModelInterface model = null;
        if (useLowLevelCalls) {
            model = bimServerClient.newModel(newProject, true);
        } else {
            model = new BasicIfcModel(bimServerClient.getMetaDataManager().getPackageMetaData("ifc2x3tc1"), null);
        }
        RichIfcModel richIfcModel = new RichIfcModel(model, !useLowLevelCalls);
        IfcBuilding ifcBuilding = richIfcModel.createDefaultProjectStructure(0, 0, 0);
        IfcRelAggregates buildingAggregation = richIfcModel.create(IfcRelAggregates.class);
        buildingAggregation.setRelatingObject(ifcBuilding);
        for (int i = 1; i <= 200; i++) {
            IfcBuildingStorey ifcBuildingStorey = richIfcModel.create(IfcBuildingStorey.class);
            ifcBuildingStorey.setName("Storey " + i);
            ifcBuildingStorey.setCompositionType(IfcElementCompositionEnum.ELEMENT);
            ifcBuildingStorey.setElevation(3000 * i);
            IfcLocalPlacement storeyPlacement = richIfcModel.create(IfcLocalPlacement.class);
            storeyPlacement.setRelativePlacement(richIfcModel.createBasicPosition(0, 0, i * 3000));
            ifcBuildingStorey.setObjectPlacement(storeyPlacement);
            buildingAggregation.getRelatedObjects().add(ifcBuildingStorey);
            IfcRelAggregates storeyAggregation = richIfcModel.create(IfcRelAggregates.class);
            storeyAggregation.setRelatingObject(ifcBuildingStorey);
            for (int x = 1; x <= 40; x++) {
                for (int y = 1; y <= 40; y++) {
                    createSpace(richIfcModel, richIfcModel.getDefaultRepresentationContext(), storeyPlacement, storeyAggregation, x, y, doreuse);
                }
            }
        }
        long roid = -1;
        if (useLowLevelCalls) {
            roid = model.commit("Initial model");
        } else {
            Serializer serializer = new Ifc2x3tc1StepSerializer(null);
            serializer.init(model, null, true);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            serializer.writeToOutputStream(baos, null);
            java.nio.file.Files.write(Paths.get("tmp.ifc"), baos.toByteArray());
            SDeserializerPluginConfiguration deserializer = bimServerClient.getServiceInterface().getSuggestedDeserializerForExtension("ifc", newProject.getOid());
            SLongCheckinActionState checkinSync = bimServerClient.checkinSync(newProject.getOid(), "New", deserializer.getOid(), false, baos.size(), "newfile", new ByteArrayInputStream(baos.toByteArray()));
            roid = checkinSync.getRoid();
        }
        SSerializerPluginConfiguration serializerByContentType = bimServerClient.getServiceInterface().getSerializerByName("Ifc2x3tc1 (Streaming)");
        bimServerClient.download(roid, serializerByContentType.getOid(), new FileOutputStream(new File("created.ifc")));
    } catch (Throwable e) {
        e.printStackTrace();
        if (e instanceof AssertionError) {
            throw (AssertionError) e;
        }
        fail(e.getMessage());
    }
}
Also used : SDeserializerPluginConfiguration(org.bimserver.interfaces.objects.SDeserializerPluginConfiguration) RichIfcModel(org.bimserver.utils.RichIfcModel) UsernamePasswordAuthenticationInfo(org.bimserver.shared.UsernamePasswordAuthenticationInfo) IfcModelInterface(org.bimserver.emf.IfcModelInterface) JsonBimServerClientFactory(org.bimserver.client.json.JsonBimServerClientFactory) SProject(org.bimserver.interfaces.objects.SProject) JsonBimServerClientFactory(org.bimserver.client.json.JsonBimServerClientFactory) BimServerClientFactory(org.bimserver.shared.BimServerClientFactory) IfcLocalPlacement(org.bimserver.models.ifc2x3tc1.IfcLocalPlacement) BimServerClientInterface(org.bimserver.plugins.services.BimServerClientInterface) IfcBuildingStorey(org.bimserver.models.ifc2x3tc1.IfcBuildingStorey) IfcBuilding(org.bimserver.models.ifc2x3tc1.IfcBuilding) Serializer(org.bimserver.plugins.serializers.Serializer) Ifc2x3tc1StepSerializer(org.bimserver.ifc.step.serializer.Ifc2x3tc1StepSerializer) SLongCheckinActionState(org.bimserver.interfaces.objects.SLongCheckinActionState) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BasicIfcModel(org.bimserver.ifc.BasicIfcModel) Ifc2x3tc1StepSerializer(org.bimserver.ifc.step.serializer.Ifc2x3tc1StepSerializer) IfcRelAggregates(org.bimserver.models.ifc2x3tc1.IfcRelAggregates) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) SSerializerPluginConfiguration(org.bimserver.interfaces.objects.SSerializerPluginConfiguration) File(java.io.File) Test(org.junit.Test)

Example 3 with BimServerClientFactory

use of org.bimserver.shared.BimServerClientFactory in project BIMserver by opensourceBIM.

the class TestManyRevisions method start.

private void start(String[] args) {
    try {
        Path home = Paths.get("home");
        PluginManager pluginManager = LocalDevPluginLoader.createPluginManager(home);
        MetaDataManager metaDataManager = new MetaDataManager(home.resolve("tmp"));
        pluginManager.setMetaDataManager(metaDataManager);
        try (BimServerClientFactory factory = new JsonBimServerClientFactory(metaDataManager, "http://localhost:8080")) {
            BimServerClientInterface client = factory.create(new UsernamePasswordAuthenticationInfo("admin@bimserver.org", "admin"));
            try {
                SProject project = client.getServiceInterface().addProject("lots2", "ifc2x3tc1");
                Path[] files = new Path[] { Paths.get("../TestData/data/AC11-Institute-Var-2-IFC.ifc"), Paths.get("../TestData/data/AC11-FZK-Haus-IFC - Alt.ifc") };
                SDeserializerPluginConfiguration deserializer = client.getServiceInterface().getSuggestedDeserializerForExtension("ifc", project.getOid());
                int fn = 0;
                for (int i = 0; i < 20; i++) {
                    System.out.println(i + ": " + files[fn].getFileName().toString());
                    client.checkinSync(project.getOid(), "comment" + i, deserializer.getOid(), false, files[fn]);
                    fn = 1 - fn;
                }
            } catch (ServerException | UserException | PublicInterfaceNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Path(java.nio.file.Path) SDeserializerPluginConfiguration(org.bimserver.interfaces.objects.SDeserializerPluginConfiguration) ServerException(org.bimserver.shared.exceptions.ServerException) UsernamePasswordAuthenticationInfo(org.bimserver.shared.UsernamePasswordAuthenticationInfo) JsonBimServerClientFactory(org.bimserver.client.json.JsonBimServerClientFactory) MetaDataManager(org.bimserver.emf.MetaDataManager) IOException(java.io.IOException) SProject(org.bimserver.interfaces.objects.SProject) JsonBimServerClientFactory(org.bimserver.client.json.JsonBimServerClientFactory) BimServerClientFactory(org.bimserver.shared.BimServerClientFactory) PublicInterfaceNotFoundException(org.bimserver.shared.exceptions.PublicInterfaceNotFoundException) IOException(java.io.IOException) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) PluginManager(org.bimserver.plugins.PluginManager) PublicInterfaceNotFoundException(org.bimserver.shared.exceptions.PublicInterfaceNotFoundException) BimServerClientInterface(org.bimserver.plugins.services.BimServerClientInterface) UserException(org.bimserver.shared.exceptions.UserException)

Example 4 with BimServerClientFactory

use of org.bimserver.shared.BimServerClientFactory in project BIMserver by opensourceBIM.

the class TestInstall method main.

public static void main(String[] args) {
    ArrayList<String> pluginList = new ArrayList<>();
    pluginList.add("C:/plugins/ifcplugins-0.0.15.jar");
    try (BimServerClientFactory factory = new JsonBimServerClientFactory("http://localhost:8080")) {
        BimServerClientInterface client = factory.create(new UsernamePasswordAuthenticationInfo("admin@bimserver.org", "admin"));
        for (String each : pluginList) {
            try {
                DataSource fds = new FileDataSource(new File(each));
                DataHandler handler = new DataHandler(fds);
                client.getPluginInterface().installPluginBundleFromFile(handler, true, true);
                System.out.println("Plugin " + each + " successfully installed !");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } catch (BimServerClientException e1) {
        e1.printStackTrace();
    } catch (ServiceException e1) {
        e1.printStackTrace();
    } catch (ChannelConnectionException e1) {
        e1.printStackTrace();
    } catch (Exception e2) {
        e2.printStackTrace();
    }
}
Also used : ChannelConnectionException(org.bimserver.shared.ChannelConnectionException) UsernamePasswordAuthenticationInfo(org.bimserver.shared.UsernamePasswordAuthenticationInfo) JsonBimServerClientFactory(org.bimserver.client.json.JsonBimServerClientFactory) ArrayList(java.util.ArrayList) DataHandler(javax.activation.DataHandler) BimServerClientException(org.bimserver.shared.exceptions.BimServerClientException) BimServerClientFactory(org.bimserver.shared.BimServerClientFactory) JsonBimServerClientFactory(org.bimserver.client.json.JsonBimServerClientFactory) ChannelConnectionException(org.bimserver.shared.ChannelConnectionException) BimServerClientException(org.bimserver.shared.exceptions.BimServerClientException) ServiceException(org.bimserver.shared.exceptions.ServiceException) FileDataSource(org.bimserver.utils.FileDataSource) DataSource(javax.activation.DataSource) ServiceException(org.bimserver.shared.exceptions.ServiceException) FileDataSource(org.bimserver.utils.FileDataSource) BimServerClientInterface(org.bimserver.plugins.services.BimServerClientInterface) File(java.io.File)

Example 5 with BimServerClientFactory

use of org.bimserver.shared.BimServerClientFactory in project BIMserver by opensourceBIM.

the class ServiceImpl method getServiceDescriptor.

@Override
public SServiceDescriptor getServiceDescriptor(String baseUrl, String serviceIdentifier) throws ServerException, UserException {
    requireRealUserAuthentication();
    try {
        try (BimServerClientFactory factory = new JsonBimServerClientFactory(baseUrl, getBimServer().getServicesMap(), getBimServer().getJsonSocketReflectorFactory(), getBimServer().getReflectorFactory(), getBimServer().getMetaDataManager())) {
            try (BimServerClientInterface client = factory.create()) {
                SServiceDescriptor service = client.getRemoteServiceInterface().getService(serviceIdentifier);
                if (service == null) {
                    throw new UserException("No service found with identifier " + serviceIdentifier);
                }
                service.setUrl(baseUrl);
                return service;
            }
        }
    } catch (Exception e) {
        return handleException(e);
    }
}
Also used : JsonBimServerClientFactory(org.bimserver.client.json.JsonBimServerClientFactory) BimServerClientInterface(org.bimserver.plugins.services.BimServerClientInterface) UserException(org.bimserver.shared.exceptions.UserException) SServiceDescriptor(org.bimserver.interfaces.objects.SServiceDescriptor) JsonBimServerClientFactory(org.bimserver.client.json.JsonBimServerClientFactory) BimServerClientFactory(org.bimserver.shared.BimServerClientFactory) ServiceException(org.bimserver.shared.exceptions.ServiceException) IOException(java.io.IOException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) SerializerException(org.bimserver.plugins.serializers.SerializerException) BcfException(org.opensourcebim.bcf.BcfException) UserException(org.bimserver.shared.exceptions.UserException) CannotBeScheduledException(org.bimserver.longaction.CannotBeScheduledException) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) ServerException(org.bimserver.shared.exceptions.ServerException) PluginException(org.bimserver.shared.exceptions.PluginException) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException)

Aggregations

JsonBimServerClientFactory (org.bimserver.client.json.JsonBimServerClientFactory)12 BimServerClientFactory (org.bimserver.shared.BimServerClientFactory)12 BimServerClientInterface (org.bimserver.plugins.services.BimServerClientInterface)9 UsernamePasswordAuthenticationInfo (org.bimserver.shared.UsernamePasswordAuthenticationInfo)9 ServiceException (org.bimserver.shared.exceptions.ServiceException)9 IOException (java.io.IOException)8 ChannelConnectionException (org.bimserver.shared.ChannelConnectionException)7 BimServerClientException (org.bimserver.shared.exceptions.BimServerClientException)6 Path (java.nio.file.Path)5 PluginException (org.bimserver.shared.exceptions.PluginException)5 ServerException (org.bimserver.shared.exceptions.ServerException)5 UserException (org.bimserver.shared.exceptions.UserException)5 MetaDataManager (org.bimserver.emf.MetaDataManager)4 SDeserializerPluginConfiguration (org.bimserver.interfaces.objects.SDeserializerPluginConfiguration)4 SProject (org.bimserver.interfaces.objects.SProject)4 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)3 ProtocolBuffersBimServerClientFactory (org.bimserver.client.protocolbuffers.ProtocolBuffersBimServerClientFactory)3 SoapBimServerClientFactory (org.bimserver.client.soap.SoapBimServerClientFactory)3 DatabaseSession (org.bimserver.database.DatabaseSession)3 PluginManager (org.bimserver.plugins.PluginManager)3