Search in sources :

Example 1 with NewProjectAdded

use of org.bimserver.models.log.NewProjectAdded in project BIMserver by opensourceBIM.

the class AddProjectDatabaseAction method execute.

@Override
public Project execute() throws UserException, BimserverDatabaseException, BimserverLockConflictException {
    User actingUser = getUserByUoid(authorization.getUoid());
    String trimmedName = name.trim();
    if (trimmedName.equals("")) {
        throw new UserException("Invalid project name");
    }
    final Project project = getDatabaseSession().create(Project.class);
    Project parentProject = null;
    if (parentPoid != -1) {
        parentProject = getProjectByPoid(parentPoid);
        project.setParent(parentProject);
        parentProject.getSubProjects().add(project);
        getDatabaseSession().store(parentProject);
    }
    if (actingUser.getUserType() != UserType.SYSTEM) {
        if (parentPoid == -1 && actingUser.getUserType() != UserType.ADMIN && !bimServer.getServerSettingsCache().getServerSettings().isAllowUsersToCreateTopLevelProjects()) {
            throw new UserException("Only administrators can create new projects");
        }
    }
    if (project.getParent() == null) {
        for (Project p : getProjectsByName(trimmedName)) {
            if (p.getParent() == null) {
                throw new UserException("Project name must be unique");
            }
        }
    } else {
        Project parent = project.getParent();
        for (Project subProject : parent.getSubProjects()) {
            if (subProject != project && subProject.getName().equals(trimmedName)) {
                throw new UserException("Project name must be unique within parent project (" + parent.getName() + ")");
            }
        }
        project.setGeoTag(parent.getGeoTag());
    }
    if (schema == null || (!schema.toLowerCase().equals("ifc2x3tc1") && !schema.toLowerCase().equals("ifc4"))) {
        throw new UserException("Invalid schema, the only 2 valid options are: \"ifc2x3tc1\" and \"ifc4\", not \"" + this.schema + "\"");
    }
    schema = schema.toLowerCase();
    final NewProjectAdded newProjectAdded = getDatabaseSession().create(NewProjectAdded.class);
    newProjectAdded.setDate(new Date());
    newProjectAdded.setExecutor(actingUser);
    newProjectAdded.setParentProject(parentProject);
    newProjectAdded.setProject(project);
    newProjectAdded.setAccessMethod(getAccessMethod());
    getDatabaseSession().addPostCommitAction(new PostCommitAction() {

        @Override
        public void execute() throws UserException {
            bimServer.getNotificationsManager().notify(new NewProjectNotification(bimServer, project.getOid()));
        // bimServer.getNotificationsManager().notify(new SConverter().convertToSObject(newProjectAdded));
        }
    });
    project.setSendEmailOnNewRevision(bimServer.getServerSettingsCache().getServerSettings().isSendEmailOnNewRevision());
    project.setId(getDatabaseSession().newPid());
    project.setName(trimmedName);
    project.setSchema(schema);
    // project.getHasAuthorizedUsers().add(getAdminUser());
    project.getHasAuthorizedUsers().add(actingUser);
    project.setCreatedBy(actingUser);
    project.setCreatedDate(new Date());
    project.setDescription("");
    project.setExportLengthMeasurePrefix(SIPrefix.METER);
    if (project.getParent() == null) {
        GeoTag geoTag = getDatabaseSession().create(GeoTag.class);
        geoTag.setEnabled(false);
        project.setGeoTag(geoTag);
        getDatabaseSession().store(geoTag);
    } else {
        GeoTag geoTag = parentProject.getGeoTag();
        project.setGeoTag(geoTag);
        getDatabaseSession().store(geoTag);
    }
    getDatabaseSession().store(project);
    getDatabaseSession().store(actingUser);
    getDatabaseSession().store(newProjectAdded);
    return project;
}
Also used : Project(org.bimserver.models.store.Project) GeoTag(org.bimserver.models.store.GeoTag) User(org.bimserver.models.store.User) PostCommitAction(org.bimserver.database.PostCommitAction) NewProjectAdded(org.bimserver.models.log.NewProjectAdded) UserException(org.bimserver.shared.exceptions.UserException) NewProjectNotification(org.bimserver.notifications.NewProjectNotification) Date(java.util.Date)

Aggregations

Date (java.util.Date)1 PostCommitAction (org.bimserver.database.PostCommitAction)1 NewProjectAdded (org.bimserver.models.log.NewProjectAdded)1 GeoTag (org.bimserver.models.store.GeoTag)1 Project (org.bimserver.models.store.Project)1 User (org.bimserver.models.store.User)1 NewProjectNotification (org.bimserver.notifications.NewProjectNotification)1 UserException (org.bimserver.shared.exceptions.UserException)1